mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-25 17:36:30 +03:00
Add Token.lex
This commit is contained in:
parent
933a7cf8d1
commit
c099f6eece
|
@ -2,6 +2,7 @@ import pytest
|
||||||
import numpy
|
import numpy
|
||||||
from spacy.tokens import Doc, Span
|
from spacy.tokens import Doc, Span
|
||||||
from spacy.vocab import Vocab
|
from spacy.vocab import Vocab
|
||||||
|
from spacy.lexeme import Lexeme
|
||||||
from spacy.lang.en import English
|
from spacy.lang.en import English
|
||||||
from spacy.attrs import ENT_TYPE, ENT_IOB, SENT_START, HEAD, DEP, MORPH
|
from spacy.attrs import ENT_TYPE, ENT_IOB, SENT_START, HEAD, DEP, MORPH
|
||||||
|
|
||||||
|
@ -389,3 +390,11 @@ def test_doc_lang(en_vocab):
|
||||||
assert doc.lang == en_vocab.strings["en"]
|
assert doc.lang == en_vocab.strings["en"]
|
||||||
assert doc[0].lang_ == "en"
|
assert doc[0].lang_ == "en"
|
||||||
assert doc[0].lang == en_vocab.strings["en"]
|
assert doc[0].lang == en_vocab.strings["en"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_token_lexeme(en_vocab):
|
||||||
|
"""Test that tokens expose their lexeme."""
|
||||||
|
token = Doc(en_vocab, words=["Hello", "world"])[0]
|
||||||
|
assert isinstance(token.lex, Lexeme)
|
||||||
|
assert token.lex.text == token.text
|
||||||
|
assert en_vocab[token.orth] == token.lex
|
||||||
|
|
|
@ -226,6 +226,11 @@ cdef class Token:
|
||||||
cdef hash_t key = self.vocab.morphology.add(features)
|
cdef hash_t key = self.vocab.morphology.add(features)
|
||||||
self.c.morph = key
|
self.c.morph = key
|
||||||
|
|
||||||
|
@property
|
||||||
|
def lex(self):
|
||||||
|
"""RETURNS (Lexeme): The underlying lexeme."""
|
||||||
|
return self.vocab[self.c.lex.orth]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def lex_id(self):
|
def lex_id(self):
|
||||||
"""RETURNS (int): Sequential ID of the token's lexical type."""
|
"""RETURNS (int): Sequential ID of the token's lexical type."""
|
||||||
|
|
|
@ -392,73 +392,74 @@ The L2 norm of the token's vector representation.
|
||||||
|
|
||||||
## Attributes {#attributes}
|
## Attributes {#attributes}
|
||||||
|
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
| -------------------------------------------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| -------------------------------------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `doc` | `Doc` | The parent document. |
|
| `doc` | `Doc` | The parent document. |
|
||||||
| `sent` <Tag variant="new">2.0.12</Tag> | `Span` | The sentence span that this token is a part of. |
|
| `lex` <Tag variant="new">3</Tag> | [`Lexeme`](/api/lexeme) | The underlying lexeme. |
|
||||||
| `text` | str | Verbatim text content. |
|
| `sent` <Tag variant="new">2.0.12</Tag> | `Span` | The sentence span that this token is a part of. |
|
||||||
| `text_with_ws` | str | Text content, with trailing space character if present. |
|
| `text` | str | Verbatim text content. |
|
||||||
| `whitespace_` | str | Trailing space character if present. |
|
| `text_with_ws` | str | Text content, with trailing space character if present. |
|
||||||
| `orth` | int | ID of the verbatim text content. |
|
| `whitespace_` | str | Trailing space character if present. |
|
||||||
| `orth_` | str | Verbatim text content (identical to `Token.text`). Exists mostly for consistency with the other attributes. |
|
| `orth` | int | ID of the verbatim text content. |
|
||||||
| `vocab` | `Vocab` | The vocab object of the parent `Doc`. |
|
| `orth_` | str | Verbatim text content (identical to `Token.text`). Exists mostly for consistency with the other attributes. |
|
||||||
| `tensor` <Tag variant="new">2.1.7</Tag> | `ndarray` | The tokens's slice of the parent `Doc`'s tensor. |
|
| `vocab` | `Vocab` | The vocab object of the parent `Doc`. |
|
||||||
| `head` | `Token` | The syntactic parent, or "governor", of this token. |
|
| `tensor` <Tag variant="new">2.1.7</Tag> | `ndarray` | The tokens's slice of the parent `Doc`'s tensor. |
|
||||||
| `left_edge` | `Token` | The leftmost token of this token's syntactic descendants. |
|
| `head` | `Token` | The syntactic parent, or "governor", of this token. |
|
||||||
| `right_edge` | `Token` | The rightmost token of this token's syntactic descendants. |
|
| `left_edge` | `Token` | The leftmost token of this token's syntactic descendants. |
|
||||||
| `i` | int | The index of the token within the parent document. |
|
| `right_edge` | `Token` | The rightmost token of this token's syntactic descendants. |
|
||||||
| `ent_type` | int | Named entity type. |
|
| `i` | int | The index of the token within the parent document. |
|
||||||
| `ent_type_` | str | Named entity type. |
|
| `ent_type` | int | Named entity type. |
|
||||||
| `ent_iob` | int | IOB code of named entity tag. `3` means the token begins an entity, `2` means it is outside an entity, `1` means it is inside an entity, and `0` means no entity tag is set. |
|
| `ent_type_` | str | Named entity type. |
|
||||||
| `ent_iob_` | str | IOB code of named entity tag. "B" means the token begins an entity, "I" means it is inside an entity, "O" means it is outside an entity, and "" means no entity tag is set. |
|
| `ent_iob` | int | IOB code of named entity tag. `3` means the token begins an entity, `2` means it is outside an entity, `1` means it is inside an entity, and `0` means no entity tag is set. |
|
||||||
| `ent_kb_id` <Tag variant="new">2.2</Tag> | int | Knowledge base ID that refers to the named entity this token is a part of, if any. |
|
| `ent_iob_` | str | IOB code of named entity tag. "B" means the token begins an entity, "I" means it is inside an entity, "O" means it is outside an entity, and "" means no entity tag is set. |
|
||||||
| `ent_kb_id_` <Tag variant="new">2.2</Tag> | str | Knowledge base ID that refers to the named entity this token is a part of, if any. |
|
| `ent_kb_id` <Tag variant="new">2.2</Tag> | int | Knowledge base ID that refers to the named entity this token is a part of, if any. |
|
||||||
| `ent_id` | int | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. |
|
| `ent_kb_id_` <Tag variant="new">2.2</Tag> | str | Knowledge base ID that refers to the named entity this token is a part of, if any. |
|
||||||
| `ent_id_` | str | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. |
|
| `ent_id` | int | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. |
|
||||||
| `lemma` | int | Base form of the token, with no inflectional suffixes. |
|
| `ent_id_` | str | ID of the entity the token is an instance of, if any. Currently not used, but potentially for coreference resolution. |
|
||||||
| `lemma_` | str | Base form of the token, with no inflectional suffixes. |
|
| `lemma` | int | Base form of the token, with no inflectional suffixes. |
|
||||||
| `norm` | int | The token's norm, i.e. a normalized form of the token text. Usually set in the language's [tokenizer exceptions](/usage/adding-languages#tokenizer-exceptions) or [norm exceptions](/usage/adding-languages#norm-exceptions). |
|
| `lemma_` | str | Base form of the token, with no inflectional suffixes. |
|
||||||
| `norm_` | str | The token's norm, i.e. a normalized form of the token text. Usually set in the language's [tokenizer exceptions](/usage/adding-languages#tokenizer-exceptions) or [norm exceptions](/usage/adding-languages#norm-exceptions). |
|
| `norm` | int | The token's norm, i.e. a normalized form of the token text. Usually set in the language's [tokenizer exceptions](/usage/adding-languages#tokenizer-exceptions) or [norm exceptions](/usage/adding-languages#norm-exceptions). |
|
||||||
| `lower` | int | Lowercase form of the token. |
|
| `norm_` | str | The token's norm, i.e. a normalized form of the token text. Usually set in the language's [tokenizer exceptions](/usage/adding-languages#tokenizer-exceptions) or [norm exceptions](/usage/adding-languages#norm-exceptions). |
|
||||||
| `lower_` | str | Lowercase form of the token text. Equivalent to `Token.text.lower()`. |
|
| `lower` | int | Lowercase form of the token. |
|
||||||
| `shape` | int | Transform of the tokens's string, to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. |
|
| `lower_` | str | Lowercase form of the token text. Equivalent to `Token.text.lower()`. |
|
||||||
| `shape_` | str | Transform of the tokens's string, to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. |
|
| `shape` | int | Transform of the tokens's string, to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. |
|
||||||
| `prefix` | int | Hash value of a length-N substring from the start of the token. Defaults to `N=1`. |
|
| `shape_` | str | Transform of the tokens's string, to show orthographic features. Alphabetic characters are replaced by `x` or `X`, and numeric characters are replaced by `d`, and sequences of the same character are truncated after length 4. For example,`"Xxxx"`or`"dd"`. |
|
||||||
| `prefix_` | str | A length-N substring from the start of the token. Defaults to `N=1`. |
|
| `prefix` | int | Hash value of a length-N substring from the start of the token. Defaults to `N=1`. |
|
||||||
| `suffix` | int | Hash value of a length-N substring from the end of the token. Defaults to `N=3`. |
|
| `prefix_` | str | A length-N substring from the start of the token. Defaults to `N=1`. |
|
||||||
| `suffix_` | str | Length-N substring from the end of the token. Defaults to `N=3`. |
|
| `suffix` | int | Hash value of a length-N substring from the end of the token. Defaults to `N=3`. |
|
||||||
| `is_alpha` | bool | Does the token consist of alphabetic characters? Equivalent to `token.text.isalpha()`. |
|
| `suffix_` | str | Length-N substring from the end of the token. Defaults to `N=3`. |
|
||||||
| `is_ascii` | bool | Does the token consist of ASCII characters? Equivalent to `all(ord(c) < 128 for c in token.text)`. |
|
| `is_alpha` | bool | Does the token consist of alphabetic characters? Equivalent to `token.text.isalpha()`. |
|
||||||
| `is_digit` | bool | Does the token consist of digits? Equivalent to `token.text.isdigit()`. |
|
| `is_ascii` | bool | Does the token consist of ASCII characters? Equivalent to `all(ord(c) < 128 for c in token.text)`. |
|
||||||
| `is_lower` | bool | Is the token in lowercase? Equivalent to `token.text.islower()`. |
|
| `is_digit` | bool | Does the token consist of digits? Equivalent to `token.text.isdigit()`. |
|
||||||
| `is_upper` | bool | Is the token in uppercase? Equivalent to `token.text.isupper()`. |
|
| `is_lower` | bool | Is the token in lowercase? Equivalent to `token.text.islower()`. |
|
||||||
| `is_title` | bool | Is the token in titlecase? Equivalent to `token.text.istitle()`. |
|
| `is_upper` | bool | Is the token in uppercase? Equivalent to `token.text.isupper()`. |
|
||||||
| `is_punct` | bool | Is the token punctuation? |
|
| `is_title` | bool | Is the token in titlecase? Equivalent to `token.text.istitle()`. |
|
||||||
| `is_left_punct` | bool | Is the token a left punctuation mark, e.g. `"("` ? |
|
| `is_punct` | bool | Is the token punctuation? |
|
||||||
| `is_right_punct` | bool | Is the token a right punctuation mark, e.g. `")"` ? |
|
| `is_left_punct` | bool | Is the token a left punctuation mark, e.g. `"("` ? |
|
||||||
| `is_space` | bool | Does the token consist of whitespace characters? Equivalent to `token.text.isspace()`. |
|
| `is_right_punct` | bool | Is the token a right punctuation mark, e.g. `")"` ? |
|
||||||
| `is_bracket` | bool | Is the token a bracket? |
|
| `is_space` | bool | Does the token consist of whitespace characters? Equivalent to `token.text.isspace()`. |
|
||||||
| `is_quote` | bool | Is the token a quotation mark? |
|
| `is_bracket` | bool | Is the token a bracket? |
|
||||||
| `is_currency` <Tag variant="new">2.0.8</Tag> | bool | Is the token a currency symbol? |
|
| `is_quote` | bool | Is the token a quotation mark? |
|
||||||
| `like_url` | bool | Does the token resemble a URL? |
|
| `is_currency` <Tag variant="new">2.0.8</Tag> | bool | Is the token a currency symbol? |
|
||||||
| `like_num` | bool | Does the token represent a number? e.g. "10.9", "10", "ten", etc. |
|
| `like_url` | bool | Does the token resemble a URL? |
|
||||||
| `like_email` | bool | Does the token resemble an email address? |
|
| `like_num` | bool | Does the token represent a number? e.g. "10.9", "10", "ten", etc. |
|
||||||
| `is_oov` | bool | Does the token have a word vector? |
|
| `like_email` | bool | Does the token resemble an email address? |
|
||||||
| `is_stop` | bool | Is the token part of a "stop list"? |
|
| `is_oov` | bool | Does the token have a word vector? |
|
||||||
| `pos` | int | Coarse-grained part-of-speech from the [Universal POS tag set](https://universaldependencies.org/docs/u/pos/). |
|
| `is_stop` | bool | Is the token part of a "stop list"? |
|
||||||
| `pos_` | str | Coarse-grained part-of-speech from the [Universal POS tag set](https://universaldependencies.org/docs/u/pos/). |
|
| `pos` | int | Coarse-grained part-of-speech from the [Universal POS tag set](https://universaldependencies.org/docs/u/pos/). |
|
||||||
| `tag` | int | Fine-grained part-of-speech. |
|
| `pos_` | str | Coarse-grained part-of-speech from the [Universal POS tag set](https://universaldependencies.org/docs/u/pos/). |
|
||||||
| `tag_` | str | Fine-grained part-of-speech. |
|
| `tag` | int | Fine-grained part-of-speech. |
|
||||||
| `morph` | `MorphAnalysis` | Morphological analysis. |
|
| `tag_` | str | Fine-grained part-of-speech. |
|
||||||
| `morph_` | str | Morphological analysis in UD FEATS format. |
|
| `morph` | `MorphAnalysis` | Morphological analysis. |
|
||||||
| `dep` | int | Syntactic dependency relation. |
|
| `morph_` | str | Morphological analysis in UD FEATS format. |
|
||||||
| `dep_` | str | Syntactic dependency relation. |
|
| `dep` | int | Syntactic dependency relation. |
|
||||||
| `lang` | int | Language of the parent document's vocabulary. |
|
| `dep_` | str | Syntactic dependency relation. |
|
||||||
| `lang_` | str | Language of the parent document's vocabulary. |
|
| `lang` | int | Language of the parent document's vocabulary. |
|
||||||
| `prob` | float | Smoothed log probability estimate of token's word type (context-independent entry in the vocabulary). |
|
| `lang_` | str | Language of the parent document's vocabulary. |
|
||||||
| `idx` | int | The character offset of the token within the parent document. |
|
| `prob` | float | Smoothed log probability estimate of token's word type (context-independent entry in the vocabulary). |
|
||||||
| `sentiment` | float | A scalar value indicating the positivity or negativity of the token. |
|
| `idx` | int | The character offset of the token within the parent document. |
|
||||||
| `lex_id` | int | Sequential ID of the token's lexical type, used to index into tables, e.g. for word vectors. |
|
| `sentiment` | float | A scalar value indicating the positivity or negativity of the token. |
|
||||||
| `rank` | int | Sequential ID of the token's lexical type, used to index into tables, e.g. for word vectors. |
|
| `lex_id` | int | Sequential ID of the token's lexical type, used to index into tables, e.g. for word vectors. |
|
||||||
| `cluster` | int | Brown cluster ID. |
|
| `rank` | int | Sequential ID of the token's lexical type, used to index into tables, e.g. for word vectors. |
|
||||||
| `_` | `Underscore` | User space for adding custom [attribute extensions](/usage/processing-pipelines#custom-components-attributes). |
|
| `cluster` | int | Brown cluster ID. |
|
||||||
|
| `_` | `Underscore` | User space for adding custom [attribute extensions](/usage/processing-pipelines#custom-components-attributes). |
|
||||||
|
|
Loading…
Reference in New Issue
Block a user