【问题标题】:Python : Group tagging of words that have concecutive positionsPython:对具有连续位置的单词进行分组标记
【发布时间】:2021-11-03 11:21:50
【问题描述】:

我有一个包含 3 列的数据框:分别为 'text', 'in', 'tar'type(str, list, list)

                   text                                              in     tar
0  This is an example text that I use in order to  get an answer     [2]    [6]
1  Discussion: We are examining the possibility of this solution.    [3]    [6, 7, 8]

intar 表示我要标记到文本中的特定实体,它们返回每个找到的实体术语在文本中的位置。

例如,在in = [3] 所在的数据框的第二行,我从text 列中取出第三个单词(即:“正在检查”)并将其标记为<IN>examining</IN>

同样,对于同一行,由于tar = [6,7, 8],我有<TAR>of</TAR><TAR>this</TAR><TAR>solution</TAR>

但我想要的是当有连续的​​位置(即[1,2,3]或[6,7,8])在一个标签中together标记它们,例如@987654333 @。

我只想在位置连续(即:[1,2,3])时这样做,而不是在它们不连续时(即 [1,3,5])。

这是我目前所拥有的:

data = {'text': ['This is an example text that I use in order to get an answer',
                 'Discussion: We are examining the possibility of this solution'],
        'in': [[2], [3]],
        'tar': [[6], [6, 7, 8]]}
df = pd.DataFrame(data)
cols = list(df.columns)[1:]
new_text = []
for idx, row in df.iterrows():
    temp = list(row['text'].split())
    for pos, word in enumerate(temp):
        for col in cols:
            if pos in row[col]:
                temp[pos] = f'<{col.upper()}>{word}</{col.upper()}>'
    new_text.append(' '.join(temp))
df['text'] = new_text
print(df.text.to_list())

输出:

['This is <IN>an</IN> example text that <TAR>I</TAR> use in order to get an answer', 
 'Discussion: We are <IN>examining</IN> the possibility <TAR>of</TAR> <TAR>this</TAR> <TAR>solution</TAR>']

期望的输出:

 ['Discussion: We are <IN>examining</IN> the possibility <TAR>of this solution</TAR>']

有人可以帮忙吗?

【问题讨论】:

    标签: python pandas dataframe nlp python-re


    【解决方案1】:

    其中一种方法:

    import pandas as pd
    data = {'text': ['This is an example text that I use in order to get an answer',
                     'Discussion: We are examining the possibility of this solution'],
            'in': [[2], [3]],
            'tar': [[6], [2, 5, 6, 7, 8]]}
    df = pd.DataFrame(data)
    cols = list(df.columns)[1:]
    for idx, row in df.iterrows():
        # Split the text on spaces
        temp = list(row['text'].split())
        for col in cols:
            # Initialise data with <IN> or <TAR> based on column
            data = f'<{col.upper()}>'
            string = []
            for i, value in enumerate(row[col]):
                # append the word at index 'i' in row[col] in 'temp' to 'data'
                # Eg: <IN>an
                data += temp[row[col][i]]
                # If the next value is a consecutive number, replace the ith word in text
                # with data obtained so far and continue
                if (len(row[col])>i+1 and row[col][i+1]-value == 1):
                    # example 'possibility' will be replaced by '<TAR>possibility'
                    temp[row[col][i]] = data
                    data = ''
                    continue
                else:
                    # If next index is not consecutive, append </IN> or </TAR> to 'data' based on column
                    # Eg: <IN>an</IN>
                    data += f'</{col.upper()}>'
                    # Replace for eg. 'an' with '<IN>an</IN>''
                    temp[row[col][i]] = data
                    data = f'<{col.upper()}>'
    
        row['text'] = ' '.join(temp)
    print (df.text.to_list())
    

    输出:

    ['This is <IN>an</IN> example text that <TAR>I</TAR> use in order to get an answer', 'Discussion: We <TAR>are</TAR> <IN>examining</IN> the <TAR>possibility of this solution</TAR>']
    

    【讨论】:

    • 您好,谢谢。它适用于数据集的子集,但在整个数据集上,我收到此错误:data += temp[row[col][i]]IndexError: list index out of range。你知道为什么吗?
    • 您得到索引错误的特定数据集是什么?
    • 这是一个包含 1M 行的数据集,我通过 SQL 查询检索它,所以当我使用“LIMIT 400”时出现此错误,而如果我使用“LIMIT 300”或更少,它工作正常
    • 可能是因为出现错误的条目介于 300-400 之间,所以如果您在此 @987654326 之后添加一些调试语句以打印行(如添加 print (row))会很好@loop for idx, row in df.iterrows(): 并检查这一行的内容
    【解决方案2】:

    作为对现有代码的快速修复,您可以将 &lt;/TAR&gt; &lt;TAR&gt; 替换为空格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 2015-05-23
      • 2023-03-11
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多