# https://blog.csdn.net/a40850273/article/details/81119103
'''
词袋模型将一段文本作为一个个分离的词进行处理,通过不同类文本中可能出现词的差异对文本进行分类。必须指出的是对于文本分类,上下文对于其真正的类别有着强相关性。个人理解此类方法可能只是为了算法的简易性选择性地放弃了文本中的上下文信息,或者说只关注词频信息也能够获得符合要求的文本分类效果。
'''
# 词袋模型的三部曲:分词(tokenizing),统计修订词特征值(counting)与标准化(normalizing)。
## 统计词频TF-IDF
# 高频率出现的词不一定有用,如as、the,使用TF-IDF可以有效过滤常见的词语,保留重要的词语
# 直接使用 TfidfVectorizer 直接得到最后的结果
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [ 'This is the first document.',
'This is the second second document.',
'And the third one.',
'Is this the first document?',
]
tfidf_vectorizer = TfidfVectorizer()
tfidf = tfidf_vectorizer.fit_transform(corpus)
print('============== tfidf ===================')
print(tfidf)
'''
# Output
============== tfidf ===================
(0, 8) 0.438776742859
(0, 3) 0.438776742859
(0, 6) 0.358728738248
(0, 2) 0.541976569726
(0, 1) 0.438776742859
(1, 8) 0.272301467523
(1, 3) 0.272301467523
(1, 6) 0.222624292325
(1, 1) 0.272301467523
(1, 5) 0.853225736145
(2, 6) 0.28847674875
(2, 0) 0.552805319991
(2, 7) 0.552805319991
(2, 4) 0.552805319991
(3, 8) 0.438776742859
(3, 3) 0.438776742859
(3, 6) 0.358728738248
(3, 2) 0.541976569726
(3, 1) 0.438776742859
'''
# 向量化
from gensim.models import word2vec