【问题标题】:Merge tuples if their first element starts with specific string如果元组的第一个元素以特定字符串开头,则合并元组
【发布时间】:2021-11-19 16:26:00
【问题描述】:

我有一个棘手的问题,我不知道如何解决,所以我会尝试彻底解释它。我的数据框有 5 列,这是一个示例行:

 title                                                                                                                                                                                                                                                                                                                                                                                             in_id    tar_id       in           tar  
[('<IN>This</IN>', 'DET', 'nsubj'), ('is', 'VERB', 'ROOT'), ('an', 'DET', 'det'), ('example', 'NOUN', 'compound'), ('text', 'NOUN', 'attr'), ('that', 'DET', 'dobj'), ('I', 'PRON', 'nsubj'), ('use', 'VERB', 'relcl'), ('in', 'ADP', 'prep'), ('order', 'NOUN', 'pobj'), ('to', 'PART', 'aux'), ('<TAR>get</TAR>', 'VERB', 'acl'), ('<TAR>an</TAR>', 'DET', 'det'), ('answer', 'NOUN', 'dobj')]    2137     2984      [1]       [12, 13]

所以在title中我们有一个元组列表,其中第一个元组元素是一个单词,第二个是词性标签,第三个是它的依赖树标签。

intar 列是数组,它们映射到我想分别用“单词”或“单词”标记的句子中单词的位置。

in_idtar_id 映射到它们的 ID。

我想要的是这个:

如果在in 和/或tar 中有多个值(例如在我显示tar=[12, 13] 的行中,我希望位置12 和13 的单词成为一个元组而不是两个单独的元组。

所以这个('&lt;TAR&gt;get&lt;/TAR&gt;', 'VERB', 'acl'), ('&lt;TAR&gt;an&lt;/TAR&gt;', 'DET', 'det')

应该变成这样:

('&lt;TAR&gt;get an&lt;/TAR&gt;', 'TAR', '')

所以 2 个元组的第一个元素合并,第二个元素被重命名为列名(INTAR,第三个元素为空。

cols = list(df.columns)[4:]
for i in range(len(df))

parsed_sent = []
for idx, row in df.iterrows():
    doc = nlp(row['title'])
    dep_sents = [(token.text, token.pos_, token.dep_) for token in doc if not token.pos_ == "PUNCT"]
    for position, tuple_ in enumerate(dep_sents):
        word = tuple_[0]
        pos_tag = tuple_[1]
        dep = tuple_[2]
        for col in cols:
          if position in row[col]:
              word = f'<{col.upper()}>{word}</{col.upper()}>'
          else:
              word = word
    tuple_ = (word, pos_tag, dep)
    dep_sents[position] = tuple_
parsed_sent.append(dep_sents)
df['title'] = parsed_sent

【问题讨论】:

    标签: python nlp tuples


    【解决方案1】:

    我认为应该这样做。您的索引从 1 开始,这很奇怪,我的代码假设它们从 0 开始 合并将发生在第一个索引上

    import re
    def merge_tags(tag, indices, tuples):
    
        untar=re.compile(f'<{tag}>(.+)</{tag}>')
    
        tar_text=[]
        for i in indices:
            column,*_ = tuples[i]
            tar_text.append(re.match(untar, column).group(1))
        tar_text=' '.join(tar_text)
    
        for i in indices[1:]:
            del tuples[i]
            
        tuples[indices[0]]=(f'<{tag}>{tar_text}</{tag}>', f'{tag}', '')
        return tuples
        
    tars=[11, 12] # in python indices start at 0
    row=[('<IN>This</IN>', 'DET', 'nsubj'), ('is', 'VERB', 'ROOT'), ('an', 'DET', 'det'), ('example', 'NOUN', 'compound'), ('text', 'NOUN', 'attr'), ('that', 'DET', 'dobj'), ('I', 'PRON', 'nsubj'), ('use', 'VERB', 'relcl'), ('in', 'ADP', 'prep'), ('order', 'NOUN', 'pobj'), ('to', 'PART', 'aux'), ('<TAR>get</TAR>', 'VERB', 'acl'), ('<TAR>an</TAR>', 'DET', 'det'), ('answer', 'NOUN', 'dobj')]
    print(merge_tags('TAR', tars, row))
    >>> [('<IN>This</IN>', 'DET', 'nsubj'), ('is', 'VERB', 'ROOT'), ('an', 'DET', 'det'), ('example', 'NOUN', 'compound'), ('text', 'NOUN', 'attr'), ('that', 'DET', 'dobj'), ('I', 'PRON', 'nsubj'), ('use', 'VERB', 'relcl'), ('in', 'ADP', 'prep'), ('order', 'NOUN', 'pobj'), ('to', 'PART', 'aux'), ('<TAR>get an</TAR>', 'TAR', ''), ('answer', 'NOUN', 'dobj')]
    

    你已经知道如何遍历行,所以我会尽量减少

    row['title']=merge_tags('TAR', row['tar'], row['title'])
    row['title']=merge_tags('IN', row['in'], row['title'])
    

    【讨论】:

    • 我仍然不确定如何将其应用于数据帧的每一行,不仅适用于“TAR”,也适用于“IN”
    猜你喜欢
    • 2015-05-03
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2012-10-11
    • 2020-10-05
    相关资源
    最近更新 更多