Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DALAnalyzer.java #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 81 additions & 33 deletions src/edu/stanford/nlp/sempre/interactive/DALAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public LanguageInfo analyze(String utterance) {
separate = boundaryBefore || boundaryAfter;
else if (c == '=') // separate all >, =, < except >=, <=
separate = !(i - 1 >= 0) || ((utterance.charAt(i - 1) != '>' && utterance.charAt(i - 1) != '<'));
else if (c == '('||c == ')')
separate=true;
else if (c == '>' || c == '<')
separate = !(i + 1 < utterance.length())
|| ((utterance.charAt(i + 1) != '=' && utterance.charAt(i + 1) != '='));
Expand All @@ -73,49 +75,95 @@ else if (c == '>' || c == '<') {
} else if (c == '=') {
buf.append(c);
buf.append(' ');
} else
}
else
buf.append(c);
if (separate)
buf.append(' ');
}
utterance = buf.toString().trim();
if (!utterance.equals("")) {
String[] tokens = utterance.split("\\s+");
for (String token : tokens) {
String lemma = token;
if (token.endsWith("s") && token.length() > 1)
lemma = token.substring(0, token.length() - 1);

languageInfo.tokens.add(LanguageAnalyzer.opts.lowerCaseTokens ? token.toLowerCase() : token);
languageInfo.lemmaTokens.add(LanguageAnalyzer.opts.lowerCaseTokens ? lemma.toLowerCase() : lemma);

// Is it a written out number?
int x = Arrays.asList(numbers).indexOf(token);
if (x != -1) {
languageInfo.posTags.add("CD");
languageInfo.nerTags.add("NUMBER");
languageInfo.nerValues.add(x + "");
continue;
}

try {
Double.parseDouble(token);
languageInfo.posTags.add("CD");
languageInfo.nerTags.add("NUMBER");
languageInfo.nerValues.add(token);
} catch (NumberFormatException e) {
// Guess that capitalized nouns are proper
if (Character.isUpperCase(token.charAt(0)))
languageInfo.posTags.add("NNP");
else if (token.equals("'") || token.equals("\"") || token.equals("''") || token.equals("``"))
languageInfo.posTags.add("''");
else
int parenCount=0;//+1 for ( -1 for )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do this as a mode during tokenization, not post processing, that should make life much easier.

String tempToken="";
boolean parenBool=false;
for (String token : tokens){
parenBool=false;
String lemma = token;
if (token.endsWith("s") && token.length() > 1)
lemma = token.substring(0, token.length() - 1);
if(token.equals("(")){
parenCount++;
tempToken+="(";
parenBool=true;
}
if (token.equals(")")){
if (parenCount == 1){
tempToken=tempToken.substring(0,tempToken.length()-1);
tempToken+=")";
}
else{
tempToken+=")";
tempToken+=" ";
}
parenBool=true;
parenCount--;
}
if (parenCount==0){
if (parenBool){
languageInfo.tokens.add(LanguageAnalyzer.opts.lowerCaseTokens ? tempToken.toLowerCase() : tempToken);
languageInfo.lemmaTokens.add(LanguageAnalyzer.opts.lowerCaseTokens ? tempToken.toLowerCase() : tempToken);
languageInfo.nerTags.add("UNK");
languageInfo.nerValues.add("UNK");
languageInfo.posTags.add("UNK");
languageInfo.nerTags.add("UNK");
languageInfo.nerValues.add("UNK");
}
tempToken="";
}
else{
languageInfo.tokens.add(LanguageAnalyzer.opts.lowerCaseTokens ? token.toLowerCase() : token);
languageInfo.lemmaTokens.add(LanguageAnalyzer.opts.lowerCaseTokens ? lemma.toLowerCase() : lemma);
int x = Arrays.asList(numbers).indexOf(token);
if (x != -1) {
languageInfo.posTags.add("CD");
languageInfo.nerTags.add("NUMBER");
languageInfo.nerValues.add(x + "");
continue;
}
try {
Double.parseDouble(token);
languageInfo.posTags.add("CD");
languageInfo.nerTags.add("NUMBER");
languageInfo.nerValues.add(token);
} catch (NumberFormatException e) {
// Guess that capitalized nouns are proper
if (Character.isUpperCase(token.charAt(0)))
languageInfo.posTags.add("NNP");
else if (token.equals("'") || token.equals("\"") || token.equals("''") || token.equals("``"))
languageInfo.posTags.add("''");
else
languageInfo.posTags.add("UNK");
languageInfo.nerTags.add("UNK");
languageInfo.nerValues.add("UNK");
}
}
}
if (parenCount > 0 && !parenBool){
tempToken+=token;
tempToken+=" ";
}
if (parenCount < 0){
languageInfo.nerTags.add("SEXP");
throw new NumberFormatException();
}


}
if (parenCount!=0){
languageInfo.nerTags.add("SEXP");
throw new NumberFormatException();
}

}
return languageInfo;
}
}