Search in sources :

Example 96 with Matcher

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;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) AnswerType(info.ephyra.questionanalysis.atype.AnswerType) HashSet(java.util.HashSet)

Example 97 with Matcher

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 98 with Matcher

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 99 with Matcher

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 100 with Matcher

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;
}
Also used : Matcher(java.util.regex.Matcher)

Aggregations

Matcher (java.util.regex.Matcher)12473 Pattern (java.util.regex.Pattern)5010 ArrayList (java.util.ArrayList)1516 IOException (java.io.IOException)904 HashMap (java.util.HashMap)565 File (java.io.File)487 Test (org.junit.Test)442 BufferedReader (java.io.BufferedReader)428 Map (java.util.Map)363 List (java.util.List)287 InputStreamReader (java.io.InputStreamReader)266 HashSet (java.util.HashSet)236 MalformedURLException (java.net.MalformedURLException)163 URL (java.net.URL)155 Date (java.util.Date)152 InputStream (java.io.InputStream)147 Field (java.lang.reflect.Field)130 PatternSyntaxException (java.util.regex.PatternSyntaxException)128 ParseException (java.text.ParseException)127 LinkedHashMap (java.util.LinkedHashMap)120