首页 > 解决方案 > 斯坦福 CoreNLP POS 标记法文

问题描述

我正在寻找一种使用 Python 对法语句子进行 Pos 标记的方法。我看到我们可以使用斯坦福 CoreNLP,但是在谷歌上搜索了几次之后,我没有找到可以让我满意的真实例子。如果有一段代码告诉我如何解决我的问题,那就太好了

标签: pythonnlpstanford-nlppart-of-speechfrench

解决方案


斯坦福 CoreNLP 有许多 Python 包装器。这里有一个列表(以及其他语言的包装器)。您需要先运行Stanford CoreNLP 服务器。这是一些使用pycorenlp的代码:

from pycorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP('http://localhost:9000')

text = "Ceci est un test de l'étiqueteur morpho-syntaxique du français."

output = nlp.annotate(text, properties={
  'annotators': 'tokenize, ssplit, pos',
  'outputFormat': 'json'
  })

from pprint import pprint
pprint(output)

结果是一个带有所有注释的 JSON 数据结构(您可以通过为属性指定不同的值来选择其他格式,outputFormat例如 'text'、'xml'...),包括 POS 标签(pos每个标记的属性),如下:

{'sentences': [{'index': 0,
                'tokens': [{'after': ' ',
                            'before': '',
                            'characterOffsetBegin': 0,
                            'characterOffsetEnd': 4,
                            'index': 1,
                            'originalText': 'Ceci',
                            'pos': 'NNP',
                            'word': 'Ceci'},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 5,
                            'characterOffsetEnd': 8,
                            'index': 2,
                            'originalText': 'est',
                            'pos': 'NNP',
                            'word': 'est'},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 9,
                            'characterOffsetEnd': 11,
                            'index': 3,
                            'originalText': 'un',
                            'pos': 'JJ',
                            'word': 'un'},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 12,
                            'characterOffsetEnd': 16,
                            'index': 4,
                            'originalText': 'test',
                            'pos': 'NN',
                            'word': 'test'},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 17,
                            'characterOffsetEnd': 19,
                            'index': 5,
                            'originalText': 'de',
                            'pos': 'IN',
                            'word': 'de'},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 20,
                            'characterOffsetEnd': 32,
                            'index': 6,
                            'originalText': "l'étiqueteur",
                            'pos': 'JJ',
                            'word': "l'étiqueteur"},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 33,
                            'characterOffsetEnd': 50,
                            'index': 7,
                            'originalText': 'morpho-syntaxique',
                            'pos': 'JJ',
                            'word': 'morpho-syntaxique'},
                           {'after': ' ',
                            'before': ' ',
                            'characterOffsetBegin': 51,
                            'characterOffsetEnd': 53,
                            'index': 8,
                            'originalText': 'du',
                            'pos': 'NNP',
                            'word': 'du'},
                           {'after': '',
                            'before': ' ',
                            'characterOffsetBegin': 54,
                            'characterOffsetEnd': 62,
                            'index': 9,
                            'originalText': 'français',
                            'pos': 'NN',
                            'word': 'français'},
                           {'after': '',
                            'before': '',
                            'characterOffsetBegin': 62,
                            'characterOffsetEnd': 63,
                            'index': 10,
                            'originalText': '.',
                            'pos': '.',
                            'word': '.'}]}]}

推荐阅读