Unigram 和bigram 對yelp資料集進行垃圾評論識別分類 python
阿新 • • 發佈:2019-01-30
依舊是對yelp資料集處理,之前效果不理想,後來仔細看了論文,用的是SVMlight分類器…(使用方法見上一篇文章),效果就差不多了。。。。
過程就是對英文進行處理(去停用,去高頻和低頻),化為詞袋模型,處理成SVMlight的格式,進行分類。貼部分程式碼。
對資料處理:
for c in cos:
cis_2=[]
id = c.split(' ')[0]
content = c[len(id) + 2:-4]
s = nltk.stem.SnowballStemmer('english')
content = s.stem(content)
# 分割成句子、分割成單詞
sentences = nltk.sent_tokenize(content)
words = []
for sen in sentences:
words.extend(nltk.word_tokenize(sen))
# 去除停用詞
stopwords = nltk.corpus.stopwords.words('english')
filtered = [w for w in words if (w not in stopwords)]
#2-gram
for i in range(len(filtered)):
c=filtered[i]
cis_2.append(c)
for i in range(len(filtered) - 1):
c = filtered[i] + filtered[i + 1]
cis_2.append(c)
contents.append(cis_2)
形成詞典去除低頻和高頻
#去掉低頻詞高頻
d=defaultdict(int)
for m in contents:
for n in m:
d[n] +=1
print(d.items())
contents = [[token for token in text if 3000>d[token] >5 ]
for text in contents]
#形成字典
dictionary = corpora.Dictionary(contents)
print(len(dictionary))
#將文字轉化為詞袋模型的向量,返回的一個個二元組
#比如(0,2)代表第0個次出現了2次
corpus = [dictionary.doc2bow(text) for text in contents]
還可以,recall還有提升,畢竟具體預處理細節也不知道