Scala中的POS标记

yAs*_*AsH 11 nlp scala pos-tagger stanford-nlp

我尝试使用下面的斯坦福解析器在Scala中标记一个句子

val lp:LexicalizedParser = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
lp.setOptionFlags("-maxLength", "50", "-retainTmpSubcategories")
val s = "I love to play"
val parse :Tree =  lp.apply(s)
val taggedWords = parse.taggedYield()
println(taggedWords)
Run Code Online (Sandbox Code Playgroud)

我的错误类型不匹配; 发现:java.lang.String required:java.util.List [_ <:edu.stanford.nlp.ling.HasWord]在行中的val解析:Tree = lp.apply(s)

我不知道这是否是正确的做法.在Scala中有没有其他简单的方法来标记句子?

mcc*_*lum 12

您可能想要考虑FACTORIE工具包(http://github.com/factorie/factorie).它是机器学习和图形模型的通用库,恰好包括一套广泛的自然语言处理组件(标记化,标记规范化,形态分析,句子分割,词性标注,命名实体识别,依赖性解析,提及寻找,共同参与).

此外,它完全用Scala编写,并在Apache License下发布.

文档目前很少,但未来几个月会有所改善.

例如,一旦完成基于Maven的安装,您可以在命令行键入:

bin/fac nlp --pos1 --parser1 --ner1
Run Code Online (Sandbox Code Playgroud)

启动套接字监听多线程NLP服务器.然后通过将纯文本管道到其套接字号来查询它:

echo "Mr. Jones took a job at Google in New York.  He and his Australian wife moved from New South Wales on 4/1/12." | nc localhost 3228
Run Code Online (Sandbox Code Playgroud)

然后输出

1       1       Mr.             NNP     2       nn      O
2       2       Jones           NNP     3       nsubj   U-PER
3       3       took            VBD     0       root    O
4       4       a               DT      5       det     O
5       5       job             NN      3       dobj    O
6       6       at              IN      3       prep    O
7       7       Google          NNP     6       pobj    U-ORG
8       8       in              IN      7       prep    O
9       9       New             NNP     10      nn      B-LOC
10      10      York            NNP     8       pobj    L-LOC
11      11      .               .       3       punct   O

12      1       He              PRP     6       nsubj   O
13      2       and             CC      1       cc      O
14      3       his             PRP$    5       poss    O
15      4       Australian      JJ      5       amod    U-MISC
16      5       wife            NN      6       nsubj   O
17      6       moved           VBD     0       root    O
18      7       from            IN      6       prep    O
19      8       New             NNP     9       nn      B-LOC
20      9       South           NNP     10      nn      I-LOC
21      10      Wales           NNP     7       pobj    L-LOC
22      11      on              IN      6       prep    O
23      12      4/1/12          NNP     11      pobj    O
24      13      .               .       6       punct   O
Run Code Online (Sandbox Code Playgroud)

当然,所有这些功能都有一个编程API.

import cc.factorie._
import cc.factorie.app.nlp._
val doc = new Document("Education is the most powerful weapon which you can use to change the world.")
DocumentAnnotatorPipeline(pos.POS1).process(doc)
for (token <- doc.tokens)
  println("%-10s %-5s".format(token.string, token.posLabel.categoryValue))
Run Code Online (Sandbox Code Playgroud)

将输出:

Education  NN   
is         VBZ  
the        DT   
most       RBS  
powerful   JJ   
weapon     NN   
which      WDT  
you        PRP  
can        MD   
use        VB   
to         TO   
change     VB   
the        DT   
world      NN   
.          .    
Run Code Online (Sandbox Code Playgroud)


yAs*_*AsH 4

我发现了一种在 Scala 中进行 POS 标记的非常简单的方法

步骤1

从下面的链接下载 stanford tagger 版本 3.2.0

http://nlp.stanford.edu/software/stanford-postagger-2013-06-20.zip

第2步

将文件夹中存在的stanford-postagger jar添加到您的项目中,并将english-left3words-distsim.tagger文件放入项目中的 models 文件夹中

然后,使用下面的代码,您可以在 Scala 中对句子进行 pos 标记

              val tagger = new MaxentTagger(
                "english-left3words-distsim.tagger")
              val art_con = "My name is Rahul"
              val tagged = tagger.tagString(art_con)
              println(tagged)
Run Code Online (Sandbox Code Playgroud)

输出: My_PRP$ name_NN is_VBZ Rahul_NNP