【问题标题】:Finding names in a list of bigrams?在二元组列表中查找名称?
【发布时间】:2021-11-03 20:47:59
【问题描述】:

我有一个正在处理的文本文件,我想标记每个单词,但将名称放在一起,例如“约翰·史密斯”。

我想使用 nltk.bigrams 来执行此操作,如果我使用它并获得一个二元组列表,我将如何在该列表中搜索两个单词都以大写字母开头的二元组?

bigrams = list(nltk.bigrams(text))

【问题讨论】:

    标签: python nltk


    【解决方案1】:
    list(filter(lambda L : L[0][0].upper() == L[0][0] and L[1][0].upper() == L[1][0], list(bigrams(text))))
    

    编辑: 作为解释,list(filter(lambda x : f(x), my_list))f(x) == True 的值过滤 my_list。在这里,我根据两个单词都以大写开头的值过滤了列表 list(bigrams(text))

    (由于list(bigrams(text)) 的元素L 是两个单词的元组,我检查L[0]L[1] 第一个字母是否为大写字母。)

    【讨论】:

    • 你能edit你的答案来解释你做了什么以及为什么?比如为什么这应该解决问题?
    • 现在清楚了吗?
    • 是的!只是,“编辑:”不是必需的,因为已经有编辑历史记录。此外,2 年后的人不会真正关心您进行了编辑以添加详细信息,他们只想要信息
    【解决方案2】:

    如果这是你想要的

    import nltk
    nltk.download('punkt')
    
    text = "My name is Shaida Muhammad and I'm not an extremist"
    text = nltk.word_tokenize(text)
    bigrams = list(nltk.bigrams(text)) 
    
    
    for first_word, second_word in bigrams:
        if first_word.istitle() and second_word.istitle():
            print(first_word, second_word)    # It will output Shaida Muhammad
    

    【讨论】:

    • 当我尝试运行此代码时出现错误:TypeError: 'str' object is not callable on line 5 'bigrams = list(nltk.bigrams(text))',你呢知道是什么原因造成的吗?
    • 追溯您的变量值。你是第 5 行的文本变量应该包含标记化的单词。不要在上面的行中使用二元组作为变量代码。
    • 或分享可重现的代码。
    【解决方案3】:

    IIUC,你想把你的句子分成单词,但把名字(两个以大写开头的连续单词)放在一起?

    你可以使用一个小的正则表达式:

    text = 'sentence where John Smith and Jane Doe are mentioned, here a Capital word alone'
    re.findall('[A-Z]\w+\s[A-Z]\w+|\w+', text)
    

    输出:

    ['sentence',
     'where',
     'John Smith',
     'and',
     'Jane Doe',
     'are',
     'mentioned',
     'here',
     'a',
     'Capital',
     'word',
     'alone']
    
    应用二元组
    [list(nltk.bigrams(x)) for x in re.findall('[A-Z]\w+\s[A-Z]\w+|\w+', text)]
    

    输出:

    [[('s', 'e'),
      ('e', 'n'),
      ('n', 't'),
      ('t', 'e'),
      ('e', 'n'),
      ('n', 'c'),
      ('c', 'e')],
     [('w', 'h'), ('h', 'e'), ('e', 'r'), ('r', 'e')],
     [('J', 'o'),
      ('o', 'h'),
      ('h', 'n'),
      ('n', ' '),
      (' ', 'S'),
      ('S', 'm'),
      ('m', 'i'),
      ('i', 't'),
      ('t', 'h')],
     [('a', 'n'), ('n', 'd')],
     [('J', 'a'),
      ('a', 'n'),
      ('n', 'e'),
      ('e', ' '),
      (' ', 'D'),
      ('D', 'o'),
      ('o', 'e')],
     [('a', 'r'), ('r', 'e')],
     [('m', 'e'),
      ('e', 'n'),
      ('n', 't'),
      ('t', 'i'),
      ('i', 'o'),
      ('o', 'n'),
      ('n', 'e'),
      ('e', 'd')],
     [('h', 'e'), ('e', 'r'), ('r', 'e')],
     [],
     [('C', 'a'), ('a', 'p'), ('p', 'i'), ('i', 't'), ('t', 'a'), ('a', 'l')],
     [('w', 'o'), ('o', 'r'), ('r', 'd')],
     [('a', 'l'), ('l', 'o'), ('o', 'n'), ('n', 'e')]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-30
      • 2011-01-12
      • 2016-07-18
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多