標題
-
應用範圍和場景
在自然語言處理的應用中,經常會用到分詞,詞性分析、句法分析、英體識別等應用,斯坦福NLP在中文支持方面還算不錯,方便我們更快的支持和驗證應用的可行性。 -
環境配值和搭建過程
下載 stanford-corenlp-full-2017-06-09.zip ,然後在系統中解壓,下載該包支持的中文模型 stanford-chinese-corenlp-2017-06-09-models.jar , 注意名稱需要對應,不然容易出bug。將模型的解壓文件放到第一文包的解壓文件中,如下圖:
新建啓動腳本如上圖:注意指定中文的模型文件就可以了。
-
啓動效果
-
語言支持
在這裏開個頭,共朋友們參考使用。
class service_ana(object):
"""
對斯坦福服務的包裝a
"""
def __init__(self):
pass
#需要提前啓動服務
self.nlp = StanfordCoreNLP('http://localhost:9000')
def get_res(self, text):
"""參數是多個句子的列表(分詞和不分詞都行), 返回各個句子的分析結果"""
output = self.nlp.annotate(text, properties={
'annotators': 'tokenize,ssplit,pos,ner,depparse,parse',
'outputFormat': 'json'
})
print(output['sentences'][0]['parse'])
print(output['sentences'][0]['basicDependencies'])
print(output['sentences'][0]['enhancedDependencies'])
print(output['sentences'][0]['enhancedPlusPlusDependencies'])
print(output['sentences'][0]['tokens'])
- 結果分析和抽取:
{“intent_res”: “你喜歡蘋果嘛”, “zhu_yu”: “你”, “bin_yu”: “蘋果”, “wei_yu”: “喜歡”, “intent_index”: “你喜歡 喜歡蘋果 嘛喜歡”}
**tips:**通過這種分析句子成分的方法分析意圖適合場景相對集中並且比較精細的意圖,比較粗的意圖可以使用分類來實現,另外,涉及到句法解析,長句子也不太適合,速度較慢。