依旧是对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还有提升,毕竟具体预处理细节也不知道