use of java.util.regex.Matcher in project lucida by claritylab.
the class QuestionAnalysis method getAtypes.
private static String[] getAtypes(String question) {
List<AnswerType> atypes = new ArrayList<AnswerType>();
try {
atypes = qc.getAnswerTypes(question);
} catch (Exception e) {
e.printStackTrace();
}
Set<AnswerType> remove = new HashSet<AnswerType>();
for (AnswerType atype : atypes) {
if (atype.getFullType(-1).equals("NONE")) {
remove.add(atype);
}
}
for (AnswerType atype : remove) {
atypes.remove(atype);
}
String[] res = new String[atypes.size()];
for (int i = 0; i < atypes.size(); i++) {
String atype = atypes.get(i).getFullType(-1).toLowerCase().replaceAll("\\.", "->NE").replaceAll("^", "NE");
StringBuilder sb = new StringBuilder(atype);
Matcher m = Pattern.compile("_(\\w)").matcher(atype);
while (m.find()) {
sb.replace(m.start(), m.end(), m.group(1).toUpperCase());
m = Pattern.compile("_(\\w)").matcher(sb.toString());
}
res[i] = sb.toString();
}
return res;
}
use of java.util.regex.Matcher in project lucida by claritylab.
the class QuestionNormalizer method handleAuxDid.
/**
* <p>Modifies the question string by applying the following rule:</p>
*
* <p><code>did [...] infinitive -> simple_past</code></p>
*
* @param question question string
* @param tagged tagged question
* @return modified question strings
*/
private static String[] handleAuxDid(String question, String tagged) {
Pattern p = Pattern.compile("(?i)(.* )?did/.*? (\\S*)/vb(\\W.*)?");
Matcher m = p.matcher(tagged);
String[] results;
if (m.matches()) {
String verb = m.group(2);
results = VerbFormConverter.infinitiveToSimplePast(verb);
for (int i = 0; i < results.length; i++) {
results[i] = question.replace(verb, results[i]);
results[i] = results[i].replace("did ", "");
}
return results;
}
return null;
}
use of java.util.regex.Matcher in project lucida by claritylab.
the class QuestionNormalizer method replaceShortForms.
/**
* Replaces short forms of "is" and "are" that occur in combination with
* interrogatives.
*
* @param question the question string
* @return modified question string
*/
private static String replaceShortForms(String question) {
// only replace occurences of "'s" and "'re" in combination with
// interrogatives
Pattern p = Pattern.compile("(?i)(how|what|which|when|where|who|why)'" + "(s|re)");
Matcher m = p.matcher(question);
if (m.find()) {
String original = m.group();
String replaced = original.replace("'s", " is");
replaced = replaced.replace("'re", " are");
return question.replace(original, replaced);
}
// no such short forms in the question
return question;
}
use of java.util.regex.Matcher in project lucida by claritylab.
the class QuestionNormalizer method handleAuxHasHad.
/**
* <p>Modifies the question string by applying the following rule:</p>
*
* <p><code>have/has/had [...] past_participle -> has/have/had
* past_participle / simple_past</code></p>
*
* @param question question string
* @param tagged tagged question
* @return modified question strings
*/
private static String[] handleAuxHasHad(String question, String tagged) {
Pattern p = Pattern.compile("(?i)(.* )?(has|have|had)/.*? " + "(\\S*)/vbn.*");
Matcher m = p.matcher(tagged);
String[] results;
if (m.matches()) {
String aux = m.group(2);
String verb = m.group(3);
String[] sp = VerbFormConverter.pastParticipleToSimplePast(verb);
results = new String[sp.length + 1];
results[0] = question.replaceFirst(verb, aux + " " + verb);
results[0] = results[0].replaceFirst(aux + " ", "");
for (int i = 0; i < sp.length; i++) {
results[i + 1] = question.replaceFirst(verb, sp[i]);
results[i + 1] = results[i + 1].replaceFirst(aux + " ", "");
}
return results;
}
return null;
}
use of java.util.regex.Matcher in project lucida by claritylab.
the class QuestionNormalizer method transformList.
/**
* Replaces certain expressions in a list question to transform it into a
* factoid question.
*
* @param question a list question
* @return transformed question
*/
public static String transformList(String question) {
question = normalize(question);
String listPattern = ("(?i)^") + "(name|(what|which|who)( (is|are|was|were))?|list|give|provide|identify) " + "((a list of )?((the )?names of )?(all|every|a few|more|(the )?other|(the )?several|some( of)?|(the )?various) )?";
Matcher m = Pattern.compile(listPattern).matcher(question);
if (m.find()) {
String match = m.group(0);
String rep = m.group(1);
if (rep.matches("(?i)(list|give|provide|identify)"))
rep = "name";
question = question.replaceFirst(match, rep + " ");
}
return question;
}
Aggregations