全体stopwords列表 http://www.ranks.nl/stopwords
Word2Vect 实例:
https://www.zybuluo.com/hanxiaoyang/note/472184
Kaggle项目:https://www.kaggle.com/c/word2vec-nlp-tutorial
Bag-of-words 模型
之前教研室有个小伙伴在做文本方面的东西,经常提及词袋模型,只知道是文本表示的一种,可是最近看的关于CV的论文中也出现BoW模型,就很好奇BoW到底是个什么东西。
BoW起始可以理解为一种直方图统计,开始是用于自然语言处理和信息检索中的一种简单的文档表示方法。 和histogram 类似,BoW也只是统计频率信息,并没有序列信息。而和histogram不同的是,histogram一般统计的某个区间的频数,BoW是选择words字典,然后统计字典中每个单词出现的次数。
比如下面两个文档
John likes to watch movies. Mary likes too.
John also likes to watch football games.
首先可以找出两篇文档中单词的并集,作为dictionary
{"John":1, 'likes':2, "to":3, 'watch':4, 'movies':5, 'also':6, 'football':7, 'games':8, 'Mary':9, 'too':10}
那么两篇文档统计出来的BoW 向量就是
[1,2,1,1,1,0,0,0,1,1]
[1,1,1,1,0,1,1,1,0,0]
实例
对影评数据做预处理,大概有以下环节:
- 去掉html标签
- 移除标点
- 切分成词/token
- 去掉停用词
- 重组为新的句子
- example = BeautifulSoup(raw_example, 'html.parser').get_text()
display(example, '去掉HTML标签的数据') - example_letters = re.sub('[^a-zA-Z]', ' ', example)
display(example_letters, '去掉标点的数据') - words = example_letters.lower().split()
display(words, '纯词列表数据') - from nltk.corpus import stopwords
# stopwords = {}.fromkeys([ line.rstrip() for line in open('../stopwords.txt')])
stopwords = stopwords.words("english")
words_nostop = [w for w in words if w not in stopwords]
display(words_nostop, '去掉停用词数据') - vectorizer = CountVectorizer(max_features = 5000)
train_data_features = vectorizer.fit_transform(df.clean_review).toarray()
train_data_features.shape
vectorizer.get_feature_names()