-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestionProcessor.java
617 lines (537 loc) · 30.8 KB
/
QuestionProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package fyp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import org.apache.http.client.cache.ResourceFactory;
import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.AnonId;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelGraphInterface;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.RDFVisitor;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.reasoner.TriplePattern;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreAnnotations.PossibleAnswersAnnotation;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.util.CoreMap;
import net.sf.extjwnl.JWNLException;
import net.sf.extjwnl.data.POS;
import net.sf.extjwnl.data.PointerType;
public class QuestionProcessor {
JenaHandler knowledgeBase, questionBase;
StanfordHandler stanfordHandler;
ExtJWNLHandler extJWNLHandler;
Path questionPath, answerPath;
String source = "platform:/resource/JenaTrial/src/EnOntology.owl";
ArrayList<String> xsolutions;
ArrayList<String> ysolutions;
public QuestionProcessor(String mainPath, String questionFile, String answerFile, StanfordHandler stanfordHandler, ExtJWNLHandler extJWNLHandler, JenaHandler knowledgeBase, JenaHandler questionBase) {
questionPath = FileSystems.getDefault().getPath(mainPath,questionFile);
answerPath = FileSystems.getDefault().getPath(mainPath, answerFile);
this.stanfordHandler = stanfordHandler;
this.knowledgeBase = knowledgeBase;
this.questionBase = questionBase;
this.extJWNLHandler = extJWNLHandler;
xsolutions = new ArrayList<>();
ysolutions = new ArrayList<>();
}
public Resource getResourceWithProperty(Resource start, String property) {
Resource toReturn = null;
StmtIterator stmtIterator = start.listProperties();
while(stmtIterator.hasNext()) {
Statement statement = stmtIterator.next();
if(statement.getPredicate().toString().equals(property)) {
toReturn = statement.getObject().asResource();
//System.out.println(predicate);
}
}
return toReturn;
}
public String getLiteralWithProperty(Resource start, String property) {
String toReturn = null;
StmtIterator stmtIterator = start.listProperties();
while(stmtIterator.hasNext()) {
Statement statement = stmtIterator.next();
if(statement.getPredicate().toString().equals(property)) {
toReturn = statement.getObject().asLiteral().getString();
//System.out.println(predicate);
}
}
return toReturn;
}
public void printAllStatements(Resource resource) {
StmtIterator stmtIterator = resource.listProperties();
while(stmtIterator.hasNext()) {
Statement statement = stmtIterator.next();
//System.out.println(statement);
}
}
public Set<RDFNode> returnPossiblePredicates(String wordtext) throws JWNLException {
Set<String> possibleStrings = extJWNLHandler.getSynonyms(POS.VERB,extJWNLHandler.getIndexWord("Verb", wordtext));
System.out.println(possibleStrings);
Set<RDFNode> rdfNodes = new HashSet<>();
Iterator<String> iterator = possibleStrings.iterator();
while(iterator.hasNext()) {
String something = iterator.next().toUpperCase();
System.out.println("See if sentence has " + something);
String query = "SELECT ?sentence ?predicate WHERE { ?predicate <"+source.concat("#Word_Text")+"> \""+something+"\". ?sentence <"+source.concat("#Predicate")+"> ?predicate }";
QuerySolution querySolution;
try (QueryExecution qexec = QueryExecutionFactory.create(query, knowledgeBase.ontModel)) {
ResultSet results = qexec.execSelect();
while(results.hasNext()) {
System.out.println("There is a sentence");
querySolution = results.next();
rdfNodes.add(querySolution.get("?sentence"));
}
}
catch(Exception e) {}
}
return rdfNodes;
}
public Set<String> getSolutionOnThisNode(RDFNode tempNode, RDFNode predicate, char var, Set<String> solutionBag) {
if(predicate.isResource() && tempNode.isResource()) {
//System.out.println("I am in the first if, where both predicate and rdfnode are resources");
if(predicate.asResource().hasProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text"))) {
//System.out.println("In the nested if, now I know predicate has Word-Text");
if(predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) == 'X' || predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) == 'Y' || predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) == 'Z') {
//System.out.println(rdfNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
//solutions.add(rdfNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
System.out.println("Now I check if the resource is a variable.");
//System.out.print(rdfNode.toString());
if(predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) == var) {
System.out.println(tempNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
solutionBag.add(tempNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
}
//if(predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) == 'X')
// xsolutions.add(rdfNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
//else if(predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) == 'Y')
// ysolutions.add(rdfNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
//return true;
}
}
StmtIterator statements = predicate.asResource().listProperties();
while(statements.hasNext()) {
Statement tempStatement = statements.next();//System.out.println("------ Statement I encountered is "+tempStatement);
Property property = tempStatement.getPredicate();//System.out.println("------ The property I am testing tempnode for is, "+property);
if(tempNode.asResource().hasProperty(property)) {
try {
getSolutionOnThisNode(tempNode.asResource().getProperty(property).getObject(), predicate.asResource().getProperty(property).getObject(), var,solutionBag);
}
catch(Exception e) {
System.out.println("----------- Exception occured");
}
}
}
}
return solutionBag;
}
public Set<String> getPossibleSolutions(Set<RDFNode> rdfNodes, RDFNode predicate, char var) throws IOException, JWNLException {
Set<String> strings = new HashSet<>();
System.out.println("Getting posssible solutions");
boolean flagDontConsider = false;
Property theUnkonwn = null;
/**
* 1. For each RDFNode in rdfnodes as rdfnode
* 2. I get the dependencies of rdfnode
* 3. I loop through the dependencies
* 4. If the dependency exists, I check for values of both
* 5. If the values are equal, I do not do anything
* 6. else if one of the values is X,Y,Z etc., I do not do anything
* 7. else, I ignore this rdfnode and go for next rdfnode
* 8. else, I ignore this rdfnode and go for next rdfnode
* 9. Despite all the difficulties, if I reach here, add that answer to the set.
*/
FileWriter fileWriter = new FileWriter("C:/Users/Sadhana/workspace/System/src/fyp/referenceText.txt", true);
Iterator<RDFNode> rdfiterator = rdfNodes.iterator();
Set<String> solutionBag = new HashSet<>();
while(rdfiterator.hasNext()) {
RDFNode tempNode = rdfiterator.next();
tempNode = tempNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Predicate")).getObject();
//System.out.println(compareRDFNodes(predicate, tempNode));
if(compareRDFNodes(predicate, tempNode)) {
//System.out.println("Reference sentence is :" + tempNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Clause")).getObject().toString());
//tempNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString();
//System.out.println(tempNode.asNode().getURI());
String tempNodeID = tempNode.asNode().getURI();
tempNodeID = tempNodeID.substring(tempNodeID.indexOf('#')+1);
tempNodeID = tempNodeID.substring(0, tempNodeID.indexOf('_'));
System.out.println(tempNodeID);
fileWriter.write(knowledgeBase.ontModel.getBaseModel().getResource(source+"#"+tempNodeID).asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Sentence_Text")).getObject().toString());
//int sentenceNumber = tempNode.asResource().getProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Predicate")
getSolutionOnThisNode(tempNode, predicate, var, solutionBag);
System.out.println(solutionBag);
}
}
System.out.print("At the end of the while loop: ");
System.out.println(solutionBag);
fileWriter.close();
return solutionBag;
}
public boolean compareRDFNodes(RDFNode predicate, RDFNode rdfNode) throws JWNLException {
boolean toReturn = true;
//System.out.println("Should consider equality? : "+considerEquality);
if(predicate.isResource() && rdfNode.isResource()) {
if(predicate.asResource().hasProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text"))) {
if(rdfNode.asResource().hasProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text"))) {
if(predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) != 'X' && predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) != 'Y' && predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().charAt(0) != 'Z') {
//boolean areEqual = predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString().equals(rdfNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString());
String posOfPredicate = predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#POS")).getObject().toString();
String predicateSide = predicate.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString();
predicateSide = predicateSide.toLowerCase();
System.out.println(predicateSide);
String nodeSide = rdfNode.asResource().getProperty(org.apache.jena.rdf.model.ResourceFactory.createProperty("platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text")).getObject().toString();
nodeSide = nodeSide.toLowerCase();
System.out.println(nodeSide);
if(posOfPredicate.contains("VB")) {
System.out.println("I am in verb comparison");
System.out.println(extJWNLHandler.getSynonyms(POS.VERB, extJWNLHandler.getIndexWord("Verb", predicateSide)));
if(!extJWNLHandler.getSynonyms(POS.VERB, extJWNLHandler.getIndexWord("Verb", predicateSide)).contains(nodeSide)) {
System.out.println("The words are not similar at all");
return false;
}
}
else if(posOfPredicate.contains("NN")) {
System.out.println("I am in noun comparison");
System.out.println(extJWNLHandler.getSynonyms(POS.NOUN, extJWNLHandler.getIndexWord("Noun", predicateSide)));
if(!extJWNLHandler.getSynonyms(POS.NOUN, extJWNLHandler.getIndexWord("Noun", predicateSide)).contains(nodeSide)) {
System.out.println("The words are not similar at all");
return false;
}
}
else if(posOfPredicate.contains("JJ")) {
System.out.println("I am in adjective comparison");
System.out.println(extJWNLHandler.getSynonyms(POS.ADJECTIVE, extJWNLHandler.getIndexWord("Adjective", predicateSide)));
if(!extJWNLHandler.getSynonyms(POS.ADJECTIVE, extJWNLHandler.getIndexWord("Adjective", predicateSide)).contains(nodeSide)) {
System.out.println("The words are not similar at all");
return false;
}
}
else if(posOfPredicate.contains("RB")) {
System.out.println("I am in adverb comparison");
System.out.println(extJWNLHandler.getSynonyms(POS.ADVERB, extJWNLHandler.getIndexWord("Adverb", predicateSide)));
if(!extJWNLHandler.getSynonyms(POS.ADVERB, extJWNLHandler.getIndexWord("Adverb", predicateSide)).contains(nodeSide)) {
System.out.println("The words are not similar at all");
return false;
}
}
}
}
}
StmtIterator statements = predicate.asResource().listProperties();
while(statements.hasNext()) {
Statement tempStatement = statements.next();
System.out.println("------ Statement I encountered is "+tempStatement);
Property property = tempStatement.getPredicate();
System.out.println("------ The property I am testing tempnode for is, "+property);
if(rdfNode.asResource().hasProperty(property)) {
try {
System.out.println("RdfNode property : "+property.toString());
toReturn = toReturn && compareRDFNodes(predicate.asResource().getProperty(property).getObject(), rdfNode.asResource().getProperty(property).getObject());
}
catch(Exception e) {
System.out.println("----------- Exception occured");
}
}
else {
System.out.println("Returning because of absence of property");
return false;
}
}
}
System.out.println("Returning in general");
return toReturn;
}
private ArrayList<Integer> getAllQuestionsWithVariable(char c) {
// TODO Auto-generated method stub
ArrayList<Integer> integers = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader = Files.newBufferedReader(questionPath);
}
catch(IOException io) {
System.out.println("IO exception "+io.getMessage());
}
int counter = 1;
while(true) {
try {
String line = bufferedReader.readLine();
StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
while(stringTokenizer.hasMoreTokens()) {
String token = stringTokenizer.nextToken();
if(token.charAt(0) == c)
integers.add(counter);
}
counter++;
}
catch(Exception e) {
break;
}
}
return integers;
}
public void runQuestionProcessor() throws JWNLException, IOException {
//get Predicate from question base
// 1. We get the predicate
// 2. We get all the sentences which have same/similar predicate
// 3. We go through the dependencies of possible question and sentence
// 4.
BufferedReader bufferedReader = null;
File file = new File("C:/Users/Sadhana/workspace/System/src/fyp/referenceText.txt");
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
file = new File("C:/Users/Sadhana/workspace/System/src/fyp/answerFile.txt");
writer = new PrintWriter(file);
writer.print("");
writer.close();
char c = 'W';
for(int i = 0;i < 3;i++) {
Set<Set<String>> sets = new HashSet<>();
Set<String> answers = new HashSet<>();
c = (char) (c + 1);
ArrayList<Integer> integers = new ArrayList();
integers = getAllQuestionsWithVariable(c);
System.out.println("Processing answers for "+c+" the sentences in which it appears is "+integers);
Iterator<Integer> iterator = integers.iterator();
while(iterator.hasNext()) {
try {
int counter = iterator.next();
String line = null;
try {
bufferedReader = Files.newBufferedReader(questionPath);
}
catch(IOException io) {
System.out.println("IO exception "+io.getMessage());
}
for(int j = 0;j < counter;j++) {
line = bufferedReader.readLine();
}
if(line!=null) {
System.out.println("Question "+counter+" : "+line);
Resource sentence = questionBase.ontModel.getBaseModel().getResource(source+"#"+counter);
Resource predicate = getResourceWithProperty(sentence, "platform:/resource/JenaTrial/src/EnOntology.owl#Predicate");
String wordtext = getLiteralWithProperty(predicate, "platform:/resource/JenaTrial/src/EnOntology.owl#Word_Text");
//System.out.println(wordtext);
System.out.println("I am running question processor now!");
System.out.println("Searching for : "+wordtext);
Set<RDFNode> rdfNodes = returnPossiblePredicates(wordtext);
System.out.println("possible predicates are "+rdfNodes);
Set<String> possibleSolutions = getPossibleSolutions(rdfNodes, predicate, c);
//System.out.println(solutions);
sets.add(possibleSolutions);
}
counter++;
}
catch(NullPointerException nullPointerException) {
//System.out.println("Breaking at "+counter);
break;
}
System.out.println(sets);
answers = new HashSet<>();
Iterator<Set<String>> setIterstor = sets.iterator();
answers = setIterstor.next();
while(setIterstor.hasNext()) {
answers.retainAll(setIterstor.next());
}
System.out.println("Intersection : "+answers);
System.out.println("---------------------------------------------------------------------------");
}
System.out.println("Actual answer : "+answers);
Iterator<String> siterator = answers.iterator();
Charset charset = Charset.forName("US-ASCII");
try(FileWriter writeToAnswer = new FileWriter("C:/Users/Sadhana/workspace/System/src/fyp/answerFile.txt", true)) {
writeToAnswer.write(new String(c+" "));
while(siterator.hasNext()) {
String nextAns = siterator.next();
if(nextAns.isEmpty())
writeToAnswer.write("--- No answer ---");
else
writeToAnswer.write(nextAns+" ");
}
writeToAnswer.write(new String("\n"));
}
catch(Exception e) {
System.out.println("Writing to ans failed");
}
System.out.println("---------------------------------------------------------------------------");
}
//Charset charset = Charset.forName("US-ASCII");
/**try (BufferedReader reader = Files.newBufferedReader(questionPath, charset);
BufferedWriter writer = Files.newBufferedWriter(answerPath, charset);)
{
String line = null;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
processQuestion(new Annotation(line), stanfordHandler.stanfordCoreNLP, knowledgeBase.ontModel, line);
}
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}**/
}
public void processQuestion(Annotation annotation, StanfordCoreNLP stanfordCoreNlp, OntModel ontModel, String question) throws JWNLException {
stanfordCoreNlp.annotate(annotation);
List<CoreMap> coreMapList = (List<CoreMap>) annotation.get(CoreAnnotations.SentencesAnnotation.class);
Iterator<CoreMap> coreMapIterator = coreMapList.iterator();
SemanticGraph semanticGraph;
IndexedWord indexedWord;
while(coreMapIterator.hasNext()) {
CoreMap sentence = coreMapIterator.next();
System.out.println(sentence.get(CoreAnnotations.TextAnnotation.class));
semanticGraph = sentence.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);
indexedWord = semanticGraph.getFirstRoot();
//semanticGraph.prettyPrint();
formulateQuestion(semanticGraph, indexedWord, ontModel);
}
}
public void formulateQuestion(SemanticGraph semanticGraph, IndexedWord indexedWord, OntModel ontModel) throws JWNLException {
String subject, object, predicate, preposition, preposition1, unknown, POSAnnotation, query;
String ns = source + "#";
IndexedWord child;
unknown = new String();
predicate = object = subject = query = preposition = preposition1 = new String();
POSAnnotation = indexedWord.get(CoreAnnotations.PartOfSpeechAnnotation.class);
if(!(POSAnnotation.contains("WDT") || POSAnnotation.contains("WP") || POSAnnotation.contains("WRB"))) {
predicate = indexedWord.get(CoreAnnotations.LemmaAnnotation.class).toUpperCase().trim();
//System.out.println(predicate);
Iterator<GrammaticalRelation> gIterator = semanticGraph.childRelns(indexedWord).iterator();
while(gIterator.hasNext())
{
GrammaticalRelation temp = gIterator.next();
if(temp.toString().contains("nmod")) {
preposition = semanticGraph.getChildWithReln(indexedWord, temp).get(CoreAnnotations.TextAnnotation.class).toLowerCase();
//System.out.println(preposition);
}
}
}
else
unknown = "PREDICATE";
Iterator<GrammaticalRelation> grammaticalRelationIterator = semanticGraph.childRelns(indexedWord).iterator();
while(grammaticalRelationIterator.hasNext()) {
GrammaticalRelation temp = grammaticalRelationIterator.next();
child = semanticGraph.getChildWithReln(indexedWord, temp);
if (temp.toString().contains("nsubj")) {
POSAnnotation = child.get(CoreAnnotations.PartOfSpeechAnnotation.class);
if(!(POSAnnotation.contains("WDT") || POSAnnotation.contains("WP") || POSAnnotation.contains("WRB"))) {
subject = child.get(CoreAnnotations.TextAnnotation.class).toUpperCase().trim();
}
else
unknown = new String("SUBJECT");
}
else if(temp.toString().contains("dobj")) {
POSAnnotation = child.get(CoreAnnotations.PartOfSpeechAnnotation.class);
if(!(POSAnnotation.contains("WDT") || POSAnnotation.contains("WP") || POSAnnotation.contains("WRB")))
object = child.get(CoreAnnotations.TextAnnotation.class).toUpperCase().trim();
else
unknown = new String("OBJECT");
}
}
if(unknown.equals("SUBJECT")) {
System.out.println("Processing Subject");
Set<String> possiblePredicates = extJWNLHandler.getSynonyms(POS.VERB, extJWNLHandler.getIndexWord("Verb", predicate));
System.out.println(possiblePredicates);
Iterator<String> sIterator = possiblePredicates.iterator();
while(sIterator.hasNext()) {
String nextPredicate = sIterator.next().trim().toUpperCase();
query = "SELECT ?x ?adjx ?sentenceText WHERE { ?predicate <"+ns.concat("Word_Text")+"> \""+nextPredicate+"\" .?object <"+ns.concat("Word_Text")+"> \""+object+"\" . ?sentence <"+ns.concat("Predicate")+"> ?predicate . ?predicate <"+ns.concat("Object")+"> ?object. ?sentence <"+ns.concat("Sentence_Text")+"> ?sentenceText .?sentence <"+ns.concat("Subject")+"> ?wanted . ?wanted <"+ns.concat("Word_Text")+"> ?x . OPTIONAL { ?wanted <"+ns.concat("Adjective")+"> ?adjwantex . ?adjwanted <"+ns.concat("Word_Text")+"> ?adjx .} . OPTIONAL { ?wanted <"+ns.concat("Conjugate")+"> ?conjugate . ?conjugate <"+ns.concat("Word_Text")+"> ?conjx .}}";
executeQuery(ontModel, query);
}
//System.out.println(query);
//if(!executeQuery(ontModel, query)) {
//query = "SELECT ?x ?adjx ?sentenceText WHERE { ?predicate <"+ns.concat("Word_Text")+"> \""+predicate+"\" . ?sentence <"+ns.concat("Predicate")+"> ?predicate . ?sentence <"+ns.concat("Sentence_Text")+"> ?sentenceText .?sentence <"+ns.concat("Subject")+"> ?wanted . ?wanted <"+ns.concat("Word_Text")+"> ?x . OPTIONAL { ?wanted <"+ns.concat("Adjective")+"> ?adjwanted . ?adjwanted <"+ns.concat("Word_Text")+"> ?adjx .} . OPTIONAL { ?wanted <"+ns.concat("Conjugate")+"> ?conjugate . ?conjugate <"+ns.concat("Word_Text")+"> ?conjx .}}";
//executeQuery(ontModel, query);
//}
}
else if(unknown.equals("OBJECT")) {
System.out.println("Processing Object");
Set<String> possiblePredicates = extJWNLHandler.getSynonyms(POS.VERB, extJWNLHandler.getIndexWord("Verb", predicate));
System.out.println(possiblePredicates);
Iterator<String> sIterator = possiblePredicates.iterator();
while(sIterator.hasNext()) {
String nextPredicate = sIterator.next().trim().toUpperCase();
query = "SELECT ?adjx ?sentenceText ?x WHERE { ?predicate <"+ns.concat("Word_Text")+"> \""+nextPredicate+"\" .?subject <"+ns.concat("Word_Text")+"> \""+subject+"\" . ?sentence <"+ns.concat("Predicate")+"> ?predicate . ?predicate <"+ns.concat("Subject")+"> ?subject . ?sentence <"+ns.concat("Sentence_Text")+"> ?sentenceText . ?predicate <"+ns.concat("Object")+"> ?wanted . ?wanted <"+ns.concat("Word_Clause")+"> ?x . OPTIONAL { ?wanted <"+ns.concat("Adjective")+"> ?adjwanted . ?adjwanted <"+ns.concat("Word_Text")+"> ?adjx .} . OPTIONAL { ?wanted <"+ns.concat("Conjugate")+"> ?conjugate . ?conjugate <"+ns.concat("Word_Text")+"> ?conjx .}}";
executeQuery(ontModel, query);
}
//query = "SELECT ?adjx ?sentenceText ?x WHERE { ?predicate <"+ns.concat("Word_Text")+"> \""+predicate+"\" .?subject <"+ns.concat("Word_Text")+"> \""+subject+"\" . ?sentence <"+ns.concat("Predicate")+"> ?predicate . ?predicate <"+ns.concat("Subject")+"> ?subject . ?sentence <"+ns.concat("Sentence_Text")+"> ?sentenceText . ?predicate <"+ns.concat("Object")+"> ?wanted . ?wanted <"+ns.concat("Word_Clause")+"> ?x . OPTIONAL { ?wanted <"+ns.concat("Adjective")+"> ?adjwanted . ?adjwanted <"+ns.concat("Word_Text")+"> ?adjx .} . OPTIONAL { ?wanted <"+ns.concat("Conjugate")+"> ?conjugate . ?conjugate <"+ns.concat("Word_Text")+"> ?conjx .}}";
//executeQuery(ontModel, query);
}
else if(unknown.equals("PREDICATE")) {
System.out.println("Processing Predicate");
Set<String> possiblePredicates = new HashSet<>();
possiblePredicates.add(subject);
try {
possiblePredicates.addAll(extJWNLHandler.getSynonyms(POS.NOUN, extJWNLHandler.getIndexWord("Noun", subject)));
}
catch(Exception e) {
}
System.out.println(possiblePredicates);
Iterator<String> sIterator = possiblePredicates.iterator();
while(sIterator.hasNext()) {
String nextPredicate = sIterator.next().trim().toUpperCase();
query = "SELECT ?x ?adjx ?sentenceText WHERE { ?subject <"+ns.concat("Word_Text")+"> \""+nextPredicate+"\" . ?predicate <"+ns.concat("Subject")+"> ?subject. ?sentence <"+ns.concat("Predicate")+"> ?predicate. ?sentence <"+ns.concat("Sentence_Text")+"> ?sentenceText. ?predicate <"+ns.concat("Word_Clause")+"> ?x .}";//?sentence <"+ns.concat("Subject")+"> ?subject. ?sentence <"+ns.concat("Sentence_Text")+"> ?sentenceText .?sentence <"+ns.concat("Predicate")+"> ?wanted . ?wanted <"+ns.concat("Word_Text")+"> ?x . ?wanted <"+ns.concat("Adjective")+"> ?adjwanted . ?adjwanted <"+ns.concat("Word_Text")+"> ?adjx .} LIMIT 1";
executeQuery(ontModel, query);
}
}
//System.out.println(query);
}
public boolean executeQuery(OntModel model, String query) {
QuerySolution soln;
Set<String> solutions = new HashSet<>();
Set<String> possibleReference = new HashSet<>();
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
ResultSet results = qexec.execSelect();
if(!results.hasNext()) {
//System.out.println("No solution found");
return false;
}
while(results.hasNext()) {
String solutionString = new String(), sentenceString = new String();
soln = results.next();
//if(soln.get("?adjx") != null)
// solutionString = solutionString.concat(soln.get("?adjx")+" ");
solutionString = solutionString.concat(soln.get("?x") + " ");
if(soln.get("?conjx") != null)
System.out.print(soln.get("?conjx") + " ");
solutions.add(solutionString);
possibleReference.add(soln.get("?sentenceText").toString());
}
System.out.print("Solution = ");
System.out.println(solutions);
System.out.print("Posiible reference = ");
System.out.println(possibleReference);
System.out.println("-------------------------------------------------");
}
return true;
}
}