from neomodel import StructuredNode, StringProperty, JSONProperty, \
                     Relationship, IntegerProperty
import numpy as np
import re

from models.text_relation import TextRelation


__all__ = ['TextNode']


class TextNode(StructuredNode):
    order_id = IntegerProperty(required=True, unique_index=True)
    label = StringProperty(required=True)
    text = StringProperty(required=True)
    alg_results = JSONProperty()
    link = Relationship('TextNode', 'ALG', model=TextRelation)

    def short(self):
        res = ''.join([word.strip() + ' '
                       for word in re.split(r'[\n ]', self.text, 5)[:5]])
        return res

    def describe(self):
        return f"""
            <h1>Фрагмент: {self.order_id} </h1>
            <table border="1" width=100%>
                <caption>
                    Информация о вершине
                </caption>
                <tr>
                    <th>Количество символов</th>
                    <td>{self.character_num()}</td>
                </tr>
                <tr>
                    <th>Количество слов</th>
                    <td>{self.words_num()}</td>
                </tr>
                <tr>
                    <th>Количество предложений</th>
                    <td>{self.sentences_num()}</td>
                </tr>
                <tr>
                    <th>Количество связей</th>
                    <td>{len(self.link)}</td>
                </tr>
            </table>
        """

    def preview(self, frag_num=0):
        leading = 3
        if frag_num > 0:
            leading = int(np.floor(np.log10(frag_num))) + 1
            if str(self.order_id) != str(self.label):
                return f"{str(self.order_id).zfill(leading)}: " \
                   + f"[{self.label}] {self.short()}..."
            else:
                return f"{str(self.order_id).zfill(leading)}: " \
                       + f"[{self.label}] {self.short()}..."
        return f"[{self.label}] {self.short()}..."

    def words_num(self):
        return len(self.text.split())

    def character_num(self):
        return len(self.text)

    def sentences_num(self):
        return len([s for s in self.text.split('.') if len(s) > 2])

[((13, 15, 13, 64), 'neomodel.IntegerProperty', 'IntegerProperty', (), '', False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((14, 12, 14, 41), 'neomodel.StringProperty', 'StringProperty', (), '', False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((15, 11, 15, 40), 'neomodel.StringProperty', 'StringProperty', (), '', False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((16, 18, 16, 32), 'neomodel.JSONProperty', 'JSONProperty', ({}, {}), '()', False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((17, 11, 17, 62), 'neomodel.Relationship', 'Relationship', (), '', False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((21, 35, 21, 67), 're.split', 're.split', ({(21, 44, 21, 52): '"""[\\\\n ]"""', (21, 54, 21, 63): 'self.text', (21, 65, 21, 66): '5'}, {}), "('[\\\\n ]', self.text, 5)", False, 'import re\n'), ((53, 35, 53, 53), 'numpy.log10', 'np.log10', ({(53, 44, 53, 52): 'frag_num'}, {}), '(frag_num)', True, 'import numpy as np\n')]