
说明
1、Spacy语言模型包含一些强大的文本分析功能,如词性标记和命名实体识别。
2、导入spacy相关模块后,需要加载中文处理包。然后读小说数据,nlp处理天龙八部小说,包括分词、定量、词性标注、语法分析、命名实体识别,用符号/分隔小说。最后,通过is_stop函数判断单词中的单词是否为无效单词,删除无效单词后,将结果写入txt文件。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import spacy
import pandas as pd
import time
from spacy.lang.zh.stop_words import STOP_WORDS
nlp = spacy.load( 'zh_core_web_sm' )
def fenci_stopwords(data,newdata1):
fenci = []
qc_stopwords =[]
article = pd.read_table(data,encoding= "utf-8" )
start1 = time.time()
with open(newdata1, 'w' ,encoding= 'utf-8' ) as f1:
for i in article[ "天龙八部" ]: #分词
doc = nlp(i)
result1 = '/' .join([t.text for t in doc])
fenci.append(result1)
for j in fenci: #去除停用词
words = nlp.vocab[j]
if words.is_stop == False:
qc_stopwords.append(j)
result2 = '/' .join(qc_stopwords)
f1.write(result2)
end1 = time.time()
return end1-start1
|
以上就是Python使用Spacy进行分词的方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。