Dam*_*mio 2 python tokenize spacy
我有一个句子,希望看到如下预期的标记。
Sentence: "[x] works for [y] in [z]."
Tokens: ["[", "x", "]", "works", "for", "[", "y", "]", "in", "[", "z", "]", "."]
Expected: ["[x]", "works", "for", "[y]", "in", "[z]", "."]
Run Code Online (Sandbox Code Playgroud)
如何通过自定义分词器函数来做到这一点?
您可以从分词器前缀和后缀中删除[and ],以便括号不会与相邻的分词分开:
import spacy
nlp = spacy.load('en_core_web_sm')
prefixes = list(nlp.Defaults.prefixes)
prefixes.remove('\\[')
prefix_regex = spacy.util.compile_prefix_regex(prefixes)
nlp.tokenizer.prefix_search = prefix_regex.search
suffixes = list(nlp.Defaults.suffixes)
suffixes.remove('\\]')
suffix_regex = spacy.util.compile_suffix_regex(suffixes)
nlp.tokenizer.suffix_search = suffix_regex.search
doc = nlp("[x] works for [y] in [z].")
print([t.text for t in doc])
# ['[x]', 'works', 'for', '[y]', 'in', '[z]', '.']
Run Code Online (Sandbox Code Playgroud)
相关文档在这里:
https://spacy.io/usage/linguistic-features#native-tokenizer-additions
| 归档时间: |
|
| 查看次数: |
2063 次 |
| 最近记录: |