2017-05-26 10:45:01 +00:00
|
|
|
|
//- 💫 DOCS > USAGE > SPACY 101 > VOCAB & STRINGSTORE
|
|
|
|
|
|
|
|
|
|
p
|
|
|
|
|
| Whenever possible, spaCy tries to store data in a vocabulary, the
|
|
|
|
|
| #[+api("vocab") #[code Vocab]], that will be
|
|
|
|
|
| #[strong shared by multiple documents]. To save memory, spaCy also
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| encodes all strings to #[strong hash values] – in this case for example,
|
|
|
|
|
| "coffee" has the hash #[code 3197928453018144401L]. Entity labels like
|
|
|
|
|
| "ORG" and part-of-speech tags like "VERB" are also encoded. Internally,
|
|
|
|
|
| spaCy only "speaks" in hash values.
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
+aside
|
|
|
|
|
| #[strong Token]: A word, punctuation mark etc. #[em in context], including
|
|
|
|
|
| its attributes, tags and dependencies.#[br]
|
|
|
|
|
| #[strong Lexeme]: A "word type" with no context. Includes the word shape
|
|
|
|
|
| and flags, e.g. if it's lowercase, a digit or punctuation.#[br]
|
|
|
|
|
| #[strong Doc]: A processed container of tokens in context.#[br]
|
|
|
|
|
| #[strong Vocab]: The collection of lexemes.#[br]
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| #[strong StringStore]: The dictionary mapping hash values to strings, for
|
|
|
|
|
| example #[code 3197928453018144401L] → "coffee".
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
+image
|
|
|
|
|
include ../../../assets/img/docs/vocab_stringstore.svg
|
|
|
|
|
.u-text-right
|
|
|
|
|
+button("/assets/img/docs/vocab_stringstore.svg", false, "secondary").u-text-tag View large graphic
|
|
|
|
|
|
|
|
|
|
p
|
|
|
|
|
| If you process lots of documents containing the word "coffee" in all
|
|
|
|
|
| kinds of different contexts, storing the exact string "coffee" every time
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| would take up way too much space. So instead, spaCy hashes the string
|
2017-05-26 10:45:01 +00:00
|
|
|
|
| and stores it in the #[+api("stringstore") #[code StringStore]]. You can
|
|
|
|
|
| think of the #[code StringStore] as a
|
|
|
|
|
| #[strong lookup table that works in both directions] – you can look up a
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| string to get its hash, or a hash to get its string:
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
+code.
|
|
|
|
|
doc = nlp(u'I like coffee')
|
2017-05-28 16:19:11 +00:00
|
|
|
|
assert doc.vocab.strings[u'coffee'] == 3197928453018144401L
|
|
|
|
|
assert doc.vocab.strings[3197928453018144401L] == u'coffee'
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
p
|
|
|
|
|
| Now that all strings are encoded, the entries in the vocabulary
|
|
|
|
|
| #[strong don't need to include the word text] themselves. Instead,
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| they can look it up in the #[code StringStore] via its hash value. Each
|
2017-05-26 10:45:01 +00:00
|
|
|
|
| entry in the vocabulary, also called #[+api("lexeme") #[code Lexeme]],
|
|
|
|
|
| contains the #[strong context-independent] information about a word.
|
|
|
|
|
| For example, no matter if "love" is used as a verb or a noun in some
|
|
|
|
|
| context, its spelling and whether it consists of alphabetic characters
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| won't ever change. Its hash value will also always be the same.
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
+code.
|
|
|
|
|
for word in doc:
|
|
|
|
|
lexeme = doc.vocab[word.text]
|
|
|
|
|
print(lexeme.text, lexeme.orth, lexeme.shape_, lexeme.prefix_, lexeme.suffix_,
|
|
|
|
|
lexeme.is_alpha, lexeme.is_digit, lexeme.is_title, lexeme.lang_)
|
|
|
|
|
|
|
|
|
|
+aside
|
|
|
|
|
| #[strong Text]: The original text of the lexeme.#[br]
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| #[strong Orth]: The hash value of the lexeme.#[br]
|
2017-05-26 10:45:01 +00:00
|
|
|
|
| #[strong Shape]: The abstract word shape of the lexeme.#[br]
|
|
|
|
|
| #[strong Prefix]: By default, the first letter of the word string.#[br]
|
|
|
|
|
| #[strong Suffix]: By default, the last three letters of the word string.#[br]
|
|
|
|
|
| #[strong is alpha]: Does the lexeme consist of alphabetic characters?#[br]
|
|
|
|
|
| #[strong is digit]: Does the lexeme consist of digits?#[br]
|
|
|
|
|
|
2017-05-28 16:19:11 +00:00
|
|
|
|
+table(["text", "orth", "shape", "prefix", "suffix", "is_alpha", "is_digit"])
|
|
|
|
|
- var style = [0, 1, 1, 0, 0, 1, 1]
|
|
|
|
|
+annotation-row(["I", "4690420944186131903L", "X", "I", "I", true, false], style)
|
|
|
|
|
+annotation-row(["love", "3702023516439754181L", "xxxx", "l", "ove", true, false], style)
|
|
|
|
|
+annotation-row(["coffee", "3197928453018144401L", "xxxx", "c", "ffe", true, false], style)
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
p
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| The mapping of words to hashes doesn't depend on any state. To make sure
|
|
|
|
|
| each value is unique, spaCy uses a
|
|
|
|
|
| #[+a("https://en.wikipedia.org/wiki/Hash_function") hash function] to
|
|
|
|
|
| calculate the hash #[strong based on the word string]. This also means
|
|
|
|
|
| that the hash for "coffee" will always be the same, no matter which model
|
|
|
|
|
| you're using or how you've configured spaCy.
|
|
|
|
|
|
|
|
|
|
p
|
|
|
|
|
| However, hashes #[strong cannot be reversed] and there's no way to
|
|
|
|
|
| resolve #[code 3197928453018144401L] back to "coffee". All spaCy can do
|
|
|
|
|
| is look it up in the vocabulary. That's why you always need to make
|
|
|
|
|
| sure all objects you create have access to the same vocabulary. If they
|
|
|
|
|
| don't, spaCy might not be able to find the strings it needs.
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
+code.
|
|
|
|
|
from spacy.tokens import Doc
|
|
|
|
|
from spacy.vocab import Vocab
|
|
|
|
|
|
|
|
|
|
doc = nlp(u'I like coffee') # original Doc
|
2017-05-28 16:19:11 +00:00
|
|
|
|
assert doc.vocab.strings[u'coffee'] == 3197928453018144401L # get hash
|
|
|
|
|
assert doc.vocab.strings[3197928453018144401L] == u'coffee' # 👍
|
|
|
|
|
|
|
|
|
|
empty_doc = Doc(Vocab()) # new Doc with empty Vocab
|
|
|
|
|
# doc.vocab.strings[3197928453018144401L] will raise an error :(
|
|
|
|
|
|
|
|
|
|
empty_doc.vocab.strings.add(u'coffee') # add "coffee" and generate hash
|
|
|
|
|
assert doc.vocab.strings[3197928453018144401L] == u'coffee' # 👍
|
|
|
|
|
|
|
|
|
|
new_doc = Doc(doc.vocab) # create new doc with first doc's vocab
|
|
|
|
|
assert doc.vocab.strings[3197928453018144401L] == u'coffee' # 👍
|
2017-05-26 10:45:01 +00:00
|
|
|
|
|
|
|
|
|
p
|
2017-05-28 21:26:13 +00:00
|
|
|
|
| If the vocabulary doesn't contain a hash for "coffee", spaCy will
|
2017-05-28 16:19:11 +00:00
|
|
|
|
| throw an error. So you either need to add it manually, or initialise the
|
2017-05-28 21:26:13 +00:00
|
|
|
|
| new #[code Doc] with the shared vocabulary. To prevent this problem,
|
|
|
|
|
| spaCy will also export the #[code Vocab] when you save a
|
|
|
|
|
| #[code Doc] or #[code nlp] object.
|