from typing import List

from rb.core.lang import Lang
from rb.core.pos import POS
from rb.core.text_element import TextElement
from rb.core.text_element_type import TextElementType
from rb.parser.spacy_parser import SpacyParser
from spacy.tokens.token import Token


class Word(TextElement):


    def __init__(self, lang: Lang, token: Token,
                 depth: int = TextElementType.WORD.value,
                 container: TextElement = None):

        TextElement.__init__(self, lang=lang, text=token.text,
                             depth=depth, container=container)
        self.token = token
        self.lemma = token.lemma_
        self.pos = POS(token.pos_)
        self.detailed_pos = token.tag_

    @classmethod
    def from_str(cls, lang: Lang, text: str, pos: POS = POS.X) -> "Word":
        token = type('Token', (object,), {'text': text, 'lemma_': text, 'pos_': pos.value, 'tag_': pos.value})()
        return Word(lang, token, None)
    
    def is_dict_word(self):
        return SpacyParser.get_instance().is_dict_word(self.lemma, self.lang)

    def is_content_word(self):
        return self.is_dict_word() and (self.pos == POS.ADJ or self.pos == POS.ADV or self.pos == POS.NOUN or self.pos == POS.VERB)

    def get_sentences(self) -> List["Sentence"]:
        return []

    def __eq__(self, other):
        if isinstance(other, Word):
            return self.lemma == other.lemma and self.pos == other.pos
        return NotImplemented

    def __hash__(self):
        return hash((self.lemma, self.pos))
