Skip to content

Commit 8b68723

Browse files
committed
Fixes dbpedia#8 Added Ignore Word NLP for language tool, Added some test cases
When DBpedia is down and the user asks natural language/factoid question then the bot replies with appropriate message that the service is down
1 parent 74ba31c commit 8b68723

File tree

12 files changed

+65
-7
lines changed

12 files changed

+65
-7
lines changed

src/main/java/chatbot/Application.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.languagetool.language.AmericanEnglish;
1313
import org.languagetool.language.BritishEnglish;
1414
import org.languagetool.rules.Rule;
15+
import org.languagetool.rules.spelling.SpellingCheckRule;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
1718
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,9 @@
3031
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
3132
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
3233

34+
import java.util.Arrays;
35+
import java.util.List;
36+
3337
/**
3438
* Created by ramgathreya on 5/10/17.
3539
* http://bits-and-kites.blogspot.com/2015/03/spring-and-nodejs.html
@@ -114,6 +118,12 @@ public Helper(final CloudantClient cloudantClient,
114118
this.tmdbApiKey = tmdbApiKey;
115119
sparql = new SPARQL(explorerDB);
116120
languageTool = new JLanguageTool(new AmericanEnglish());
121+
for (Rule rule : languageTool.getAllActiveRules()) {
122+
if (rule instanceof SpellingCheckRule) {
123+
List<String> wordsToIgnore = Arrays.asList("nlp");
124+
((SpellingCheckRule)rule).addIgnoreTokens(wordsToIgnore);
125+
}
126+
}
117127
}
118128

119129
public RiveScriptBot getRiveScriptBot() {

src/main/java/chatbot/lib/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Created by ramgathreya on 6/21/17.
77
*/
88
public interface Constants {
9-
int API_TIMEOUT = 10000;
9+
int API_TIMEOUT = 5000;
1010

1111
// Services
1212
String DBPEDIA_SERVICE = "dbpedia";

src/main/java/chatbot/lib/api/SPARQL.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package chatbot.lib.api;
22

3+
import chatbot.lib.Constants;
34
import chatbot.lib.Utility;
45
import chatbot.lib.request.TemplateType;
56
import chatbot.lib.response.ResponseData;
@@ -8,6 +9,7 @@
89
import com.cloudant.client.api.views.Key;
910
import org.apache.jena.query.*;
1011
import org.apache.jena.rdf.model.RDFNode;
12+
import org.apache.jena.sparql.engine.http.QueryEngineHTTP;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315

@@ -379,7 +381,9 @@ public String getRDFTypes(String uri) {
379381
public QueryExecution executeQuery(String queryString) {
380382
logger.info("SPARQL Query is:\n" + queryString);
381383
Query query = QueryFactory.create(queryString);
382-
return QueryExecutionFactory.sparqlService(ENDPOINT, query);
384+
QueryEngineHTTP queryEngine = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ENDPOINT, query);
385+
queryEngine.addParam("timeout", String.valueOf(Constants.API_TIMEOUT));
386+
return queryEngine;
383387
}
384388

385389
public static class ProcessedResponse {

src/main/java/chatbot/lib/api/StatusCheckService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package chatbot.lib.api;
22

3+
import chatbot.lib.Constants;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -16,8 +17,6 @@
1617
public class StatusCheckService {
1718
private static final Logger logger = LoggerFactory.getLogger(StatusCheckService.class);
1819

19-
private static final int TIMEOUT = 50000;
20-
2120
private String url;
2221

2322
public String getUrl() {
@@ -33,8 +32,8 @@ public boolean isOnline() {
3332
try {
3433
HttpURLConnection connection = (HttpURLConnection) new URL(url)
3534
.openConnection();
36-
connection.setConnectTimeout(TIMEOUT);
37-
connection.setReadTimeout(TIMEOUT);
35+
connection.setConnectTimeout(Constants.API_TIMEOUT);
36+
connection.setReadTimeout(Constants.API_TIMEOUT);
3837
connection.setRequestMethod("HEAD");
3938
int responseCode = connection.getResponseCode();
4039
if (responseCode != 200) {
@@ -45,4 +44,8 @@ public boolean isOnline() {
4544
}
4645
return true;
4746
}
47+
48+
public boolean isDBpediaOnline() {
49+
return this.setUrl(Constants.SERVICES.get(Constants.DBPEDIA_SPARQL_SERVICE)[1]).isOnline();
50+
}
4851
}

src/main/java/chatbot/lib/handlers/NLHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import chatbot.Application;
44
import chatbot.lib.Utility;
5+
import chatbot.lib.api.StatusCheckService;
56
import chatbot.lib.api.qa.QAService;
67
import chatbot.lib.api.SPARQL;
78
import chatbot.lib.request.TemplateType;
@@ -39,6 +40,12 @@ public NLHandler(Request request, String question, Application.Helper helper) {
3940

4041
public ResponseGenerator answer() throws Exception {
4142
ResponseGenerator responseGenerator = new ResponseGenerator();
43+
44+
// Check if DBpedia is Live
45+
if(!new StatusCheckService().isDBpediaOnline()) {
46+
return responseGenerator.setDBpediaFallbackResponse(request, helper.getRiveScriptBot());
47+
}
48+
4249
QAService.Data data = qaService.search(question);
4350
SPARQL.ProcessedResponse processedResponse = processResponseData(data);
4451
List<ResponseData> responseDatas = processedResponse.getResponseData();

src/main/java/chatbot/lib/handlers/TextHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,8 @@ public ResponseGenerator handleTextMessage() throws Exception {
9393
}
9494
return responseGenerator;
9595
}
96+
97+
public String getTextMessage() {
98+
return textMessage;
99+
}
96100
}

src/main/java/chatbot/lib/response/ResponseGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ public ResponseGenerator setFallbackResponse(Request request, RiveScriptBot rive
9797
return this;
9898
}
9999

100+
// Needs to be used when DBpedia is down
101+
public ResponseGenerator setDBpediaFallbackResponse(Request request, RiveScriptBot riveScriptBot) {
102+
showFeedback = false;
103+
addTextResponse(new ResponseData(riveScriptBot.answer(request.getUserId(), RiveScriptReplyType.DBPEDIA_FALLBACK_TEXT)[0]));
104+
return this;
105+
}
106+
100107
public ResponseGenerator setNoResultsResponse(Request request, RiveScriptBot riveScriptBot) {
101108
showFeedback = false;
102109
addTextResponse(new ResponseData(riveScriptBot.answer(request.getUserId(), RiveScriptReplyType.NO_RESULTS_TEXT)[0]));

src/main/java/chatbot/rivescript/RiveScriptReplyType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface RiveScriptReplyType {
1212

1313
// String Scenarios where we just want different kinds of messages from Rive
1414
String FALLBACK_TEXT = "fallbacktext";
15+
String DBPEDIA_FALLBACK_TEXT = "dbpediafallbacktext";
1516
String NO_RESULTS_TEXT = "noresultstext";
1617

1718
String HELP_TEXT = "helptext";

src/main/resources/rivescript/begin.rive

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
! sub natural language processing = nlp
4242
! sub language processing = nlp
4343
! sub datset = dataset
44+
! sub data set = dataset
4445
! sub mappings.dbpedia = dbpedia mappings
4546
! sub mapping.dbpedia = dbpedia mappings
4647
! sub mapping = mappings

src/main/resources/rivescript/scenario-text.rive

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
+ dbpedialanguagetext *
3737
- Here are some links about <formal>:
3838

39+
+ dbpediafallbacktext
40+
- I'm sorry, DBpedia seems to be down right now. So I will be unable to answer factual questions temporarily.
41+
3942
+ fallbacktext
4043
- I'm sorry, but I couldn’t understand your question. I'm still learning, and will get better.
4144

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package chatbot.lib.handlers;
2+
3+
import chatbot.lib.TestUtility;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
/**
9+
* Created by ramgathreya on 8/1/17.
10+
*/
11+
public class TestTextHandler {
12+
13+
@Test
14+
public void testSanitizeText() throws Exception {
15+
System.out.println(new TextHandler(null, "download nlp dataset", TestUtility.getHelper()).getTextMessage());
16+
assertEquals("download nlp dataset", new TextHandler(null, "download nlp dataset", TestUtility.getHelper()).getTextMessage());
17+
}
18+
}

src/test/java/rivescript/dbpedia/TestDBpediaDataset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class TestDBpediaDataset extends RiveScriptBase {
1212
@Test
1313
public void testDBpediaDataset() {
14-
String[] testCases = new String[]{"I'm starting my master research work on NLP. I want to use DBpedia dataset. But I don't know how to use it. Who can help me?"};
14+
String[] testCases = new String[]{"I'm starting my master research work on NLP. I want to use DBpedia dataset. But I don't know how to use it. Who can help me?", "Download nlp dataset"};
1515
ArrayList<String[]> expectedAnswer = new ArrayList<String[]>(){{
1616
add(new String[]{"{\"type\": \"template\", \"name\": \"dbpedia-dataset-nlp\"}"});
1717
}};

0 commit comments

Comments
 (0)