环境
Python3,
gensim,jieba,numpy ,pandas
原理:文章转成向量,然后在计算两个向量的余弦值。
Gensim
gensim是一个python的自然语言处理库,能够将文档根据TF-IDF, LDA, LSI 等模型转化成向量模式,gensim还实现了word2vec功能,以便进行进一步的处理。
中文分词
中文需要分词,英文就不需要了,分词用的 jieba 。
def segment(doc: str):
"""中文分词
Arguments:
doc {str} -- 输入文本
Returns:
[type] -- [description]
"""
# 停用词
stop_words = pd.read_csv("./data/stopwords_TUH.txt", index_col=False, quoting=3,
names=['stopword'],
sep="\n",
encoding='utf-8')
stop_words = list(stop_words.stopword)
# 去掉html标签数字等
reg_html = re.compile(r'<[^>]+>', re.S)
doc = reg_html.sub('', doc)
doc = re.sub('[0-9]', ''