spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

Can't add a tokenizer exception

Open FredRodrigues opened this issue 9 years ago • 3 comments

Hey! I am trying to add an exception at tokenizing some tokens using spacy 2.02, I know that exists .tokenizer.add_special_case() which I am using for some cases but for example a token like US$100, spacy splits in two tokens.

('US$', 'SYM'), ('100', 'NUM')`

But I want to split in three like this:

('US', 'PROPN'), ('$', 'SYM'), ('800', 'NUM')

Instead of doing a special case for each number after the us$, i want to make an exception for every token that has a forma of US$NUMBER.

I was reading about the TOKENIZER_EXCEPTIONS on the documentation of spacy but I can't figure out how to this.

I was trying to use from spacy.lang.en.tokenizer_exceptions import TOKENIZER_EXCEPTIONS and also spacy.util which have a method update_exc(), but don't do nothing.

Can someone post a full code example on how to do it?

Oh, another thing, i know that the file tokenizer_exceptions on lang.en, has already some exceptions like split "i'm" in "i" "'m", i already commented that part but that won't work. I don't want that the tokenizer split "i'm", how i can also do this ?

Info about spaCy

  • Platform: Linux-4.4.92-31-default-x86_64-with-SuSE-42.3-x86_64
  • Python version: 3.4.6
  • Models: pt_core_news_sm, pt, en_core_web_lg
  • spaCy version: 2.0.2

FredRodrigues avatar Nov 17 '17 16:11 FredRodrigues

@FredRodrigues, you can change the behavior of tokenizer by creating a custom one, like this:

 def custom_en_tokenizer(en_vocab):  
     prefixes = list(English.Defaults.prefixes)
     prefixes.remove('US\$')  # Remove exception for currencies
     prefixes.append(r'(?:US)(?=\$\d+)')  # Append new prefix-matching rule
     prefix_re = util.compile_prefix_regex(tuple(prefixes))
     suffix_re = util.compile_suffix_regex(English.Defaults.suffixes)
     infix_re = util.compile_infix_regex(English.Defaults.infixes)
     return Tokenizer(en_vocab,
                      English.Defaults.tokenizer_exceptions,
                      prefix_re.search,
                      suffix_re.search,
                      infix_re.finditer,
                      token_match=None)

tokenizer = custom_en_tokenizer(spacy.blank('en').vocab)
for token in tokenizer('US$100'):
     print(token, end=' ')
>>> US $ 100

As for your second question, I think the problem is that you are loading model using spacy.load, which unpickles tokenizer from model dir, if you want to change the behavior of tokenizer in this case, you should change exceptions and use space.blank, which creates model with tokenizer instance from code-base, not from disk.

GreenRiverRUS avatar Nov 23 '17 19:11 GreenRiverRUS

It worked! Huge thanks !

FredRodrigues avatar Nov 23 '17 20:11 FredRodrigues

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar May 08 '18 06:05 lock[bot]