ツイートの感情分析を行っています。ツイートの感情を計算する前に、絵文字といくつかの特殊文字を削除するアルゴリズムを作成しました。その後、絵文字や特殊文字を含まないツイートが感情とともにデータフレームに配置されます。コードは次のとおりです。
x = 0
a = 0
d = {}
for vertaling in df['text']:
bericht = re.sub('[^A-Za-z0-9]', ' ', df['text'].iloc[x])
bericht = re.sub(' +',' ', bericht)
translations = translator.translate([bericht], dest='en')
for translation in translations:
a = a + 1
print(a)
print(translation.origin)
analysis = TextBlob(translation.text)
print(analysis.sentiment)
x = x + 1
d[translation.origin] = analysis.sentiment
c = ['Tweets','Sentiment']
df2 = pd.DataFrame(list(d.items()), columns=c)
元のツイートと計算された感情を組み合わせてほしい。上記のコードは、フィルタリングされたツイートとこの特定の行の感情を組み合わせたものです。
c = ['Tweets','Sentiment']
df2 = pd.DataFrame(list(d.items()), columns=c)
元のツイートをデータフレーム内の新しい計算された感情と組み合わせる方法を知っている人はいますか?
気にしないでください、私は自分で解決策を見つけました。解決:
x = 0
a = 0
d = {}
#df2 = pd.DataFrame(['Tweets', 'Sentiment'])
df['Tweets'] = ""
df['Sentiment'] = ""
for vertaling in df['text']:
df['Tweets'].iloc[x] = df['text'].iloc[x]
bericht = re.sub('[^A-Za-z0-9]', ' ', df['text'].iloc[x])
bericht = re.sub(' +',' ', bericht)
translations = translator.translate([bericht], dest='en')
for translation in translations:
a = a + 1
print(a)
print(translation.origin)
analysis = TextBlob(translation.text)
df['Sentiment'].iloc[x] = analysis.sentiment
x = x + 1
d[translation.origin] = analysis.sentiment
これにより、新しい列が既存の列に結合されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加