所以我在使用斯坦福核心nlp库进行TokenRegex模式匹配的示例。需要帮助将规则文件放置在适当的位置。试图将其放置在src文件夹中,但没有用
代码:-
public static void main(String[] args) throws ClassNotFoundException
{
// set properties
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,tokensregex");
props.setProperty("tokensregex.rules", "basic_ner.rules");
props.setProperty("tokensregex.matchedExpressionsAnnotationKey","edu.stanford.nlp.examples.TokensRegexAnnotatorDemo$MyMatchedExpressionAnnotation");
// build pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
// annotate
Annotation ann = new Annotation("There will be a big announcement by Apple Inc today at 5:00pm. " +
"She has worked at Miller Corp. for 5 years.");
pipeline.annotate(ann);
// show results
System.out.println("---");
System.out.println("tokens\n");
for (CoreMap sentence : ann.get(CoreAnnotations.SentencesAnnotation.class))
{
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class))
{
System.out.println(token.word() + "\t" + token.ner());
}
System.out.println("");
}
System.out.println("---");
System.out.println("matched expressions\n");
for (CoreMap me : ann.get(MyMatchedExpressionAnnotation.class))
{
System.out.println(me);
}
}
}
答案 0 :(得分:0)
我认为,如果将文件basic_ner.rules
放在工作目录(不会是“ src”)中,那应该可以工作。您应该在Eclipse中找到默认的工作目录。我假设它只是整个项目的根目录。您也可以只放入规则文件的绝对路径。