我有一个文本文件和 2 个用户定义的正负文件。我正在将两个文件中的单词与文本文件进行比较,并返回正数或负数。
但我需要在文本中打印这些关键字,将它们分类为正面或负面。
我正在寻找的输出示例:
file_name IBM Keywords Label
audio1.wav The customer is good good Positive
audio2.wav the service is bad bad Negative
请让我知道如何去做。这是到目前为止的代码
pos = readwords('C:\\Users\\anagha\\Desktop\\SynehackData\\positive.txt')
neg = readwords('C:\\Users\\anagha\\Desktop\\SynehackData\\Negative.txt')
pos = [w.lower() for w in pos]
neg = [w.lower() for w in neg]
def assign_comments_labels(x):
try:
if any(w in x for w in pos) :
return 'positive'
elif any(w in x for w in neg):
return 'negative'
else:
return 'neutral'
except:
return 'neutral'
import pandas as pd
df = pd.read_csv("C:\\Users\\anagha\\Desktop\\SynehackData\\noise_free_audio\\outputfile.csv", encoding="utf-8")
df['IBM'] = df['IBM'].str.lower()
df['file_name'] = df['file_name'].str.lower()
df['labels'] = df['IBM'].apply(lambda x: assign_comments_labels(x))
df[['file_name','IBM','labels']]