I'm trying to write an NLP module for an application (need to convert it to Android later), users can input more than one sentence in the code, it will be converted to a prolog query which will be processed and returned as variables for the other modules. However, when I input more than 1 sentence (apart from the second query possibly failing because of lack of words in the lexicon) I get this error in Java:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at gapt.LanguageProcessor.Directions(LanguageProcesso r.java:269)
at gapt.LanguageProcessor.questionHandler(LanguagePro cessor.java:129)
at gapt.LanguageProcessor.queryDecoder(LanguageProces sor.java:96)
at gapt.LanguageProcessor.NLPquery(LanguageProcessor. java:80)
at gapt.GAPT.main(GAPT.java:11)
Java Result: 1
(supposedly it's easy to fix so I'm posting it in here, but the code itself is quite complex so I was confused as to whether or not it would count as an advanced problem)
Now, I've looked at other threads with the same problem on this forum and others and for the most part they say it's because the Scanner closed for the second input - but I didn't use the close() method on my Scanners in any part of the code so I'm lost
Java Code:
public void NLPquery(String x) //this method checks if the query posted by the user is valid
{
String[] y = x.split("[\n.] ");
for(int i=0;i<y.length;i++)
{
npt.add(y[i]); //each one a sentence
}
//each sentence a list
//for each, check number of words, make new list where each word is a new term
for(int i = 0; i < npt.size(); i++)
{
String[] z = npt.get(i).split(" ");
input.add(new ArrayList<>());
for(int j=0;j<z.length;j++)
{input.get(i).add(z[j]);}
String S = "[";
for (int j = 0; j<input.get(i).size();j++)
{
S+=input.get(i).get(j);
if(j<input.get(i).size()-1)
{
S+= ",";
}
}
S+= "]";
Query q2 = new Query("s(_,"+S+",[]).");
check.add(q2.hasSolution());
}
for(int i = 0; i<check.size();i++)
{
if(truth && check.get(i) == true)
{
truth = true;
}
else
{
truth = false; i=check.size();
}
}
if(truth == true)
{
//the query() method returns a boolean value based on the output of the query object instance created
// in this case, the JVM will be querying the GAPT.pl knowledge base if the input string is recognized by the grammar as a subset of "phr"
// in prolog query syntax, this would be: ?- phr(X,[]).
queryDecoder();
}
else response(false);
}
public void queryDecoder() //this method re-routes the program to the Handler of whatever kind of phrase was given by the user
{
for(int i = 0; i < input.size(); i++)
{
String[] S = new String[input.size()];
Query q2 = new Query(new Compound("s", new Term[]{new Variable("X"),Util.stringArrayToList(input.get(i).toArray(S)),new Atom("[]")}));
java.util.Hashtable solution;
solution = q2.oneSolution();
switch(solution.get("X").toString())
{
case "command": {commandHandler(i); break;}
case "query": {questionHandler(i); break;}
default: break;
}
}
}
public void questionHandler(int i)
{
//if map query pass control to map
//if calendar query, query the calendar database and update if necessary
//else query google
String[] Y = new String[input.size()];
Query q2 = new Query(new Compound("q", new Term[]{new Variable("X"),Util.stringArrayToList(input.get(i).toArray(Y)),new Atom("[]")}));
java.util.Hashtable solution = q2.oneSolution();
switch(solution.get("X").toString())
{
case "dir": {Directions(); break;}
case "time": {Calendar("Query",i); break;}
default: break;
}
}
public void Directions()
{
try ( //this method will create the parameters used in a map enquiry and call the method necessary
Scanner sc = new Scanner(System.in))
{
sc.useDelimiter("\n");
System.out.println("What is the name of the location you want to find?");
String name = sc.next();
Map(name);
}
}
No comments:
Post a Comment