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])

[((270, 319), 'neomodel.IntegerProperty', 'IntegerProperty', ([], {'required': '(True)', 'unique_index': '(True)'}), '(required=True, unique_index=True)\n', (285, 319), False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((332, 361), 'neomodel.StringProperty', 'StringProperty', ([], {'required': '(True)'}), '(required=True)\n', (346, 361), False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((373, 402), 'neomodel.StringProperty', 'StringProperty', ([], {'required': '(True)'}), '(required=True)\n', (387, 402), False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((421, 435), 'neomodel.JSONProperty', 'JSONProperty', ([], {}), '()\n', (433, 435), False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((447, 498), 'neomodel.Relationship', 'Relationship', (['"""TextNode"""', '"""ALG"""'], {'model': 'TextRelation'}), "('TextNode', 'ALG', model=TextRelation)\n", (459, 498), False, 'from neomodel import StructuredNode, StringProperty, JSONProperty, Relationship, IntegerProperty\n'), ((598, 630), 're.split', 're.split', (['"""[\\\\n ]"""', 'self.text', '(5)'], {}), "('[\\\\n ]', self.text, 5)\n", (606, 630), False, 'import re\n'), ((1597, 1615), 'numpy.log10', 'np.log10', (['frag_num'], {}), '(frag_num)\n', (1605, 1615), True, 'import numpy as np\n')]