【问题标题】:Extracting all words after specified words until end of sentence提取指定单词之后的所有单词直到句子结尾
【发布时间】:2014-07-21 10:48:29
【问题描述】:

我需要提取以下单词之后的所有单词,直到句子 (/[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  } 

【问题讨论】:

  • 预期输出是什么?
  • 预期的输出是。第一句话“每个校区都有一个俱乐部”,第二句话“每个项目都有唯一的代码、名称、级别和持续时间”。

标签: java regex stanford-nlp


【解决方案1】:

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

/someregex/

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

一旦你去掉那些斜线,修复你的正则表达式更改,删除不正确的字符类,以及其他一些调整,这个正则表达式应该可以工作了:

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

【讨论】:

  • 问题还是一样,能帮忙吗?
  • 也许显示样本输入和预期匹配 - 这会很有帮助
  • 如果输入是“请创建一个包含以下内容的数据库,每个员工都有姓名、地​​址和电话号码。”输出应该是“每个员工都有姓名、地​​址和电话号码。”
  • 那么,你想要从第一次出现“each”、“has”或“have”中的任何一个词到句子的下一个结尾的所有输入?
【解决方案2】:

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

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

作为 Java 字符串的正则表达式:

"(?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));
    }

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 2021-11-23
    • 2015-01-20
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多