【问题标题】:Extracting only nouns from list of lists pos_tag sequence? [duplicate]从列表pos_tag序列列表中仅提取名词? [复制]
【发布时间】:2019-05-14 16:53:51
【问题描述】:

我正在尝试使用nltk.pos_tag()list of lists text sequence 中仅提取名词。我能够从nltk.pos_tag() 列表中提取所有名词,而不保留列表序列的列表吗?如何通过保留列表序列的列表来实现这一点。非常感谢任何帮助。

这里,列表文本序列集合的意思是:由列表分隔的标记化单词的集合。

[[('icosmos', 'JJ'), ('cosmology', 'NN'), ('calculator', 'NN'), ('with', 'IN'), ('graph', 'JJ')], [('generation', 'NN'), ('the', 'DT'), ('expanding', 'VBG'), ('universe', 'JJ')], [( '美国', 'JJ'), ('研究所', 'NN')]]

输出应如下所示:

[['宇宙学', '计算器'], ['世代'], [研究所]]

我尝试过的如下:

def function1():
    tokens_sentences = sent_tokenize(tokenized_raw_data.lower())
    unfiltered_tokens = [[word for word in word_tokenize(word)] for word in tokens_sentences]
    word_list = []
    for i in range(len(unfiltered_tokens)):
        word_list.append([]) 
    for i in range(len(unfiltered_tokens)):
        for word in unfiltered_tokens[i]:
            if word[:].isalpha():
               word_list[i].append(word[:])
    tagged_tokens=[]
    for token in word_list:
        tagged_tokens.append(nltk.pos_tag(token))
    noun_tagged = [(word,tag) for word, tag in tagged_tokens 
            if tag.startswith('NN') or tag.startswith('NNPS')]
    print(nouns_tagged)

如果我在附加 tagged_tokens 列表后在原始代码中使用了下面提到的 code-shippet,则输出显示在单个列表中,这不是必需的。

only_tagged_nouns = []
for sentence in tagged_tokens:
    for word, pos in sentence:
        if (pos == 'NN' or pos == 'NNPS'):
            only_tagged_nouns.append(word)

【问题讨论】:

    标签: python list nltk pos-tagger


    【解决方案1】:

    对单行解决方案使用列表推导:

    inputList = [[('icosmos', 'JJ'), ('cosmology', 'NN'), ('calculator', 'NN'), ('with', 'IN'), ('graph', 'JJ')], [('generation', 'NN'), ('the', 'DT'), ('expanding', 'VBG'), ('universe', 'JJ')], [('american', 'JJ'), ('institute', 'NN')]]
    
    [[k[0] for k in j if k[1].startswith("NN")] for j in inputList]
    
    #[['cosmology', 'calculator'], ['generation'], ['institute']]
    

    【讨论】:

    • 谢谢@lenka。但是,我不需要扁平列表。顺便感谢您的帮助。
    • 输出列表未展平。
    • 我的坏。打错字了!
    • 顺便说一句,如果你想要扁平化列表,你可以这样做:[k[0] for j in inputList for k in j if k[1].startswith("NN")]
    • 感谢@Lenka 为我解惑。这对我帮助很大。
    【解决方案2】:

    你可以这样做:

    words = [[('icosmos', 'JJ'), ('cosmology', 'NN'), ('calculator', 'NN'), ('with', 'IN'), ('graph', 'JJ')], [('generation', 'NN'), ('the', 'DT'), ('expanding', 'VBG'), ('universe', 'JJ')], [('american', 'JJ'), ('institute', 'NN')]]
    
    new_list = []
    for i in words:
        temp = [j[0] for j in i if j[1].startswith("NN")]
        new_list.append(temp)
    
    print(new_list)
    

    输出

    [['cosmology', 'calculator'], ['generation'], ['institute']]
    

    【讨论】:

    • @它有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 2019-11-05
    • 2017-06-19
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多