从带有特定单词的 POS 标记语料库中提取句子,标记组合



我正在玩棕色语料库,特别是"新闻"中标记的句子。我发现"TO"是单词标签最模糊的单词(TO,IN,TO-HL,IN-HL,IN-TL,NPS)。我正在尝试编写一个代码,该代码将为与"to"关联的每个标签从语料库中打印一个句子。这些句子不需要"清理"标签,而只包含"to"和每个关联的pos-tag

brown_sents = nltk.corpus.brown.tagged_sents(categories="news")
for sent in brown_sents:
    for word, tag in sent:
        if (word == 'to' and tag == "IN"):
            print sent

我只用其中一个 pos-tag 尝试了上面的代码,看看它是否有效,但它打印了它的所有实例。我需要它只打印与单词匹配的第一个找到的句子,标签,然后停止。我试过这个:

for sent in brown_sents:
    for word, tag in sent:
        if (word == 'to' and tag == 'IN'):
            print sent
        if (word != 'to' and tag != 'IN'):
            break

这适用于这个pos-tag,因为它是第一个与"to"相关的标签,但是如果我使用:

for sent in brown_sents:
    for word, tag in sent:
        if (word == 'to' and tag == 'TO-HL'):
            print sent
        if (word != 'to' and tag != 'TO-HL'):
            break

它不返回任何内容。我想我离得很近——愿意帮忙吗?

您可以继续添加到当前代码中,但代码没有考虑以下事项:

  • 如果"to"在具有相同或不同 POS 的句子中多次出现,会发生什么?
  • 如果您使用相同的POS在句子中出现两次,您是否希望该句子打印两次?
  • 如果"to"出现在句子的第一个单词中并且大写,会发生什么情况?

如果你想坚持你的代码,试试这个:

from nltk.corpus import brown
brown_sents = brown.tagged_sents(categories="news")
def to_pos_sent(pos):
    for sent in brown_sents:
        for word, tag in sent:
            if word == 'to' and tag == pos:
                yield sent
for sent in to_pos_sent('TO'):
    print sent
for sent in to_pos_sent('IN'):
    print sent

我建议您将句子存储在defaultdict(list)中,然后您可以随时检索它们。

from nltk.corpus import brown
from collections import Counter, defaultdict
sents_with_to = defaultdict(list)
to_counts = Counter()
for i, sent in enumerate(brown.tagged_sents(categories='news')):
    # Check if 'to' is in sentence.
    uniq_words = dict(sent)
    if 'to' in uniq_words or 'To' in uniq_words:
        # Iterate through the sentence to find 'to'
        for word, pos in sent:
            if word.lower()=='to':
                # Flatten the sentence into a string
                sents_with_to[pos].append(sent)
                to_counts[pos]+=1

for pos in sents_with_to:
    for sent in sents_with_to[pos]:
        print pos, sent

要访问特定 POS 的句子,请执行以下操作:

for sent in sents_with_to['TO']:
    print sent

您会意识到,如果带有特定POS的"to"在句子中出现两次。它在sents_with_to[pos]中记录了两次。如果要删除它们,请尝试:

sents_with_to_and_TO = set(" ".join(["#".join(word, pos) for word, pos in sent] for sent in sents_with_to['TO']))

关于为什么这不起作用:

for sent in brown_sents:
    for word, tag in sent:
        if (word == 'to' and tag == 'TO-HL'):
            print sent
        if (word != 'to' and tag != 'TO-HL'):
            break

在解释之前,您的代码并没有真正接近您想要的输出。这是因为您的if-else语句并没有真正满足您的需求。

首先,您需要了解多个条件(即"如果")在做什么。

# Loop through the sentence
for sent in brown_sents:
  # Loop through each word with its POS
  for word, tag in sent:
    # For each sentence checks whether word and tag is in sentence:
    if word == 'to' and tag == 'TO-HL':
      print sent # If the condition is true, print sent
    # After checking the first if, you continue to check the second if
    # if word is not 'to' and tag is not 'TO-HL', 
    # you want to break out of the sentence. Note that you are still
    # in the same iteration as the previous condition.
   if word != 'to' and tag != 'TO-HL':
     break

现在让我们从一些基本的if-else语句开始:

>>> from nltk.corpus import brown
>>> first_sent = brown.tagged_sents()[0]
>>> first_sent
[(u'The', u'AT'), (u'Fulton', u'NP-TL'), (u'County', u'NN-TL'), (u'Grand', u'JJ-TL'), (u'Jury', u'NN-TL'), (u'said', u'VBD'), (u'Friday', u'NR'), (u'an', u'AT'), (u'investigation', u'NN'), (u'of', u'IN'), (u"Atlanta's", u'NP$'), (u'recent', u'JJ'), (u'primary', u'NN'), (u'election', u'NN'), (u'produced', u'VBD'), (u'``', u'``'), (u'no', u'AT'), (u'evidence', u'NN'), (u"''", u"''"), (u'that', u'CS'), (u'any', u'DTI'), (u'irregularities', u'NNS'), (u'took', u'VBD'), (u'place', u'NN'), (u'.', u'.')]
>>> for word, pos in first_sent:
...     if word != 'to' and pos != 'TO-HL':
...             break
...     else:
...             print 'say hi'
... 
>>> 

从上面的例子中,我们遍历了 sentnece 中的每个单词 + POS,在对单词位置上,if条件将检查它是否不是单词"to"而不是 pos "TO-HL",如果是这种情况,它会中断并且永远不会say hi给你。

因此,如果您将代码保持在if-else条件下,您将始终中断而不继续循环,因为to不是句子中的第一个单词,并且匹配的 pos 不正确。

实际上,您的if条件是尝试检查每个单词是否都是"to"以及其POS标签是否为" TO-HL"。


您要做的是检查:

    句子中是否包含"to",
  1. 而不是每个单词是否都是"to",然后检查
  2. 句子中的"to"是否包含您要查找的 POS 标签

因此,条件 (1) 所需的if条件是:

>>> from nltk.corpus import brown
>>> three_sents = brown.tagged_sents()[:3]
>>> for sent in three_sents:
...     if 'to' in dict(sent):
...             print sent
... 
[(u'The', u'AT'), (u'September-October', u'NP'), (u'term', u'NN'), (u'jury', u'NN'), (u'had', u'HVD'), (u'been', u'BEN'), (u'charged', u'VBN'), (u'by', u'IN'), (u'Fulton', u'NP-TL'), (u'Superior', u'JJ-TL'), (u'Court', u'NN-TL'), (u'Judge', u'NN-TL'), (u'Durwood', u'NP'), (u'Pye', u'NP'), (u'to', u'TO'), (u'investigate', u'VB'), (u'reports', u'NNS'), (u'of', u'IN'), (u'possible', u'JJ'), (u'``', u'``'), (u'irregularities', u'NNS'), (u"''", u"''"), (u'in', u'IN'), (u'the', u'AT'), (u'hard-fought', u'JJ'), (u'primary', u'NN'), (u'which', u'WDT'), (u'was', u'BEDZ'), (u'won', u'VBN'), (u'by', u'IN'), (u'Mayor-nominate', u'NN-TL'), (u'Ivan', u'NP'), (u'Allen', u'NP'), (u'Jr.', u'NP'), (u'.', u'.')]

现在您知道if 'to' in dict(sent)检查句子中是否包含"to"。

然后检查条件 (2):

>>> for sent in three_sents:
...     if 'to' in dict(sent):
...             if dict(sent)['to'] == 'TO':
...                     print sent
... 
[(u'The', u'AT'), (u'September-October', u'NP'), (u'term', u'NN'), (u'jury', u'NN'), (u'had', u'HVD'), (u'been', u'BEN'), (u'charged', u'VBN'), (u'by', u'IN'), (u'Fulton', u'NP-TL'), (u'Superior', u'JJ-TL'), (u'Court', u'NN-TL'), (u'Judge', u'NN-TL'), (u'Durwood', u'NP'), (u'Pye', u'NP'), (u'to', u'TO'), (u'investigate', u'VB'), (u'reports', u'NNS'), (u'of', u'IN'), (u'possible', u'JJ'), (u'``', u'``'), (u'irregularities', u'NNS'), (u"''", u"''"), (u'in', u'IN'), (u'the', u'AT'), (u'hard-fought', u'JJ'), (u'primary', u'NN'), (u'which', u'WDT'), (u'was', u'BEDZ'), (u'won', u'VBN'), (u'by', u'IN'), (u'Mayor-nominate', u'NN-TL'), (u'Ivan', u'NP'), (u'Allen', u'NP'), (u'Jr.', u'NP'), (u'.', u'.')]
>>> for sent in three_sents:
...     if 'to' in dict(sent):
...             if dict(sent)['to'] == 'TO-HL':
...                     print sent
... 
>>> 

现在您看到if dict(sent)['to'] == 'TO-HL'在您检查if 'to' in dict(sent)控制条件以检查 pos 限制之后。

但是您意识到,如果您在句子中有 2 个"to"dict(sent)['to']则只能捕获最后一个"to"的 POS。这就是为什么您需要上一个答案中建议的defaultdict(list)

真的没有干净的方法来执行检查,最有效的方法在前面的答案中描述,叹息。

最新更新