赞
踩
一、什么是停用词?
在汉语中,有一类没有实际意义的词语,比如组词“的”,连词“以及”,副词“甚至”,语气词“吧”,被称为停用词。一个句子去掉这些停用词,并不影响理解。所以,进行自然语言处理时,一般将停用词过滤掉。一般词表文件中每一行存储一个停用词,行数就是停用词个数。目前一些业界公开的中文停用词表如下表所示。当然,也可以根据任务的需求完全可以自我定制停用词表。
词表名 | 词表文件 | 地址 |
四川大学机器智能实验室停用词库 | scu_stopwords.txt | GitHub - goto456/stopwords: 中文常用停用词表(哈工大停用词表、百度停用词表等) |
中文停用词表 | cn_stopwords.txt | GitHub - goto456/stopwords: 中文常用停用词表(哈工大停用词表、百度停用词表等) |
哈工大停用词表 | hit_stopwords.txt | GitHub - goto456/stopwords: 中文常用停用词表(哈工大停用词表、百度停用词表等) |
百度停用词表 | baidu_stopwords.txt | GitHub - goto456/stopwords: 中文常用停用词表(哈工大停用词表、百度停用词表等) |
二、停用词怎么构建?
一般可以把TF-IDF非常低的K个词语找出来,就能构成一个初步的停用词表。
TF-IDF(term frequency-inverse document frequency,词频-逆向文件频率)算法是一种用于信息检索与文本数据挖掘的常用加权技术。它用统计学方法评估一个词对某篇文章的重要程度,常用来提取文章的关键词。
TF-IDF算法的核心思想就是通过统计的方法,评估一个词对一个文件集或者语料库的重要程度。一个词的重要程度与它在文章中出现的次数成正比,跟它在语料库出现的次数成反比。
TF:词频,TF=某词在某文档中出现的次数 (ps:也有TF=某次在某 文档中出现的次数/该文档的总词量 这种计算,但Sklearn是采用直接计算次数。)
也就是说,就一篇文章局部来看,一个单词出现的次数越多越重要,但这并不是绝对的。比如,a, the, of等单词出现的次数一定不会少,但是它们并没有什么重要信息,所以,接下来引入IDF.
IDF:逆文本频率
指的是某个词在一个文件集或者语料库中区分力指标。计算公式为:
其中,Nd是训练集文档总数量,df(d,t)是包含某个单词的文档数量, +1的原因是避免分母为0.
也就是说,对一个文件集或者语料库而言,包含某个单词的文档越少,IDF的值越大,这个词的区分力越强,就越重要。
TF-IDF=TF*IDF
用sklearn实现,代码如下:
- from sklearn.feature_extraction.text import TfidfVectorizer
-
- corpus = [
- "what is the weather like today",
- "what is for dinner tonight",
- "this is a question worth pondering",
- "it is a beautiful day today"
- ]
-
- tfidf_vec = TfidfVectorizer()
-
- tfidf_matrix = tfidf_vec.fit_transform(corpus)
-
- print(tfidf_vec.get_feature_names_out())# 利用get_feature_names得到不重复的单词
-
- print(tfidf_vec.vocabulary_)#得到每个单词对应的ID
- print(tfidf_matrix)#输出TF-IDF矩阵
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
得到的结果如下:
- ['beautiful' 'day' 'dinner' 'for' 'is' 'it' 'like' 'pondering' 'question'
- 'the' 'this' 'today' 'tonight' 'weather' 'what' 'worth']
- {'what': 14, 'is': 4, 'the': 9, 'weather': 13, 'like': 6, 'today': 11, 'for': 3, 'dinner': 2, 'tonight': 12, 'this': 10, 'question': 8, 'worth': 15, 'pondering': 7, 'it': 5, 'beautiful': 0, 'day': 1}
- (0, 11) 0.3710221459250386
- (0, 6) 0.47059454669821993
- (0, 13) 0.47059454669821993
- (0, 9) 0.47059454669821993
- (0, 4) 0.24557575678403082
- (0, 14) 0.3710221459250386
- (1, 12) 0.506765426545092
- (1, 2) 0.506765426545092
- (1, 3) 0.506765426545092
- (1, 4) 0.2644512224141842
- (1, 14) 0.3995396830595886
- (2, 7) 0.4838025881780501
- (2, 15) 0.4838025881780501
- (2, 8) 0.4838025881780501
- (2, 10) 0.4838025881780501
- (2, 4) 0.25246826075544676
- (3, 1) 0.506765426545092
- (3, 0) 0.506765426545092
- (3, 5) 0.506765426545092
- (3, 11) 0.3995396830595886
- (3, 4) 0.2644512224141842
-
- Process finished with exit code 0
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。