【问题标题】:NLP / Rails sentiment searchNLP / Rails 情感搜索
【发布时间】:2021-02-12 19:36:03
【问题描述】:

我正在从头开始构建一个工具,它获取文本样本并将其转换为类别列表。我目前没有为此使用任何库,但如果有人在这个领域有经验,我很感兴趣,因为我正在努力解决的最困难的部分是建立对搜索的情感。单词匹配很容易,但情绪更具挑战性。

我们的目标是采取类似这一段的内容;

"Whenever I am out walking with my son, I like to take portrait photographs of him to see how he changes over time. My favourite is a pic of him when we were on holiday in Spain and when his face was covered in chocolate from a cake we had baked"

把它变成

categories = ['father', 'photography', 'travel', 'spain', 'cooking', 'chocolate']

如果可能的话,我想最后添加一个过滤器来过滤负面情绪,这样如果文字说的话;

"I hate cooking" 

“烹饪”不会包含在类别中。

非常感谢任何帮助。 TIA ????

【问题讨论】:

    标签: ruby-on-rails search nlp


    【解决方案1】:

    您似乎至少有两个任务:1.按主题进行序列分类; 2. 情绪分析。 [编辑,我现在才注意到您使用的是 Ruby/Rails,但下面的代码是 Python 中的。但也许这个答案对某些人仍然有用,并且这些步骤可以应用于任何语言。]

    1.对于按主题进行的序列分类,您可以使用您所说的单词列表简单地定义类别。根据用例,这可能是最简单的选择。如果创建该单词列表过于耗时,您可以使用预训练的零样本分类器。我会推荐 HuggingFace 的零样本分类器,详情请参阅代码 here

    应用于您的用例,如下所示:

    # pip install transformers  # pip install in terminal
    from transformers import pipeline
    
    classifier = pipeline("zero-shot-classification")
    
    sequence = ["Whenever I am out walking with my son, I like to take portrait photographs of him to see how he changes over time. My favourite is a pic of him when we were on holiday in Spain and when his face was covered in chocolate from a cake we had baked"]
    candidate_labels = ['father', 'photography', 'travel', 'spain', 'cooking', 'chocolate']
    
    classifier(sequence, candidate_labels, multi_class=True)
    
    # output: 
    {'labels': ['photography', 'spain', 'chocolate', 'travel', 'father', 'cooking'],
     'scores': [0.9802802205085754, 0.7929317951202393, 0.7469273805618286, 0.6030028462409973, 0.08006269484758377, 0.005216470453888178]}
    

    分类器返回分数取决于每个候选标签在您的序列中表示的确定程度。它无法捕捉所有内容,但效果很好,并且可以快速付诸实践。

    2。对于情绪分析,您可以使用 HuggingFace 的sentiment classification pipeline。在您的用例中,这看起来像这样:

    classifier = pipeline("sentiment-analysis")
    sequence = ["I hate cooking"]
    classifier(sequence)
    
    # Output
    [{'label': 'NEGATIVE', 'score': 0.9984041452407837}]
    

    将 1. 和 2. 放在一起: 我可能会(a)首先把你的整个文本分成几个句子(see here怎么做);然后 (b) 对每个句子运行情绪分类器并丢弃那些具有高负面情绪分数的句子(参见上面的步骤 2.),然后 (c) 对剩余的句子运行标签/主题分类(参见上面的 1.)。

    【讨论】:

    • 非常感谢您。我现在无法强调以上信息对我的价值。这对我来说都是非常新的,我有一次陡峭的攀登才能对它有一个基本的了解。真的很感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多