如何将列表对象作为字符串传递?

我正在使用Textblob分析一本书的全文,通过单独分析句子来汇总本章的内容。我有一个脚本,可以将各章转换成单个句子的列表,但是我无法找出一种将这些列表对象作为字符串传递给Naive Bayes Analyzer的方法,因为它只希望输入字符串。

到目前为止,我仅尝试将整个列表作为参数传递,但是它始终给我相同的错误。

 TypeError: The `text` argument passed to `__init__(text)` must be a string,not <class 'list'>

这是我的代码:

from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
blob = TextBlob("This is a horrible idea.",analyzer=NaiveBayesAnalyzer())
blob.sentiment
print(blob.sentiment)

我的列表看起来像这样:

sentences = ['Maria was five years old the first time she heard the word 
hello.\n','It happened on a Thursday.\n',]

如何更改此代码以接收整个句子列表,并将输出作为数据帧传递?如果可能的话,我想要这样的东西:

                                         Line          Polarity Subjectivity Classification
0    Mariam was five years old the first time sh      0.175000   0.266667   Pos                                                 
1    It happened on a Thursday.                       0.000000   0.000000 Neu
tong817480 回答:如何将列表对象作为字符串传递?

您的意思是构造一个像这样的数据框。或者至少这是我从您的问题中了解到的。 我假设您有句子列表,并且在运行分析器之前将它们加入了一个段落。

import pandas as pd
from textblob import TextBlob

from textblob.sentiments import NaiveBayesAnalyzer

df = pd.DataFrame(columns = ['Line','Polarity','Subjectivity','Classification'])
sentences = ['Maria was five years old the first time she heard the word hello.\n','It happened on a Thursday.\n',]

blob = TextBlob("".join(sentences),analyzer=NaiveBayesAnalyzer())
for sentence in blob.sentences:
    df.loc[len(df)] = [str(sentence),sentence.polarity,sentence.subjectivity,sentence.sentiment.classification]
print(df)
本文链接:https://www.f2er.com/3139457.html

大家都在问