【问题标题】:joining sentences from a list in python3从python3中的列表中加入句子
【发布时间】:2018-04-26 17:03:20
【问题描述】:

我正在尝试将附加句子列表加入到一个大的字符串文本对象中,以便我可以将其用作 Gensim 汇总模块的输入。但是,当我尝试这样做时,它说输入的句子小于 2。但是当我对文本运行拆分时,我看到多个句子,但它计算每个句子一次,而不是句子总数。而变量 r 是一个字符串类型的对象。我想将这些句子连接成一个大字符串,以便可以通过 Gensim 汇总模块读取。

示例代码:

import re
ruling_corpora  = re.findall("\.?([^\.].\*?I find[^\.]*\. |[^\.]*$In sum[^\.]*\. |[^\.]*$agree[^\.]*\.)", tokenized, re.I |re.DOTALL |re.M)[1:-1]

for r in ruling_corpora:                                   
    print(type(r))
    rc= ''.join(r)
    print(summarize(rc))

样本输出:

raise ValueError("input must have more than one sentence")
ValueError: input must have more than one sentence

这是我想用 Gensim 总结器总结的输入示例。每个字符串下面的数字代表以句点结尾的句子的数量:

####Beginning of File### LUMB65.BL23607963.xml
Background Content: ANDERSON INITIAL DECISIONOn January 13, 2015, the appellant filed this appeal arguing that the agency's decision not to renew his term limited appointment which expired on January 28, 2015, is in error.  

 For the reasons discussed below, this appeal is DISMISSED for lack of jurisdiction without a hearing.
1
There is nothing in the agreement that curtails the agency's ability not to extend the term appointment. 
 IdIn reviewing the appellant's arguments, the appellant fails to establish that the Board has jurisdiction to review the agency's decision not to renew his time-limited appointment at issue in this appeal.
 Following a review of the record evidence, I find that the appellant has failed to non-frivolously allege Board jurisdiction over this appeal on any basis.
 Accordingly, this appeal must be dismissed for lack of jurisdiction.
1
####End of File### LUMB65.BL23607963.xml

【问题讨论】:

  • 请修正你的缩进

标签: python regex list join gensim


【解决方案1】:

根据the documentation(强调我的):

输入应该是一个字符串,并且必须比 INPUT_MIN_LENGTH 长 使摘要有意义的句子。文本将分为 使用 split_sentences 方法的句子 gensim.summarization.texcleaner 模块。 请注意,换行符分开 句子。

尝试使用rc = '\n'.join(r)。也可以调用gensim.summarization.texcleaner.split_sentences进行调试,查看结果。

此外,您的正则表达式与您给定的输入不匹配,即使匹配,您也会丢弃带有[1:-1] 的仅有的两个结果。试试这个:

>>> map(lambda x: x[0], re.findall('([^.]*?(I find|In sum|agree)[^.]*\.)', tokenized, re.I | re.DOTALL | re.M))
["\n1\nThere is nothing in the agreement that curtails the agency's ability not to extend the term appointment.", '\n Following a review of the record evidence, I find that the appellant has failed to non-frivolously allege Board jurisdiction over this appeal on any basis.']

您可能需要先处理独立数字,因为它们会出现在比赛中。

【讨论】:

  • 谢谢。我尝试了 rc = '\n'.join(r) 但它似乎将单词垂直拆分而不是一起拆分。但我会尝试从 Gensim 库中调用 textcleaner 方法
  • 请用您的输入创建一个MVE(即tokenized的值)。
  • 示例输入在我的帖子中。它位于以下行:####Beginning of File### LUMB65.BL23607963.xml
  • 您的正则表达式与该输入完全不匹配。如果您删除了 I find 中的尾随空格,您将得到一个匹配项,但您会在最后使用 [1:-1] 将其丢弃。
  • 我做了这些调整。并感谢您的帮助。但是,当我仍然加入匹配的句子时,由于某种原因,它不会将加入的字符串视为总字符串,而是单独计算每一行。因此,例如,如果我有 4 个连接的句子,当我计算句子的数量时,它会将每个句子视为单独的句子,而不是组合在一起。
猜你喜欢
  • 1970-01-01
  • 2018-08-13
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
相关资源
最近更新 更多