QQ:66781877
最近的一个项目有个需求是这样的,需要从一篇英文文章或者一段英文文字中提取出其中的形容词(adjective),即对单词进行词性标注,我看到这个需求,第一反应是这肯定是个自然语言处理的问题。首先需要对句子进行分词,英文的分词是很容易的,但是分词之后,对于词性的判断,我最初的想法是,找一个英文字典,然后将单词输入,在字典中进行词性的查找。
先不说这种方法词性判断的准确率,从效率上来说,这样去对单词按词性分类,速度是很慢的。于是我继续查找资料,发现,对中文的词性判断,是有开源的解决方案的,如一些中文分词器:jcseg等等。但是对英文单词的词性标注,这些中文分词器是没有做到的,至少我目前没有找到。
既然中文分词器能对中文进行词性标注,那么英文分词器是否能对英文进行词性标注呢?很遗憾,我在lucene中,并没有找到相关的API,于是乎,我只能寻求其它解决办法。
进过一番努力,我从Stanford University的自然语言处理小组网站上找到了一个开源的英文词性标注工具,大喜过望,看了相关的介绍文档之后,运行了例子程序,发现分词的准确率是很高的,而且速度很快。
public class Tagger { public static void main(String[] args) throws Exception { String str = "The list of prisoners who may be released in coming days includes militants" + " who threw firebombs, in one case at a bus carrying children; stabbed and shot" + " civilians, including women, elderly Jews and suspected Palestinian collaborators; " + "and ambushed and killed border guards, police officers, security agents and soldiers. " + "All of them have been in prison for at least two decades; some were serving life sentences."; MaxentTagger tagger = new MaxentTagger("c:/wsj-0-18-bidirectional-nodistsim.tagger"); Long start = System.currentTimeMillis(); List<List<HasWord>> sentences = MaxentTagger.tokenizeText(new StringReader(str)); System.out.println("Tagging 用时"+(System.currentTimeMillis() - start)+"毫秒"); for (List<HasWord> sentence : sentences) { ArrayList<TaggedWord> tSentence = tagger.tagSentence(sentence); System.out.println(Sentence.listToString(tSentence, false)); } } }
Tagging 用时84毫秒 The/DT list/NN of/IN prisoners/NNS who/WP may/MD be/VB released/VBN in/IN coming/VBG days/NNS includes/VBZ militants/NNS who/WP threw/VBD firebombs/NNS ,/, in/IN one/CD case/NN at/IN a/DT bus/NN carrying/VBG children/NNS ;/: stabbed/VBN and/CC shot/VBN civilians/NNS ,/, including/VBG women/NNS ,/, elderly/JJ Jews/NNS and/CC suspected/JJ Palestinian/JJ collaborators/NNS ;/: and/CC ambushed/VBN and/CC killed/VBN border/NN guards/NNS ,/, police/NN officers/NNS ,/, security/NN agents/NNS and/CC soldiers/NNS ./.All/DT of/IN them/PRP have/VBP been/VBN in/IN prison/NN for/IN at/IN least/JJS two/CD decades/NNS ;/: some/DT were/VBD serving/VBG life/NN sentences/NNS ./.