Inconsistent Tokenization
Problem Description
Spacy's default models produces different tokens for a text/doc. Lets say a document is processed using nlp = spacy.load('en') model and later you just want to tokenize another document (instead of using entire nlp object on it). The tokens produced in above ways are different and leads to inconsistent behaviour. Here is a sample code to reproduce the issue:
import spacy
from spacy.tokenizer import Tokenizer
nlp = spacy.load('en')
tokenizer = Tokenizer(nlp.vocab)
x = "Hi (Spacy)."
d = nlp(x)
nlp_toks = [tok for tok in d]
tokenizer_toks = [tok for tok in tokenizer(x)]
print("Tokens using nlp object:")
print(*nlp_toks, sep="\n")
print("\nTokens using Tokenizer object:")
print(*tokenizer_toks, sep="\n")
Output
Tokens using nlp object:
Hi
(
Spacy
)
.
Tokens using Tokenizer object:
Hi
(Spacy).
Environment
- spaCy version: 2.0.3
- Platform: Darwin-17.2.0-x86_64-i386-64bit
- Python version: 3.6.1
- Models: en
@pratapbhanu I believe there is no issue. Tokenizer itself is language-independent, which allows you to create custom tokenizers for different languages.
When you call Tokenizer(nlp.vocab) you aren't passing any tokenization rules as optional arguments. You are simply using whitespace tokenization, which is roughly similar to text.split(' ')[1]. That is why you get this result.
However, when you call nlp(x) you are actually using Tokenizer with English-specific tokenization rules. Since nlp is a Language instance, you can take a look at how tokenizer is created in language.py:
https://github.com/explosion/spaCy/blob/75d2a05c2938f412f0fae44748374e4de19cc2be/spacy/language.py#L55-L69
When creating a Tokenizer, spaCy provides several other arguments: rules, prefix_search, suffix_search, infix_finditer and token_match. These arguments make a difference as they specify additional tokenization rules (such as punctuation).
There are some global rules, which you can find under lang directory inside .py files and there are language-specific rules, which could override these rules or add something new to them and they could be found inside language-specific directories under lang/xx.
So if you want to produce similar results, you have to add additional parameters when creating a Tokenizer. By looking at spaCy's source code you could see where you can get what you need.
Useful links:
Notes:
- Any sequence of whitespace characters beyond a single space is included as a token
@fucking-signup Thanks for getting back so quickly. Got it, if I want to use the same tokenizer as used in nlp (here, an english language instance), I need to use it as nlp.tokenizer(x). May the documentation is not so clear on this point. This is from the Tokenizer api documentation:
# Construction 1
from spacy.tokenizer import Tokenizer
tokenizer = Tokenizer(nlp.vocab)
# Construction 2
from spacy.lang.en import English
tokenizer = English().Defaults.create_tokenizer(nlp)
Here if I use the Construction 2, I get the same tokenization as used in nlp', while the Construction 1` (for the reasons you mentioned) just returns tokens by splitting the text on white space.
Note that 'nlp' is an English language instance, Construction 1 is actually using 'nlp' too but still results in a language independent tokenizer.
@pratapbhanu In the first construction example, you're only passing in the English vocabulary to the tokenizer, but not the individual tokenization rules defined in the English class and its Defaults. So the tokenizer you're creating here won't have those rules available.
The API documentation is mostly intended to cover the usage and structure of the individual classes, the available methods and how they're constructed under the hood – not necessarily the ideal, recommended usage of the library. Maybe this should be made more clear.
@fucking-signup's great comment covers everything else you need to know – couldn't have said it better 👍
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.