【问题标题】:to find the opinion of a sentence as positive or negative找出一个句子的意见是积极的还是消极的
【发布时间】:2016-03-01 11:52:07
【问题描述】:

我需要查找网站中给出的某些评论的意见。我为此使用 sentiwordnet。我首先将包含所有评论的文件发送到 POS Tagger。

tokens=nltk.word_tokenize(line) #tokenization for line in file
tagged=nltk.pos_tag(tokens) #for POSTagging
print tagged

除了将其视为 2 个单独的单词之外,是否还有其他准确的标记方法将其视为 1 个单词。

现在我必须给标记化的单词打正分和负分,然后计算总分。 sentiwordnet 中是否有任何功能。请帮忙。

【问题讨论】:

    标签: python python-2.7 pos-tagger senti-wordnet


    【解决方案1】:

    查看首先从评论中提取副词和形容词 例如:

    import nltk
    from nltk.tokenize import sent_tokenize, word_tokenize
    import csv
    
    para = "What can I say about this place. The staff of the restaurant is nice and the eggplant is not bad. Apart from that, very uninspired food, lack of atmosphere and too expensive. I am a staunch vegetarian and was sorely dissapointed with the veggie options on the menu. Will be the last time I visit, I recommend others to avoid"
    
    sentense = word_tokenize(para)
    word_features = []
    
    for i,j in nltk.pos_tag(sentense):
        if j in ['JJ', 'JJR', 'JJS', 'RB', 'RBR', 'RBS']: 
            word_features.append(i)
    
    rating = 0
    
    for i in word_features:
        with open('words.txt', 'rt') as f:
            reader = csv.reader(f, delimiter=',')
            for row in reader:
                if i == row[0]:
                    print i, row[1]
                    if row[1] == 'pos':
                        rating = rating + 1
                    elif row[1] == 'neg':
                        rating = rating - 1
    print  rating
    

    现在你必须有一个外部 csv 文件,其中应该有正面和负面的词

    喜欢: 皱纹,否定 起皱的,否定的 皱纹,否定 熟练地,pos 杰作,位置 杰作,位置

    上述脚本的工作原理如下:

    1 .读句子 2.提取副词和形容词 3.比较 CVS 的正面和负面词 4.然后给句子打分

    上述脚本的结果是:

    nice pos  
    bad neg  
    expensive neg  
    sorely neg  
    -2
    

    根据您的需要更改结果。 对不起我的英语:P

    【讨论】:

    • 谢谢。但是我们正在使用大量数据将其分类为我们无法手动传递的正面和负面词。所以我为此使用 sentiwordnet。如果有任何代码可以获取每个句子的正负分,我会很高兴。
    • 这只是一个例子@THIRTHA,我也用它来处理批量数据!!
    • 感谢您的帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2022-08-08
    • 2012-02-20
    • 1970-01-01
    • 2013-09-24
    • 2011-08-16
    相关资源
    最近更新 更多