代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如何将每个句子拆分为单独的单词和每个句子的平均极性得分,并在数据框中追加到新列中?

  •  0
  • RustyShackleford  · 技术社区  · 7 年前

    我可以成功地将一个句子分割成几个单独的单词,并使用这个代码计算每个单词的极性得分的平均值。它很好用。

    import statistics as s
    from textblob import TextBlob
    
    a = TextBlob("""Thanks, I'll have a read!""")
    print(a)
    
        c=[]
        for i in a.words: 
            c.append(a.sentiment.polarity)
            d = s.mean(c)
    
    
    d = 0.25
    a.words = WordList(['Thanks', 'I', "'ll", 'have', 'a', 'read'])
    

    如何将上述代码传输到类似这样的df?:

    东风

         text
    1    Thanks, I’ll have a read!
    

    但是每个词的平均极性是多少?

    最重要的是,我可以对df中的每一个句子应用极性:

    def sentiment_calc(text):
        try:
            return TextBlob(text).sentiment.polarity
        except:
            return None
    
    df_sentences['sentiment'] = df_sentences['text'].apply(sentiment_calc)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   kevh    7 年前

    here

    [TextBlob(x).sentiment.polarity for x in a.split()]
    

    import statistics as s
    from textblob import TextBlob
    import pandas as pd
    
    a = TextBlob("""Thanks, I'll have a read!""")
    
    def compute_mean(a):
        return s.mean([TextBlob(x).sentiment.polarity for x in a.split()])
    
    print(compute_mean("Thanks, I'll have a read!"))
    
    df = pd.DataFrame({'text':["Thanks, I'll have a read!",
        "Second sentence",
        "a bag of apples"]})
    
    df['score'] = df['text'].map(compute_mean)
    print(df)