python - 斯坦福 CoreNLP POS 标记法文
问题描述
我正在寻找一种使用 Python 对法语句子进行 Pos 标记的方法。我看到我们可以使用斯坦福 CoreNLP,但是在谷歌上搜索了几次之后,我没有找到可以让我满意的真实例子。如果有一段代码告诉我如何解决我的问题,那就太好了
解决方案
斯坦福 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': '.'}]}]}
推荐阅读
- python-3.x - 在 Pyqt4 中用不同的 QMainWindow 刷新 QMainWindow
- c# - 优先级队列删除具有相同优先级的项目第一个进入
- git - 无法合并存储库中不相关的历史记录 - 我可以在新存储库中重新启动吗?
- java - 进程失败,退出代码为 1
- java - TextArea 不会显示用户输入
- scala - 在 RDD 上使用 take 方法时,Apache Spark 引发反序列化错误
- python - 为 GKE 身份验证生成的令牌缺少权限
- pandas - 如何从返回元组的向量函数中填充两列(col_a,col_b)
- multithreading - 新线程执行前的多线程、内存顺序和函数调用
- android - 布局视图在滚动后不包裹内容