在原始的、未标记的、通常杂乱的文本中匹配标记化的单词和短语。
项目描述
模块 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 match 或 conda install -c ets match
要求
文档
在这里!.
项目详情
关闭
match-0.3.2.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9ff437183587c604ffe19ed7a0d3b5cd4c5c78cb6d5e4e22b1f0e9b97befebd7 |
|
MD5 | 2f260a5feb197aa50e2864e0fe9654aa |
|
BLAKE2b-256 | 15bafd1ef510717bd032a48a6818f64cd96adef1155ac8c1db159bb572acce79 |