我正在使用ITest
在Stanford POS Tagger
中使用NLTK 3.2.4
使用Python 3.6
阿拉伯语文本,我发现了一个代码来源,但我对其中的大多数都不了解,因为我对{{{{{ 1}}。
代码来源:
Stanford POS Tagger
我得到的错误:
import os
java_path = "C:\\Program Files (x86)\\Java\\jdk1.8.0_112\\bin\\java.exe"
os.environ['JAVAHOME'] = java_path
from nltk.tag.stanford import StanfordPOSTagger as POS_Tag
home = 'E:\\Asmaa\\TP python\\'
_path_to_model = home + 'stanford-arabic-corenlp-2017-06-09-models.jar'
_path_to_jar = home + 'stanford-postagger.jar'
POS_Tag.java_options='-mx4096m'
st = POS_Tag(model_filename=_path_to_model, path_to_jar=_path_to_jar)
sentence = '.شرب القط الحليب اللذيذ'
st.tag(sentence.split())
那么出了什么问题?
答案 0 :(得分:2)
首先升级nltk
,然后升级终端或命令提示符:
wget http://nlp.stanford.edu/software/stanford-corenlp-full-2016-10-31.zip
unzip stanford-corenlp-full-2016-10-31.zip && cd stanford-corenlp-full-2016-10-31
wget http://nlp.stanford.edu/software/stanford-arabic-corenlp-2016-10-31-models.jar
wget https://raw.githubusercontent.com/stanfordnlp/CoreNLP/master/src/edu/stanford/nlp/pipeline/StanfordCoreNLP-arabic.properties
java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer \
-serverProperties StanfordCoreNLP-arabic.properties \
-preload tokenize,ssplit,pos,parse \
-status_port 9005 -port 9005 -timeout 15000
在Python中:
>>> from nltk.tag.stanford import CoreNLPPOSTagger
>>> from nltk.tokenize.stanford import CoreNLPTokenizer
>>> sttok = CoreNLPTokenizer('http://localhost:9005')
>>> stpos = CoreNLPPOSTagger('http://localhost:9005')
>>> text = u'انا حامل'
>>> stpos.tag(sttok.tokenize(text))
[('انا', 'DET'), ('حامل', 'NC')]
答案 1 :(得分:1)
我在this web page找到了解决方案,这是在阿拉伯语模型中。
正确的代码:
import os
java_path = "C:\\Program Files (x86)\\Java\\jdk1.8.0_112\\bin\\java.exe"
os.environ['JAVAHOME'] = java_path
from nltk.tag.stanford import StanfordPOSTagger as POS_Tag
arabic_postagger = POS_Tag('models/arabic.tagger', 'stanford-postagger.jar')
sentence = '.شرب القط الحليب اللذيذ'
print(arabic_postagger.tag(sentence.split()))
结果:
[('','شرب/ VBD'),('','القط/ DTNN'),('','الحليب/ DTNN'),('','اللذيذ/ DTJJ')]