跳转到主要内容

在原始的、未标记的、通常杂乱的文本中匹配标记化的单词和短语。

项目描述

Build Status

Latest Conda VersionLatest PyPI VersionPython Versions

模块 Match 的目的是从原始的、未标记的源中获取清洗后、标记化字符串的偏移量(以及这些偏移量之间的字符串,以供调试)。你可能觉得“这没什么大不了的”,但如果原始文本非常杂乱,特别是充满了Unicode字符,这实际上是一个非常困难的任务。

考虑一些存储在变量 original_text 中的文本

I   am writing a letter !  Sometimes,I forget to put spaces (and do weird stuff with punctuation)  ?  J'aurai une pomme, s'il vous plâit !

这应该/可能/应该被正确地标记为

[['I', 'am', 'writing', 'a', 'letter', '!'],
 ['Sometimes', ',', 'I', 'forget', 'to', 'put', 'spaces', '-LRB-', 'and', 'do', 'weird', 'stuff', 'with', 'punctuation', '-RRB-', '?'],
 ["J'aurai", 'une', 'pomme', ',', "s'il", 'vous', 'plâit', '!']]

现在

In [2]: import match

In [3]: match.match(original_text, ['-LRB-', 'and', 'do', 'weird', 'stuff', 'with', 'punctuation', '-RRB-'])
Out[3]: [(60, 97, '(and do weird stuff with punctuation)')]

In [4]: match.match(original_text, ['I', 'am', 'writing', 'a', 'letter', '!'])
Out[4]: [(0, 25, 'I   am writing a letter !')]

In [5]: match.match(original_text, ["s'il", 'vous', 'plâit', '!'])
Out[5]: [(121, 138, "s'il vous plâit !")]

match() 返回的类型是 list,因为它将返回 所有 的参数发生,无论是标记的 list 还是单个 string(单词)

In [6]: match.match(original_text, "I")
Out[6]: [(0, 1, 'I'), (37, 38, 'I')]

当传入一个单个 string 时,match() 期望这个 string 是一个单词或标记。因此

In [7]: match.match("****because,the****", "because , the")
Out[7]: []

尝试传递 "because , the".split(' '),或者更好的是,来自适当标记器的输出。

为了方便,提供了一个名为 match_lines() 的函数

In [8]: match.match_lines(original_text, [
   ...: ['-LRB-', 'and', 'do', 'weird', 'stuff', 'with', 'punctuation', '-RRB-'],
   ...: ['I', 'am', 'writing', 'a', 'letter', '!'],
   ...: "I"
   ...: ])
Out[8]:
[(0, 1, 'I'),
 (0, 25, 'I   am writing a letter !'),
 (37, 38, 'I'),
 (60, 97, '(and do weird stuff with punctuation)')]

返回的值将始终按其偏移量排序。

安装

pip install matchconda install -c ets match

要求

文档

在这里!.

项目详情


下载文件

下载适合您平台文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

match-0.3.2.tar.gz (8.9 kB 查看哈希

上传时间