【问题标题】:VADER sentiment analysis in Python: remove words from dictionaryPython中的VADER情感分析:从字典中删除单词
【发布时间】:2018-12-01 06:38:02
【问题描述】:

我正在使用 Python 的 nltk 库中的 VADER 情绪分析工具。我想知道是否有人知道如何从词典中删除单词而不必在文本文件中手动删除。使用 lexicon.update(new_words) 可以添加新单词。我们也可以使用类似的东西来删除单词吗?

所以,要添加新词,我们可以使用:

sia.lexicon.update(new_words)

我尝试使用 lexicon.remove 删除单词,但这不起作用。任何建议将不胜感激。

【问题讨论】:

    标签: python sentiment-analysis vader


    【解决方案1】:

    我认为VADER 中的lexicon 只是字典。
    如果是这种情况,请检查以下代码:

    # you can update your lexicon to add key:value pairs
    example_dict = {"a":1, "b":2, "c":3, "d":4}
    example_dict.update({"e":5})
    print(example_dict) # OUTPUT: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
    
    # you can pop a key from the dictionary to remove the key:value pair
    # .pop returns also the value
    example_dict.pop("a")
    print(example_dict) # OUTPUT: {'b': 2, 'c': 3, 'd': 4, 'e': 5}
    
    # if you're sure that all the values in another dictionary are present in yours,
    # you can map the pop function
    dict2 = {"b":2, "d":6}
    all(map( example_dict.pop, dict2))
    print(example_dict) # {'c': 3, 'e': 5}
    
    # you can also merge two dictionaries
    dict3 = {"x":44, "y":42}
    new_dict = {**example_dict, **dict3}
    print(new_dict) # OUTPUT: {'c': 3, 'e': 5, 'x': 44, 'y': 42}
    

    您可以对字典执行许多操作。如果确实是您的情况,请检查documentation

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多