Как я могу заставить Spacy перестать разбивать дефисные числа и слова на отдельные токены? - PullRequest
2 голосов
/ 30 января 2020

Спасибо за поиск. Я использую spaCy для распознавания именованных объектов в блоке текста, и у меня возникла особая проблема, которую я не могу решить. Вот пример кода:

from spacy.tokenizer import Tokenizer
nlp = spacy.load("en_core_web_sm")

doc = nlp('The Indo-European Caucus won the all-male election 58-32.')

Это приводит к следующему:

['The', 'Indo', '-', 'European', 'Caucus', 'won', 'the', 'all', '-', 'male', 'election', ',', '58', '-', '32', '.']

Мои проблемы в том, что мне нужны те слова и числа, которые содержат дефисы, чтобы проходить как одиночные токены. Я следовал примерам, приведенным на этом ответе , используя следующий код:

inf = list(nlp.Defaults.infixes)
inf = [x for x in inf if '-|–|—|--|---|——|~' not in x] # remove the hyphen-between-letters pattern from infix patterns
infix_re = compile_infix_regex(tuple(inf))

def custom_tokenizer(nlp):
    return Tokenizer(nlp.vocab, prefix_search=nlp.tokenizer.prefix_search,
                                suffix_search=nlp.tokenizer.suffix_search,
                                infix_finditer=infix_re.finditer,
                                token_match=nlp.tokenizer.token_match,
                                rules=nlp.Defaults.tokenizer_exceptions)

nlp.tokenizer = custom_tokenizer(nlp)

Это помогло с алфавитными символами c, и я получил это:

['The', 'Indo-European', 'Caucus', 'won', 'the', 'all-male', 'election', ',', '58', '-', '32', '.']

Это было намного лучше, но '58-32' все еще был разделен на отдельные токены. Я попытался этот ответ и получил обратный эффект:

['The', 'Indo', '-', 'European', 'Caucus', 'won', 'the', 'all', '-', 'male', 'election', ',' '58-32', '.']

Как я могу изменить токенизатор, чтобы дать мне правильные результаты в обоих случаях?

1 Ответ

2 голосов
/ 31 января 2020

Вы можете объединить два решения:

import spacy
from spacy.tokenizer import Tokenizer
from spacy.util import compile_infix_regex

nlp = spacy.load("en_core_web_sm")

def custom_tokenizer(nlp):
    inf = list(nlp.Defaults.infixes)               # Default infixes
    inf.remove(r"(?<=[0-9])[+\-\*^](?=[0-9-])")    # Remove the generic op between numbers or between a number and a -
    inf = tuple(inf)                               # Convert inf to tuple
    infixes = inf + tuple([r"(?<=[0-9])[+*^](?=[0-9-])", r"(?<=[0-9])-(?=-)"])  # Add the removed rule after subtracting (?<=[0-9])-(?=[0-9]) pattern
    infixes = [x for x in infixes if '-|–|—|--|---|——|~' not in x] # Remove - between letters rule
    infix_re = compile_infix_regex(infixes)

    return Tokenizer(nlp.vocab, prefix_search=nlp.tokenizer.prefix_search,
                                suffix_search=nlp.tokenizer.suffix_search,
                                infix_finditer=infix_re.finditer,
                                token_match=nlp.tokenizer.token_match,
                                rules=nlp.Defaults.tokenizer_exceptions)

nlp.tokenizer = custom_tokenizer(nlp)
doc = nlp('The Indo-European Caucus won the all-male election 58-32.')
print([token.text for token in doc]) 

Вывод:

['The', 'Indo-European', 'Caucus', 'won', 'the', 'all-male', 'election', '58-32', '.']
...