【问题标题】:Capture words and rewrite捕获单词并重写
【发布时间】:2017-08-29 00:07:41
【问题描述】:

用 nlpnet (http://nilc.icmc.usp.br/nlpnet/index.html) 做了一个词分类器。目标是使用给定的标记器单独提取单词。

响应代码

import nlpnet
import codecs
import itertools

TAGGER = nlpnet.POSTagger('pos-pt', language='pt')


def TAGGER_txt(text):
    return (list(TAGGER.tag(text)))

with codecs.open('document.txt', encoding='utf8') as original_file:
     with codecs.open('document_teste.txt', 'w') as output_file:
          for line in original_file.readlines():
          print (line)
          words = TAGGER_txt(line)
          all_words = list(itertools.chain(*words))
          nouns = [word[0] for word in all_words if word[1]=='V']
          print (nouns)

结果

O gato esta querendo comer o ratão 
['gato', 'ratão']

【问题讨论】:

  • Edit 你的问题并展示一个超过 5 个动词的例句以及该句子的 print(TAGGER.tag(Sentence)[0].arg_structures) 的输出。
  • 输入文件的单行是否包含某个整数倍数的葡萄牙语句子?我的意思是,没有多余的词,例如从上一句的结尾或从下一句的开头?
  • @stovfl 我编辑了问题,看看是否更清楚。
  • @BillBell 一个包含多行文本、不同大小和不同单词的文本文档。
  • (1) 让我换一种方式提出我的问题:如果我读取输入文本文件的一行,那将只包含一个句子吗? (2) 请您不要给我们图片,而是在您的问题中插入文字,以便我们可以方便地复制它?

标签: python python-2.7 text url-rewriting pos-tagger


【解决方案1】:

问题:...将包含超过 N 次特定 POS 出现的句子转储到文件中


注意:假设'document.txt' 每行包含一个句子!

def is_worth_saving(tags, pos, pos_count):
    """
    :param tags:        nlpnet tags from ONE Sentence
    :param pos:         The POS to filter
    :param pos_count:   Number of 'param pos'
    :return:
        True if 'tags' contain more than 'pos_count' occurrences of 'pos'
        False otherwise
    """  
    pos_found = 0
    # Iterate tags
    for word, _pos in tags:
        if _pos == pos:
            pos_found += 1

    return pos_found >= pos_count

if __name__ == '__main__':
    with open('document.txt') as in_fh, open('document_test.txt', 'w') as out_fh:
        for sentence in in_fh:
            print('Sentence:{}'.format(sentence[:-1]))
            tags = TAGGER.tag(sentence)

            # As your Example Sentence has only **2** Verbs,
            # pass 'pos_count=2'
            if is_worth_saving(tags[0], 'V', 2):
                out_fh.write(sentence)
                print (tags[0])

输出

Sentence:O gato esta querendo comer o ratão
[(u'O', u'ART'), (u'gato', u'N'), (u'esta', u'PROADJ'), (u'querendo', u'V'), (u'comer', u'V'), (u'o', u'ART'), (u'rat', u'N')]

使用 Python 测试:3.4.2 和 2.7.9

【讨论】:

  • 我只需要捕获类 n 的单词。
  • @Jeferson:Edit 您的问题并显示您的预期输出或从#Bill Bell 那里获取答案。
  • 对不起,我不想解释我的问题。
【解决方案2】:

我认为这可能是您需要的本质。 请查看修改后的版本。

正如您在问题中所说,标记Sentence 的结果将类似于tagged。如果您只想要来自Sentence 的名词,您可以使用nouns = 之后的表达式来恢复它们。

Sentence = " O gato esta querendo comer o rato "  
tagged = [('O', 'ADJ'), ('gato', 'N'), ('esta', 'V'), ('querendo', 'V'), ('comer', 'V'), ('o', 'ADJ'), ('rato', 'N')]

nouns = [t[0] for t in tagged if t[1]=='N']

print (nouns)

输出:

['gato', 'rato']

编辑:我不清楚你想要什么。这是另一种可能性。

  • 我还没有安装 nlpnet,因为那会做很多工作而且我自己不会使用它。
  • 我用 TAGGER_txt 模拟 TAGGER.txt。
  • 我已将编码更改为 Latin-1。它用于标题和codecs.open

.

# -*- coding: Latin-1 -*-
import codecs
import itertools

def TAGGER_txt(text): ## simulate TAGGER.txt
    return [[(u'O', u'ART'), (u'gato', u'N'), (u'esta', u'PROADJ'), (u'querendo', u'V'), (u'comer', u'V'), (u'o', u'ART'), (u'ratão', u'N')]]

with codecs.open('document.txt', encoding='Latin-1') as original_file:
    with codecs.open('document_test.txt', 'w') as output_file:
        for line in original_file.readlines():
            print (line)
            words = TAGGER_txt(line)
            all_words = list(itertools.chain(*words))
            nouns = [word[0] for word in all_words if word[1]=='N']
            print (nouns)

输出:

 O gato esta querendo comer o ratão 
['gato', 'ratão']

【讨论】:

  • 我理解你的逻辑。但是在 tagged 参数中,我无法有效地应用于我的 codic。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 2019-11-02
  • 2010-10-02
  • 2021-12-04
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多