Unigram 和bigram 对yelp数据集进行垃圾评论识别分类 python

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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值