spaCy/spacy/tokens/morphanalysis.pyx

424 lines
10 KiB
Cython
Raw Normal View History

2019-03-07 17:32:51 +00:00
from libc.string cimport memset
2019-03-07 11:14:53 +00:00
from ..vocab cimport Vocab
2019-03-07 17:32:51 +00:00
from ..typedefs cimport hash_t, attr_t
2019-03-08 00:38:34 +00:00
from ..morphology cimport list_features, check_feature, get_field, tag_to_json
2019-03-07 17:32:51 +00:00
from ..strings import get_string_id
2019-03-07 11:14:53 +00:00
2019-03-07 13:03:07 +00:00
2019-03-07 13:34:54 +00:00
cdef class MorphAnalysis:
"""Control access to morphological features for a token."""
2019-03-07 13:03:07 +00:00
def __init__(self, Vocab vocab, features=tuple()):
self.vocab = vocab
self.key = self.vocab.morphology.add(features)
analysis = <const MorphAnalysisC*>self.vocab.morphology.tags.get(self.key)
2019-03-07 17:32:51 +00:00
if analysis is not NULL:
self.c = analysis[0]
else:
memset(&self.c, 0, sizeof(self.c))
@classmethod
2019-03-07 17:32:51 +00:00
def from_id(cls, Vocab vocab, hash_t key):
2019-09-12 14:48:10 +00:00
"""Create a morphological analysis from a given ID."""
2019-03-07 17:32:51 +00:00
cdef MorphAnalysis morph = MorphAnalysis.__new__(MorphAnalysis, vocab)
2019-03-07 23:09:16 +00:00
morph.vocab = vocab
2019-03-07 17:32:51 +00:00
morph.key = key
analysis = <const MorphAnalysisC*>vocab.morphology.tags.get(key)
if analysis is not NULL:
morph.c = analysis[0]
else:
memset(&morph.c, 0, sizeof(morph.c))
return morph
def __contains__(self, feature):
2019-09-12 14:48:10 +00:00
"""Test whether the morphological analysis contains some feature."""
2019-03-07 17:32:51 +00:00
cdef attr_t feat_id = get_string_id(feature)
return check_feature(&self.c, feat_id)
def __iter__(self):
2019-09-12 14:48:10 +00:00
"""Iterate over the features in the analysis."""
2019-03-07 23:09:16 +00:00
cdef attr_t feature
for feature in list_features(&self.c):
yield self.vocab.strings[feature]
def __len__(self):
2019-09-12 14:48:10 +00:00
"""The number of features in the analysis."""
2019-03-07 23:09:16 +00:00
return self.c.length
def __str__(self):
2019-03-07 23:09:16 +00:00
return self.to_json()
def __repr__(self):
2019-03-07 23:09:16 +00:00
return self.to_json()
def __hash__(self):
2019-03-07 23:09:16 +00:00
return self.key
2019-03-08 00:38:34 +00:00
def get(self, unicode field):
2019-09-12 14:48:10 +00:00
"""Retrieve a feature by field."""
2019-03-09 19:55:33 +00:00
cdef int field_id = self.vocab.morphology._feat_map.attr2field[field]
2019-03-08 00:38:34 +00:00
return self.vocab.strings[get_field(&self.c, field_id)]
2019-03-07 13:03:07 +00:00
def to_json(self):
2019-09-12 14:48:10 +00:00
"""Produce a json serializable representation, which will be a list of
strings.
"""
2019-03-07 23:09:16 +00:00
return tag_to_json(&self.c)
2019-03-07 13:03:07 +00:00
@property
def is_base_form(self):
2019-03-07 17:32:51 +00:00
raise NotImplementedError
@property
def pos(self):
2019-03-07 17:32:51 +00:00
return self.c.pos
@property
def pos_(self):
2019-03-07 17:32:51 +00:00
return self.vocab.strings[self.c.pos]
2019-03-07 17:32:51 +00:00
property id:
def __get__(self):
return self.key
2019-03-07 13:03:07 +00:00
property abbr:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.abbr
2019-03-07 13:03:07 +00:00
property adp_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.adp_type
2019-03-07 13:03:07 +00:00
property adv_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.adv_type
2019-03-07 13:03:07 +00:00
property animacy:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.animacy
2019-03-07 13:03:07 +00:00
property aspect:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.aspect
2019-03-07 13:03:07 +00:00
property case:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.case
2019-03-07 13:03:07 +00:00
property conj_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.conj_type
2019-03-07 13:03:07 +00:00
property connegative:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.connegative
2019-03-07 13:03:07 +00:00
property definite:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.definite
2019-03-07 13:03:07 +00:00
property degree:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.degree
2019-03-07 13:03:07 +00:00
property derivation:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.derivation
2019-03-07 13:03:07 +00:00
property echo:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.echo
2019-03-07 13:03:07 +00:00
property foreign:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.foreign
2019-03-07 13:03:07 +00:00
property gender:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.gender
2019-03-07 13:03:07 +00:00
property hyph:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.hyph
2019-03-07 13:03:07 +00:00
property inf_form:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.inf_form
2019-03-07 13:03:07 +00:00
property mood:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.mood
2019-03-07 13:03:07 +00:00
property name_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.name_type
2019-03-07 13:03:07 +00:00
property negative:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.negative
2019-03-07 13:03:07 +00:00
2019-03-07 13:34:54 +00:00
property noun_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.noun_type
2019-03-07 13:34:54 +00:00
2019-03-07 13:03:07 +00:00
property number:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.number
2019-03-07 13:03:07 +00:00
property num_form:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.num_form
2019-03-07 13:03:07 +00:00
property num_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.num_type
2019-03-07 13:03:07 +00:00
property num_value:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.num_value
2019-03-07 13:03:07 +00:00
property part_form:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.part_form
2019-03-07 13:03:07 +00:00
property part_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.part_type
2019-03-07 13:03:07 +00:00
property person:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.person
2019-03-07 13:03:07 +00:00
property polite:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.polite
2019-03-07 13:03:07 +00:00
property polarity:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.polarity
2019-03-07 13:03:07 +00:00
property poss:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.poss
2019-03-07 13:03:07 +00:00
property prefix:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.prefix
2019-03-07 13:03:07 +00:00
property prep_case:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.prep_case
2019-03-07 13:03:07 +00:00
property pron_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.pron_type
2019-03-07 13:03:07 +00:00
property punct_side:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.punct_side
2019-03-07 13:03:07 +00:00
property punct_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.punct_type
2019-03-07 13:03:07 +00:00
property reflex:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.reflex
2019-03-07 13:03:07 +00:00
property style:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.style
2019-03-07 13:03:07 +00:00
property style_variant:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.style_variant
2019-03-07 13:03:07 +00:00
property tense:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.tense
2019-03-07 13:03:07 +00:00
property typo:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.typo
2019-03-07 13:03:07 +00:00
property verb_form:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.verb_form
2019-03-07 13:03:07 +00:00
property voice:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.voice
2019-03-07 13:03:07 +00:00
property verb_type:
def __get__(self):
2019-03-07 17:32:51 +00:00
return self.c.verb_type
2019-03-07 13:03:07 +00:00
property abbr_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.abbr]
2019-03-07 13:03:07 +00:00
property adp_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.adp_type]
2019-03-07 13:03:07 +00:00
property adv_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.adv_type]
2019-03-07 13:03:07 +00:00
property animacy_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.animacy]
2019-03-07 13:03:07 +00:00
property aspect_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.aspect]
2019-03-07 13:03:07 +00:00
property case_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.case]
2019-03-07 13:03:07 +00:00
property conj_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.conj_type]
2019-03-07 13:03:07 +00:00
property connegative_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.connegative]
2019-03-07 13:03:07 +00:00
property definite_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.definite]
2019-03-07 13:03:07 +00:00
property degree_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.degree]
2019-03-07 13:03:07 +00:00
property derivation_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.derivation]
2019-03-07 13:03:07 +00:00
property echo_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.echo]
2019-03-07 13:03:07 +00:00
property foreign_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.foreign]
2019-03-07 13:03:07 +00:00
property gender_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.gender]
2019-03-07 13:03:07 +00:00
property hyph_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.hyph]
2019-03-07 13:03:07 +00:00
property inf_form_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.inf_form]
2019-03-07 13:03:07 +00:00
property name_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.name_type]
2019-03-07 13:03:07 +00:00
property negative_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.negative]
2019-03-07 13:03:07 +00:00
property mood_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.mood]
2019-03-07 13:03:07 +00:00
property number_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.number]
2019-03-07 13:03:07 +00:00
property num_form_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.num_form]
2019-03-07 13:03:07 +00:00
property num_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.num_type]
2019-03-07 13:03:07 +00:00
property num_value_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.num_value]
2019-03-07 13:03:07 +00:00
property part_form_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.part_form]
2019-03-07 13:03:07 +00:00
property part_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.part_type]
2019-03-07 13:03:07 +00:00
property person_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.person]
2019-03-07 13:03:07 +00:00
property polite_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.polite]
2019-03-07 13:03:07 +00:00
property polarity_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.polarity]
2019-03-07 13:03:07 +00:00
property poss_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.poss]
2019-03-07 13:03:07 +00:00
property prefix_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.prefix]
2019-03-07 13:03:07 +00:00
property prep_case_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.prep_case]
2019-03-07 13:03:07 +00:00
property pron_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.pron_type]
2019-03-07 13:03:07 +00:00
property punct_side_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.punct_side]
2019-03-07 13:03:07 +00:00
property punct_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.punct_type]
2019-03-07 13:03:07 +00:00
property reflex_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.reflex]
2019-03-07 13:03:07 +00:00
property style_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.style]
2019-03-07 13:03:07 +00:00
property style_variant_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.style_variant]
2019-03-07 13:03:07 +00:00
property tense_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.tense]
2019-03-07 13:03:07 +00:00
property typo_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.typo]
2019-03-07 13:03:07 +00:00
property verb_form_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.verb_form]
2019-03-07 13:03:07 +00:00
property voice_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.voice]
2019-03-07 13:03:07 +00:00
property verb_type_:
def __get__(self):
2019-03-08 00:38:34 +00:00
return self.vocab.strings[self.c.verb_type]