使用gensim摘要器按列汇总列表中存在的多个句子

时间:2019-07-04 08:45:50

标签: python nlp gensim

我有一个数据集,由教职员工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:输入必须包含一个以上的句子

我需要进行哪些更改,以便摘要器读取所有句子并对其进行总结。

2 个答案:

答案 0 :(得分:0)

对于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 

答案 1 :(得分:0)

得到了解决方案,实际上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)