1

我想计算文本中 pp/np/vp 的数量,但我不知道如何在 openNLP 分块器中识别 PP-tags/NP-tags/VP-tags?我已经尝试过这段代码,但它不起作用。

ChunkerModel cModel = new ChunkerModel(modelIn);
ChunkerME chunkerME = new ChunkerME(cModel);  
String result[] = chunkerME.chunk(whitespaceTokenizerLine, tags); 
HashMap<Integer,String> phraseLablesMap = new HashMap<Integer, String>();  
Integer wordCount = 1;  
Integer phLableCount = 0;  
for (String phLable : result) {  
    if(phLable.equals("O")) phLable += "-Punctuation"; //The phLable of the last word is OP  
    if(phLable.split("-")[0].equals("B")) phLableCount++;  
    phLable = phLable.split("-")[1] + phLableCount;  
    System.out.println(wordCount + ":" + phLable);  
    phraseLablesMap.put(wordCount, phLable);  
    wordCount++;  
}

Integer noPP=0;
Integer TotalPP=0;
for (String PPattach: result) {
    if (PPattach.equals("PP")) {
        for (int i=0;i<result.length;i++)
            TotalPP = noPP +1;
        }
    }
System.out.println(TotalPP); 

输出:

1:NP1
2:VP2
3:NP3
4:NP3
5:VP4
6:PP5
7:NP6
8:NP6
9:NP6
10:NP6
11:PP7
12:NP8
13:NP8
14:NP8
15:PP9
16:NP10
17:NP10
18:PP11
19:NP12
20:NP12
21:VP13
22:VP13
23:NP14
24:NP14
25:PP15
26:NP16
27:NP16
28:Punctuation16
0
4

1 回答 1

0

最好的方法是使用 span 对象,它们有一个返回块类型的 getType() 方法。

看到这个帖子

将文档中的所有命名实体分组

于 2014-02-05T00:00:45.410 回答