# Path: laice/utils/spacy_ner.py
# def train_query(queryObj):
# def get_query(queryObj):
# def clean_story(story):
# def retrain_story(story):
#     ENTITY_OFFSETS = []
#     ENTITY_LIST = []
#     TRAIN_DATA = [
#         (
#             querystring,
#             ENTITY_OFFSETS
#         ),
#     ]
#     ENTITY_LIST = []
# 
# Path: stories/models.py
# class Query(models.Model):
#     # Querystring
#     querystring = models.TextField()
# 
#     # Each query will belong to a story
#     story = models.ForeignKey(Story, null=False)
# 
#     # Has the querystring been configured
#     # by configured I mean manually parsed
#     configured = models.BooleanField(default=False)
# 
#     # Store it's named entity recognizier via Spacy
#     parsed_ner = JSONField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.querystring + ' | ' + str(self.story)
# 
# class Story(models.Model):
#     name = models.CharField(
#         max_length=128,
#         unique=True,
#     )
# 
#     training = models.BooleanField(default=False)
# 
#     @property
#     def unconfigured_requests(self):
#         return self.query_set.filter(configured=False).count()
# 
#     def perform_clean(self):
#         if self.name:
#             # Replace whitespaces
#             self.name = self.name.replace(' ', '')
# 
#             # Replace special characters with '-'
#             self.name = map(lambda x: x if (x.isalnum() or x == '-') else '-', self.name)
#             self.name = ''.join(self.name)
# 
#     def save(self, *args, **kwargs):
#         self.perform_clean()
#         super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.name


from django.db.models.signals import pre_save, pre_delete
from django.dispatch import receiver
from django.conf import settings
from laice.utils import spacy_ner
from stories.models import Query, Story


@receiver(pre_save, sender=Query)
def parse_ner(sender, instance, *args, **kwargs):
    """
    Callback function that occurs whenever a Query
    object is created. Used to parse NER
    """

    # If its being created, it is assumed that it wants
    # to be automatically parsed
    if instance.pk is None:
        # Remember that key is the text
        # and the dict[key] contains the attribute (or entity)
        print('Query created for ', str(instance))
        spacy_ner.get_query(instance)

    # If its being updated, it is assumed that it wants
    # to be manually parsed and should be fed back into
    # the training data
    else:
        instance.configured = True
        print('Query updated for ', str(instance))

# Path: laice/utils/spacy_ner.py
# def train_query(queryObj):
# def get_query(queryObj):
# def clean_story(story):
# def retrain_story(story):
#     ENTITY_OFFSETS = []
#     ENTITY_LIST = []
#     TRAIN_DATA = [
#         (
#             querystring,
#             ENTITY_OFFSETS
#         ),
#     ]
#     ENTITY_LIST = []
# 
# Path: stories/models.py
# class Query(models.Model):
#     # Querystring
#     querystring = models.TextField()
# 
#     # Each query will belong to a story
#     story = models.ForeignKey(Story, null=False)
# 
#     # Has the querystring been configured
#     # by configured I mean manually parsed
#     configured = models.BooleanField(default=False)
# 
#     # Store it's named entity recognizier via Spacy
#     parsed_ner = JSONField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.querystring + ' | ' + str(self.story)
# 
# class Story(models.Model):
#     name = models.CharField(
#         max_length=128,
#         unique=True,
#     )
# 
#     training = models.BooleanField(default=False)
# 
#     @property
#     def unconfigured_requests(self):
#         return self.query_set.filter(configured=False).count()
# 
#     def perform_clean(self):
#         if self.name:
#             # Replace whitespaces
#             self.name = self.name.replace(' ', '')
# 
#             # Replace special characters with '-'
#             self.name = map(lambda x: x if (x.isalnum() or x == '-') else '-', self.name)
#             self.name = ''.join(self.name)
# 
#     def save(self, *args, **kwargs):
#         self.perform_clean()
#         super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.name


# Path: stories/signals.py
from django.db.models.signals import pre_save, pre_delete
from django.dispatch import receiver
from django.conf import settings
from laice.utils import spacy_ner
from stories.models import Query, Story



@receiver(pre_save, sender=Query)
def parse_ner(sender, instance, *args, **kwargs):
    """
    Callback function that occurs whenever a Query
    object is created. Used to parse NER
    """

    # If its being created, it is assumed that it wants
    # to be automatically parsed
    if instance.pk is None:
        # Remember that key is the text
        # and the dict[key] contains the attribute (or entity)
        print('Query created for ', str(instance))
        spacy_ner.get_query(instance)

    # If its being updated, it is assumed that it wants
    # to be manually parsed and should be fed back into
    # the training data
    else:
        instance.configured = True
        print('Query updated for ', str(instance))

        spacy_ner.train_query(instance)