NLP---NLTK学习和初识word2vec + kaggle项目Bag of Words Meets Bags of Popcorn(bag _of_words_model)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyf52010/article/details/84961532

全体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]

实例

对影评数据做预处理,大概有以下环节:

  1. 去掉html标签
  2. 移除标点
  3. 切分成词/token
  4. 去掉停用词
  5. 重组为新的句子
  6. example = BeautifulSoup(raw_example, 'html.parser').get_text()
    display(example, '去掉HTML标签的数据')
  7. example_letters = re.sub('[^a-zA-Z]', ' ', example)
    display(example_letters, '去掉标点的数据')
  8. words = example_letters.lower().split()
    display(words, '纯词列表数据')
  9. 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, '去掉停用词数据')
  10. vectorizer = CountVectorizer(max_features = 5000) 
    train_data_features = vectorizer.fit_transform(df.clean_review).toarray()
    train_data_features.shape
    vectorizer.get_feature_names()

猜你喜欢

转载自blog.csdn.net/lyf52010/article/details/84961532
今日推荐