spaCy - util.compile_prefix_regex



此實用程式函式將一系列字首規則編譯到 regex 物件中。

引數

下表說明了其引數 -

名稱 型別 說明
entries 元組 此引數表示字首規則。例如,lang.punctuation.TOKENIZER_PREFIXES</>.

語法

prefixes = ("§", "%", "=", r"+")
prefix_reg = spacy.util.compile_prefix_regex(prefixes)
nlp.tokenizer.prefix_search = prefix_reg.search

示例

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

doc = nlp("[A] works for [B] in [C].")
print([t.text for t in doc])
# ['[A]', 'works', 'for', '[B]', 'in', '[C]', '.']

輸出

['[A', ']', 'works', 'for', '[B', ']', 'in', '[C', ']', '.']
spacy_util_get_data_path.htm
廣告
© . All rights reserved.