最近有处理一批语句获取名词集合的需求,写了个小工具。
Stanford Parser导入
利用maven引入Stanford Parser的包,没有特殊需求的话直接使用里面训练完的各国语言文件即可,很方便。
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-parser</artifactId>
<version>3.9.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
<classifier>models</classifier>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-parser</artifactId>
<version>3.9.2</version>
<classifier>models</classifier>
</dependency>
Stanford Parser java实现使用了slf4j
进行输出提示管理,为了不报错把slf4j
也引入。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
语句处理
Stanford Parser有个Type Dependency
的概念,表示词之间和词本身的关系。每个词对应的td有对应的tag
,该属性表示了词汇在当前句法树中的词性。Stanford Parser定义了很多种词汇的性质,具体可参考Stanford Parser标记含义
这边我们按照需求,将tag为NN(常见名词)和NR(固有名词)的词汇进行分析输出。
获取模型
private String model="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
public LexicalizedParser createParser(){
LexicalizedParser lp = LexicalizedParser.loadModel(model);