捕捉文字并重写

2024-09-28 15:34:15 发布

您现在位置:Python中文网/ 问答频道 /正文

用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']

Tags: textimporttxtptwithlineopentagger
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,因为这将是相当多的工作,我不会使用它自己。你知道吗
  • 我模拟标签.txt带标签。你知道吗
  • 我把编码改成了拉丁文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']

Question: ... dump to a file the sentences that contain more than N occurrences of a particular POS


Note: Assuming 'document.txt' contains one Sentence per Line!

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])

Output:

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

相关问题 更多 >