提取指定单词后的所有单词直到句末

时间:2014-07-21 10:48:30

标签: java regex stanford-nlp

我需要提取以下单词之后的所有单词,直到句子结尾(/[Ee]ach+/) ([tag:NN]+|[tag:NNS]+) (/has+/|/have+/)但我在第13行收到错误,下面是我的代码:

 1  String file="Each campus has one club. Each programme has a unique code, title, level and   duration.";
 2  Properties props = new Properties();
 3  props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
 4  StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
 5  Annotation document = new Annotation(file);
 6  pipeline.annotate(document);
 7  List<CoreLabel> tokens = new ArrayList<CoreLabel>();

 8  List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
 9  for(CoreMap sentence: sentences) 
10  {
11      for (CoreLabel token: sentence.get(CoreAnnotations.TokensAnnotation.class)) 
12         tokens.add(token); 

13      TokenSequencePattern pattern = TokenSequencePattern.compile("(/[Ee]ach+/) ([tag:NN]+|[tag:NNS]+) (/has+/|/have+/) [A-Z]");
14      TokenSequenceMatcher matcher = pattern.getMatcher(tokens);
15      while( matcher.find()){
16          JOptionPane.showMessageDialog(rootPane, matcher.group()); 
17          String matched = matcher.group();
18      }
19      tokens.removeAll(tokens);
20  } 

2 个答案:

答案 0 :(得分:0)

我认为你的意思是这个正则表达式:

(?i)each[^.]+[.]

正则表达式为Java String:

"(?i)each[^.]+[.]"

使用它的Java代码:

String file = "Each campus has one club. Each programme has a unique code, title, level and   duration.";
    String pattern = "(?i)each[^.]+[.]";
    Pattern compile = Pattern.compile(pattern);
    Matcher matcher = compile.matcher(file);
    while (matcher.find()) {            
        JOptionPane.showMessageDialog(null, matcher.group(0));
    }

答案 1 :(得分:0)

您在许多语言的正则表达式周围看到的斜杠,例如

/someregex/

与正则表达式没有任何关系:斜杠是应用程序语言 artefact,而java不是使用它们的语言之一。

一旦你剥去这些斜线,修复你的正则表达式修改,并删除错误的用心角色类,以及其他一些调整,这个正则表达式应该有效:

([Ee]ach|tag:NNS?|ha(s|ve)) +\w+