StanfordCoreNLP 运行缓慢(python)
使用python启动NLP服务器
comments = open('../word2vec_train/comment_all.txt', 'r', encoding='utf-8').readlines()
nlp = StanfordCoreNLP('G:/stanford-corenlp-full-2018-10-05', lang='zh')
tokens = []
tags = []
ii = 0
pbar = ProgressBar(widgets=['Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer(),
' ', ETA(), ' ', FileTransferSpeed()],
maxval=len(comments)).start()
for com in comments:
tk = nlp.word_tokenize(com)
dependency = nlp.dependency_parse(com)
pos = nlp.pos_tag(com)
tokens.append(tk)
tags.append(near_nsubj_get(tk, dependency, pos))
ii += 1
pbar.update(ii)
nlp.close()
按理说nlp只会在第一次使用加载时耗费大量时间,多次调用不应该花太多时间
开启nlp服务器Debug, 发现大量等待发生在创建服务器连接时
查看 corenlp.py 创建连接使用的是域名 http://localhost 直接改为IP地址 http://127.0.0.1
109行左右
self.url = 'http://127.0.0.1:' + str(self.port)
问题解决