【问题标题】:Removing multiple recurring text from pandas rows`从熊猫行中删除多个重复出现的文本`
【发布时间】:2018-12-31 06:56:22
【问题描述】:

我有一个 pandas 数据框,其中包含从网站上抓取的文章作为行。我有 10 万篇类似性质的文章。

这是我的数据集的一瞥。

text
0   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
1   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
2   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
3   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
4   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
5   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
6   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
7   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
8   which brings not only warmer weather but also the unsettling realization that the year is more than halfway over. So
for those who werent as productive as they would have liked during the first half of 2018
28  for those who werent as productive as they would have liked during the first half of 2018
29  for those who werent as productive as they would have liked during the first half of 2018
30  for those who werent as productive as they would have liked during the first half of 2018
31  for those who werent as productive as they would have liked during the first half of 2018
32  for those who werent as productive as they would have liked during the first half of 2018

现在,这些是每个文本的缩写,它们是重复的。正文位于这些文本之后。

有没有什么方法或功能可以识别这些文本并在几行代码中将它们刷出来。

【问题讨论】:

  • “刷掉它们”是什么意思?您只是想删除这些短语?
  • 不,删除重复项只能应用于行,这都是不同的,因为主要文本位于这些重复文本之后。不同网站的报废对应着这些相似的重复出现的文字。
  • @HarvIpan,你明白我的意思了吗,如果你愿意,我可以澄清更多
  • 你知道这些短语吗?
  • 我可以提取一些,但我有数千个或更多这样的。我相信,这就是为什么我正在寻找能够自动提取的智能方法。

标签: python pandas nlp data-science text-processing


【解决方案1】:

我认为您可以以某种方式使用difflib,例如:

>>> import difflib
>>> a = "my mother always told me to mind my business" 
>>> b = "my mother always told me to be polite"
>>> s = difflib.SequenceMatcher(None,a,b)
>>> s.find_longest_match(0,len(a),0,len(b))

输出:

Match(a=0, b=0, size=28)

其中a=0表示匹配序列从字符串a中的字符0开始,b=0表示匹配序列从字符串b中的字符0开始。

现在如果你这样做:

>>> b.replace(a[:28],"")

输出将是:

'be polite'

如果你选择c = s.find_longest_match(0,len(a),0,len(b)),那么c[0] = 0c[1] = 0c[2] = 28

您可以在此处阅读更多信息: https://docs.python.org/2/library/difflib.html

【讨论】:

  • 感谢您抽出宝贵的时间,但我也可以采用不同的方式,我也想到了相同的方法,但它无法根据我的需要进行扩展。
  • 我投了赞成票,但我正在寻找更多指导和更直观的方法,您还有其他想法吗
  • 你可以先对列表进行排序,然后在字符串ii+1之间应用方法,这样你只比较0和1、1和2....如果你需要获取原始顺序,您可以对索引而不是值进行排序。不过,这只会有助于主角的相似之处:/
【解决方案2】:

如果要删除完全相同的字符串,请对数据框进行排序,然后按顺序遍历它。 (这与 Nerdrigo 在评论中提到的类似。)

sents = ... # sorted dataframe
out = [] # stuff here will be unique
for ii in range(len(sents) - 1):
    if sents[ii] != sents[ii + 1]:
        out.append(sents[ii])

如果要删除非常相似但不完全相同的句子,问题就更难了,也没有简单的解决方案。您需要研究 locality-sensitive hashingnear-duplicate detectiondatasketch 库可能会有所帮助。


根据您的评论,我想我终于明白了 - 您想删除 通用前缀。在这种情况下,将上面的代码修改为:

sents = ... # sorted dataframe
out = [] # cleaned sentences go here
lml = -1 # last match length
for ii in range(len(sents) - 1):
    # first check if the match from the last iteration still works
    if sents[ii][:lml] == sents[ii+1][:lml] and sents[ii][:lml + 1] != sents[ii+1][:lml + 1]:
        # old prefix still worked, chop and move on
        out.append(sents[ii][lml:])
        continue

    # if we're here, it means the prefix changed
    ml = 1 # match length
    # find the longest matching prefix
    while sents[ii][:ml] == sents[ii+1][:ml]:
        ml += 1

    # save the prefix length
    lml = ml
    # chop off the shared prefix
    out.append(sents[ii][ml:])

【讨论】:

  • 真的很抱歉,我的想法是删除开始时出现的相同字符串。我不想完全删除它们,因为主要文本在 df 行中重复出现的文本之后开始。
  • 这个,长度是初始数据帧的1/20,我想保持长度相同,因为它们都不同
  • 所以我可以说,这些是输出的不同重复模式,大约是所有 df 的 1/20。但所有内容都不同,因为文章内容在这些文本之后
猜你喜欢
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 2016-01-30
  • 2019-11-13
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
相关资源
最近更新 更多