如何从包含特殊字符的句子中检索单词?

时间:2012-12-23 17:44:19

标签: java regex stanford-nlp

从下面给出的代码:: 我希望将关系提取为

amod delhi
nsubj capital delhi 
.....etc

[amod(delhi-2, -1), nsubj(capital-5, delhi-2), cop(capital-5, is-3),
det(capital-5, the-4), root(ROOT-0, capital-5), prep_of(capital-5,
india-7)]

1 个答案:

答案 0 :(得分:0)

如果您的输入看起来那样,那么您可以使用此

Pattern p = Pattern.compile("(\\w+)\\((\\w*)-\\d+,\\s(\\w*)-\\d+\\)");
//groups                      1       2              3
String data = "[amod(delhi-2, -1), nsubj(capital-5, delhi-2), " +
        "cop(capital-5, is-3), det(capital-5, the-4), root(ROOT-0, " +
        "capital-5), prep_of(capital-5, india-7)]";
Matcher m = p.matcher(data);
while (m.find()) 
    System.out.println(m.group(1) + " " + m.group(2) + " " + m.group(3));