【问题标题】:count the occurrences of POS tagging pattern计算 POS 标记模式的出现次数
【发布时间】:2021-08-24 04:08:45
【问题描述】:

所以我已将 POS 标记应用于我的数据框中的一列。对于每个句子,我想统计这种模式的出现次数:NNP、MD、VB。

例如,我有以下句子: 委托人与承包商之间的通信应使用英语

POS 标记将是: (communications, NNS), (between,IN), (the, DT), (Principal, NNP), (and, CC), (the, DT), (Contractor, NNP), (shall, MD) , (be,VB), (in, DT), (the, DT), (English, JJ), (language, NN).

注意在词性标注结果中,模式(NNP, MD, VB)存在并且出现了1次。我想在 df 中为这个出现次数创建一个新列。

有什么想法可以做到这一点吗?

提前致谢

【问题讨论】:

    标签: python dataframe nlp pos-tagger


    【解决方案1】:

    一个简单的计数器功能将执行您想要的!

    输入:

    df = pd.DataFrame({'POS':['(communications, NNS), (between,IN), (the, DT), (Principal, NNP), (and, CC), (the, DT), (Contractor, NNP), (shall, MD), (be,VB), (in, DT), (the, DT), (English, JJ), (language, NN)', '(Contractor, NNP), (shall, MD), (be,VB), (communications, NNS), (between,IN), (the, DT), (Principal, NNP), (and, CC), (the, DT), (Contractor, NNP), (shall, MD), (be,VB), (in, DT), (the, DT), (English, JJ), (language, NN)', '(and, CC), (the, DT)']})
    

    功能:

    def counter(pos):
        words, tags = [], []
        for item in pos.split('), ('):
            temp = item.strip(' )(')
            word, tag = temp.split(',')[0], temp.split(',')[-1].strip()
            words.append(word); tags.append(tag)
        length = len(tags)
        if length<3:
            return 0
        count = 0
        for idx in range(length):
            if tags[idx:idx+3]==['NNP', 'MD', 'VB']:
                count+=1
        return count
    

    输出:

    df['occ'] = df['POS'].apply(counter)
    df
    
        POS     occ
    0   (communications, NNS), (between,IN), (the, DT)...   1
    1   (Contractor, NNP), (shall, MD), (be,VB), (comm...   2
    2   (and, CC), (the, DT)    0
    

    【讨论】:

    • 谢谢!但是我遇到了一个错误,说'list'对象没有属性'split'。这可能是因为 POS 标签结果不是字符串,即格式是 [(communications, NNS), (between,IN), (the, DT)...] 而不是 ['(communications, NNS), (between, IN), (the, DT), ...']
    • 是的,我想就是这个原因! :[@cookieclatter
    • 一切都好,我修好了:),再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多