本文介绍了如何从文本中获取NN和NNS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从下面的脚本中给出的示例文本中获取NN或NNS.为此,当我使用下面的代码时,输出为:
I want to get NN or NNS from a sample text as given within the script below. To this end, when I use the code below, the output is:
types
synchronization
phase
synchronization
-RSB-
synchronization
-LSB-
-RSB-
projection
synchronization
为什么我会得到[-RSB-]
或[-LSB-]
?我是否应该使用其他模式同时获取NN或NNS?
Here why am I getting [-RSB-]
or [-LSB-]
? Should I use a different pattern to get NN or NNS at the same time?
atic = "So far, many different types of synchronization have been investigated, such as complete synchronization [8], generalized synchronization [9], phase synchronization [10], lag synchronization [11], projection synchronization [12, 13], and so forth.";
Reader reader = new StringReader(atic);
DocumentPreprocessor dp = new DocumentPreprocessor(reader);
docs_terms_unq.put(rs.getString("u"), new ArrayList<String>());
docs_terms.put(rs.getString("u"), new ArrayList<String>());
for (List<HasWord> sentence : dp) {
List<TaggedWord> tagged = tagger.tagSentence(sentence);
GrammaticalStructure gs = parser.predict(tagged);
Tree x = parserr.parse(sentence);
System.out.println(x);
TregexPattern NPpattern = TregexPattern.compile("@NN|NNS");
TregexMatcher matcher = NPpattern.matcher(x);
while (matcher.findNextMatchingNode()) {
Tree match = matcher.getMatch();
ArrayList hh = match.yield();
Boolean b = false;
System.out.println(hh.toString());}
推荐答案
我不知道为什么会出现这些问题.但是,如果您使用语音标记器,您将获得更准确的POS标记.我建议直接看一下注解.这是一些示例代码.
I do not know why those are coming up. But you will get more accurate POS tags if you use the part of speech tagger. I would suggest just looking directly at the Annotation. Here is some sample code.
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;
import java.util.Properties;
public class NNExample {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "So far, many different types of synchronization have been investigated, such as complete " +
"synchronization [8], generalized synchronization [9], phase synchronization [10], " +
"lag synchronization [11], projection synchronization [12, 13], and so forth.";
Annotation annotation = new Annotation(text);
pipeline.annotate(annotation);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
String partOfSpeechTag = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
if (partOfSpeechTag.equals("NN") || partOfSpeechTag.equals("NNS")) {
System.out.println(token.word());
}
}
}
}
}
还有我得到的输出.
types
synchronization
synchronization
synchronization
phase
synchronization
lag
synchronization
projection
synchronization
这篇关于如何从文本中获取NN和NNS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!