从python中的标记语料库中提取

时间:2012-01-31 15:16:35

标签: python nlp nltk

我正在尝试从标记语料库中提取专有名词,比方说 - 从nltk标记语料库褐色我试图提取仅标记为“NP”的单词。

我的代码:

  import nltk
  from nltk.corpus import brown
  f = brown.raw('ca01')
  print nltk.corpus.brown.tagged_words()
  w=[nltk.tag.str2tuple(t) for t in f.split()]
  print w

但它没有显示单词只显示

[]

示例输出:

    [('The', 'AT'), ('Fulton', 'NP-TL'), ...]
    []

为什么会这样?

感谢。

我只是引用f.split()..然后我得到了

             [('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'), ('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'), ('investigation', 'NN'), ('of', 'IN'), ("Atlanta's", 'NP$'), ('recent', 'JJ'), ('primary', 'NN'), ('election', 'NN'), ('produced', 'VBD'), ('``', '``'), ('no', 'AT'), ('evidence', 'NN'), ("''", "''"), ('that', 'CS'), ('any', 'DTI'), ('irregularities', 'NNS'), ('took', 'VBD'), ('place', 'NN'), ('.', '.'), ('The', 'AT'), ('jury', 'NN'), ('further', 'RBR'), ('said', 'VBD'), ('in', 'IN'), ('term-end', 'NN'), ('presentments', 'NNS'), ('that', 'CS'), ('the', 'AT'), ('City', 'NN-TL').....

2 个答案:

答案 0 :(得分:4)

无法从你给我们的内容中得知,但你是否尝试过一步一步解决问题?似乎t.split('/')[1] == 'NP'在任何情况下都不会评估为True。所以你应该从:

开始
  1. 打印/调试以查看f.split()包含的确切内容
  2. 确保你的条件实际上是正确的,从你给出的小输出样本中我看到你正在寻找更多:if t.split('/')[1].startswith('NP')但是无法说出来。
  3. 编辑:

    好的,首先,如果这确实是f.split()打印给你的那么你应该得到一个异常sicne t是一个元组而一个元组没有split()方法。所以你让我很好奇,我安装了nltk并下载了“棕色”语料库并尝试了你的代码。首先,如果我这样做,对我来说:

      import nltk
      from nltk.corpus import brown
      f = brown.raw('ca01')
      print f.split()
    
      ['The/at', 'Fulton/np-tl', 'County/nn-tl', 'Grand/jj-tl', 'Jury/nn-tl', 'said/vbd', 'Friday/nr', 'an/at', 'investigation/nn', 'of/in', "Atlanta's/np$", 'recent/jj', 'primary/nn', 'election/nn', 'produced/vbd', '``/``', 'no/at', 'evidence/nn', "''/''", 'that/cs', 'any/dti', 'irregularities/nns', 'took/vbd', 'place/nn', './.', 'The/at', 'jury/nn', 'further/rbr', 'said/vbd', 'in/in', 'term-end/nn', 'presentments/nns', 'that/cs', 'the/at', 'City/nn-tl', 'Executive/jj-tl', 'Committee/nn-tl', ',/,', 'which/wdt', 'had/hvd', 'over-all/jj', 'charge/nn', 'of/in', 'the/at', 'election/nn', ',/,', '``/``', 'deserves/vbz', 'the/at', 'praise/nn', 'and/cc', 'thanks/nns', 'of/in', 'the/at', 'City/nn-tl' .....]
    

    所以我不知道你在那里做了什么来获得结果,但这是不正确的。现在您可以从组中看到,单词的第二部分是小写的,这就是您的代码失败的原因。所以如果你这样做:

    w=[nltk.tag.str2tuple(t) for t in f.split() if t.split('/')[1].lower() == 'np']
    

    这会得到结果:

    [('September-October', 'NP'), ('Durwood', 'NP'), ('Pye', 'NP'), ('Ivan', 'NP'), ('Allen', 'NP'), ('Jr.', 'NP'), ('Fulton', 'NP'), ('Atlanta', 'NP'), ('Fulton', 'NP'), ('Fulton', 'NP'), ('Jan.', 'NP'), ('Fulton', 'NP'), ('Bellwood', 'NP'), ('Alpharetta', 'NP'), ('William', 'NP'), ('B.', 'NP'), ('Hartsfield', 'NP'), ('Pearl', 'NP'), ('Williams', 'NP'), ('Hartsfield', 'NP'), ('Aug.', 'NP'), ('William', 'NP'), ('Berry', 'NP'), ('Jr.', 'NP'), ('Mrs.', 'NP'), ('J.', 'NP'), ('M.', 'NP'), ('Cheshire', 'NP'), ('Griffin', 'NP'), ('Opelika', 'NP'), ('Ala.', 'NP'), ('Hartsfield', 'NP'), ('E.', 'NP'), ('Pelham', 'NP'), ('Henry', 'NP'), ('L.', 'NP'), ('Bowden', 'NP'), ('Hartsfield', 'NP'), ('Atlanta', 'NP'), ('Jan.', 'NP'), ('Ivan', 'NP'), ....]
    

    现在为了将来的参考仔细检查,你发布的信息就像我要求的那样,只是因为如果它不正确那么它是误导性的,它既不会帮助那些试图帮助你的人,也不会帮助你自己。不是批评者,而是建设性的建议:)

答案 1 :(得分:0)

有人认为t.split('/')[1] == 'NP'总是评估为假。