【问题标题】:Improve text classification accuracy by using POS tagging - NLP通过使用 POS 标记提高文本分类准确性 - NLP
【发布时间】:2021-11-13 19:24:22
【问题描述】:

我正在做一个项目,将推文分类为健康和政治类别。我使用朴素贝叶斯算法进行分类。

我正在尝试通过应用 POS 标记来提高朴素贝叶斯分类的准确性。因为我认为,分配语言信息会提高分类效率。

在预处理和应用 pos 标记后,我的数据集如下所示:

ID      tweet                               Category   pos_tagged_tweet
1   හාමුදුරුවරු සියලූම පූජකයන්ගේ මානසික සෞඛ්යය  Health    [(හාමුදුරුවරු, NNC), (සියලූම, NNC), (පූජකයන්ගේ, NNC), (මානසික, JJ), (සෞඛ්යය, NNC), (., FS)]
2   ද්විපාර්ශවික එකඟතා ජන ජීවිත සෞඛ්යය මනාව    Politics  [(ද්විපාර්ශවික, NNP), (එකඟතා, NNP), (ජන, JJ), (ජීවිත, NNJ), (සෞඛ්යය, NNC), (මනාව, RB),  (., FS)]
3   කරැනාකර චින නිෂ්පාදිත එන්නත ලබාගත්         Health    [(කරැනාකර, NNC), (චින, VP), (නිෂ්පාදිත, VP), (එන්නත, NNC), (ලබාගත්, VP),(., FS)]
'
'
'

我需要知道如何为朴素贝叶斯算法应用 pos_tagged_tweet 列和 Category 列来分类推文是基于健康的推文还是政治推文。我正在使用 python 和 NLTK 来实现。

【问题讨论】:

    标签: python nlp text-classification naivebayes pos-tagger


    【解决方案1】:

    我的想法是用句子中对应的 pos_tags 替换单词并形成如下的新属性:

    sentence = ["A quick brown fox jumped over the cat",
            "An apple fell from a tree",
            "I like old western classics"]
    tokenized_sents = [nltk.word_tokenize(i) for i in sentence]
    print(tokenized_sents)
    pos_tags = [nltk.pos_tag(token) for token in tokenized_sents]
    
    print(pos_tags)
    
    [[('A', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ('jumped', 'VBD'), ('over', 'IN'), ('the', 'DT'), ('cat', 'NN')], [('An', 'DT'), ('apple', 'NN'), ('fell', 'VBD'), ('from', 'IN'), ('a', 'DT'), ('tree', 'NN')], [('I', 'PRP'), ('like', 'VBP'), ('old', 'JJ'), ('western', 'JJ'), ('classics', 'NNS')]]
    

    现在通过用 pos_tags 替换句子中的单词,从 pos_tags 创建词向量。

    # from gensim.test.utils import common_texts
    from gensim.models import Word2Vec
    
     pos_tag_list = [['DT', 'JJ', 'NN', 'NN', 'VBD', 'IN', 'DT', 'NN'],
                          ['DT','NN','VBD','IN','DT','NN'],['PRP','VBP','JJ','JJ','NNS']]
     w2v_model = Word2Vec(min_count=1,
                     window=2,
                     size=30,
                     sample=1e-5, 
                     alpha=0.01, 
                     min_alpha=0.0007, 
                     negative=0,
                     workers=2)
    w2v_model.build_vocab(pos_tag_list, progress_per=1)
    w2v_model.train(pos_tag_list, total_examples=w2v_model.corpus_count, epochs=3, report_delay=1)
    
    # get the vectors for the Pos_tags from w2v_model
     my_dict = dict({})
     for index, key in enumerate(w2v_model.wv.vocab):
        my_dict[key] = w2v_model.wv[key]
    
     # Sample Output vector for pos_tags, we got 30-dimensional word vector since
     we used size=30.
    
      {'DT': array([-0.01487986,  0.00341667,  0.00576919, -0.01203213,  0.01111736,
         0.01643543,  0.00583243,  0.00283635, -0.00892249,  0.01334178,
         0.01324782,  0.00843606,  0.00965199,  0.00849338, -0.00584444,
        -0.00482766,  0.01218408, -0.00959254, -0.00172328,  0.01302824,
        -0.00374165, -0.01516393, -0.00604865,  0.00170989,  0.00843781,
        -0.01403714,  0.00150807,  0.01511062,  0.00798908,  0.0088043 ],
       dtype=float32)}
    

    现在对于 pos_tag_list 中的每个条目,将 pos_tags 替换为向量,并为朴素贝叶斯模型创建一个训练数据集。您还可以将实际的词向量与 pos_tags 一起使用,并创建一个综合数据集。我没有专门研究它,但根据我发现的研究,我认为这可能有效。试试看。

    【讨论】:

    • 非常感谢@Priya。我会试一试的。
    【解决方案2】:

    首先,当提出问题时,请遵循guidelines 并包括一个最小的可重现示例,如here 所述。

    现在,我假设您使用的是 nltk,并且所有内容都是英文的。首先,你需要标记你的句子,然后应用 POS Tagging:

    import nltk
     
    sentence = "Can you help me with this?"
    tokens = nltk.word_tokenize(sentence)
    pos = nltk.pos_tag(tokens)
    
    print(pos)
    

    这将为您提供以下列表:

    [('Can', 'MD'), ('you', 'PRP'), ('help', 'VB'), ('me', 'PRP'), ('with', 'IN'), ('this', 'DT'), ('?', '.')]
    

    您可以在他们的documentation 上找到有关 NLTK 的更多信息。

    【讨论】:

    • 一个想法是用相应的邮资代替单词
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2020-08-13
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多