我有一个数据集,包括教员id和学生对各自教员的反馈。每个教员都有多个评论,因此对每个教员的评论都以列表的形式出现。我想在数据集的“评论”栏上应用gensim摘要,根据学生的反馈生成教师绩效摘要。
为了进行一次试验,我试图总结与第一个教员id相对应的反馈。在这个特定的反馈中有8个不同的注释(句子),但是gensim抛出一个错误ValueError:输入必须有多个句子。
df_test.head()
csf_id comments
0 9 [' good subject knowledge.', ' he has good kn...
1 10 [' good knowledge of subject. ', ' good subjec...
2 11 [' good at clearing the concepts interactive w...
3 12 [' clears concepts very nicely interactive wit...
4 13 [' good teaching ability.', ' subject knowledg...
from gensim.summarization import summarize
text = df_test["comments"][0]
print("Text")
print(text)
print("Summary")
print(summarize(text))ValueError:输入必须有多个句子
我应该做些什么改变,以便摘要器阅读所有的句子并对它们进行总结。
发布于 2019-07-04 13:13:17
对于gensim摘要,换行符和句号将分隔句子。
from gensim.summarization.summarizer import summarize
summarize("punctual in time.") 这将引发相同的错误ValueError:输入必须有多个句子
现在,当在句号之后有什么东西时,它会把它解释为不止一个句子。
summarize("punctual in time. good subject knowledge")
#o/p will be blank string since the text is very small, and now you won't receive any error
''现在谈到您的问题,您需要将所有元素连接到一个字符串中。
#example
import pandas as pd
df = pd.DataFrame([[["good subject."," punctual in time.","discipline person."]]], columns = ['comment'])
print(df)
comment
0 [good subject., punctual in time, discipline ...
df['comment'] = df['comment'].apply(''.join)
df['comment'].apply(summarize) #this will work for you but keep in mind you have long text to generate summary 发布于 2019-07-05 08:58:07
得到了解决方案,实际上Pandas有内置的方法来完成这个任务。如果有些人面临同样的问题,请按照下面的代码进行操作。
df["comments"] = df["comments"].str.replace(",","").astype(str) df["comments"] = df["comments"].str.replace("[","").astype(str) df["comments"] = df["comments"].str.replace("]","").astype(str) df["comments"] = df["comments"].str.replace("'","").astype(str)
这样做将删除列表中的所有方括号和逗号,并且反馈将被视为一个字符串。然后,您可以使用以下方法总结数据格式行中的文本:
from gensim.summarization import summarize summary = summarize(df["comment[i]"]) print(summary)
https://stackoverflow.com/questions/56883959
复制相似问题