# Path: newsarticles/models.py
# class TrainedCategoryRelevance(models.Model):
#     coding = models.ForeignKey(TrainedCoding)
#     category = models.ForeignKey(Category)
#     relevance = models.FloatField()
# 
# class TrainedCoding(models.Model):
#     article = models.OneToOneField(Article, db_index=True)
#     date = models.DateTimeField(auto_now=True)
#     model_info = models.TextField()
# 
#     categories = models.ManyToManyField(
#         Category, through='TrainedCategoryRelevance')
#     relevance = models.FloatField()
#     sentiment = models.FloatField(null=True)
#     bin = models.IntegerField(null=True, blank=True)
#     sentiment_processed = models.BooleanField(default=False)
# 
# class TrainedLocation(models.Model):
#     coding = models.ForeignKey(TrainedCoding)
#     text = models.TextField()
#     latitude = models.FloatField(null=True, blank=True)
#     longitude = models.FloatField(null=True, blank=True)
#     confidence = models.FloatField(null=True, blank=True)
#     neighborhood = models.TextField(default="", blank=True)
#     is_best = models.BooleanField(default=False)
# 
# class Category(models.Model):
# 
#     KINDS = OrderedDict([
#         ('crimes', 'Crimes'),
#         ('orgs', 'Justice Agencies / Agencies'),
#         ('policing', 'Policing'),
#         ('other', 'Misc.'),
#     ])
#     DEFAULT_KIND = 'other'
# 
#     title = models.CharField(max_length=256)
#     abbreviation = models.CharField(max_length=5)
#     active = models.BooleanField(default=True)
#     kind = models.CharField(
#         max_length=50, choices=KINDS.items(), default=DEFAULT_KIND)
#     created = models.DateTimeField(auto_now=False, auto_now_add=True)
# 
#     objects = CategoryQuerySet.as_manager()
# 
#     def __str__(self):
#         return '{} ({})'.format(self.title, self.abbreviation)
# 
#     class Meta:
#         ordering = ['kind', 'abbreviation']


from functools import lru_cache
from math import isnan
from django.core.exceptions import ObjectDoesNotExist
from newsarticles.models import TrainedCategoryRelevance, TrainedCoding, TrainedLocation, Category
import logging
import tagnews
import numpy as np
MIN_LOCATION_RELEVANCE = 0.5

LOG = logging.getLogger(__name__)

# Memoize taggers, they are slow to instantiate
@lru_cache(maxsize=1)
def crime_tagger():
    return tagnews.CrimeTags()

@lru_cache(maxsize=1)
def geo_tagger():
    return tagnews.GeoCoder()

@lru_cache(maxsize=1)
def sent_evaller():
    return tagnews.SentimentGoogler()

def current_model_info():
    return 'tagnews {}'.format(tagnews.__version__)

def tag_article(article):
    try:
        locations = extract_locations(article)
        category_scores, max_score = tag_categories(article)
    except Exception as e:
        LOG.exception(e)
        return

    TrainedCoding.objects.filter(article=article).delete()


# Path: newsarticles/models.py
# class TrainedCategoryRelevance(models.Model):
#     coding = models.ForeignKey(TrainedCoding)
#     category = models.ForeignKey(Category)
#     relevance = models.FloatField()
# 
# class TrainedCoding(models.Model):
#     article = models.OneToOneField(Article, db_index=True)
#     date = models.DateTimeField(auto_now=True)
#     model_info = models.TextField()
# 
#     categories = models.ManyToManyField(
#         Category, through='TrainedCategoryRelevance')
#     relevance = models.FloatField()
#     sentiment = models.FloatField(null=True)
#     bin = models.IntegerField(null=True, blank=True)
#     sentiment_processed = models.BooleanField(default=False)
# 
# class TrainedLocation(models.Model):
#     coding = models.ForeignKey(TrainedCoding)
#     text = models.TextField()
#     latitude = models.FloatField(null=True, blank=True)
#     longitude = models.FloatField(null=True, blank=True)
#     confidence = models.FloatField(null=True, blank=True)
#     neighborhood = models.TextField(default="", blank=True)
#     is_best = models.BooleanField(default=False)
# 
# class Category(models.Model):
# 
#     KINDS = OrderedDict([
#         ('crimes', 'Crimes'),
#         ('orgs', 'Justice Agencies / Agencies'),
#         ('policing', 'Policing'),
#         ('other', 'Misc.'),
#     ])
#     DEFAULT_KIND = 'other'
# 
#     title = models.CharField(max_length=256)
#     abbreviation = models.CharField(max_length=5)
#     active = models.BooleanField(default=True)
#     kind = models.CharField(
#         max_length=50, choices=KINDS.items(), default=DEFAULT_KIND)
#     created = models.DateTimeField(auto_now=False, auto_now_add=True)
# 
#     objects = CategoryQuerySet.as_manager()
# 
#     def __str__(self):
#         return '{} ({})'.format(self.title, self.abbreviation)
# 
#     class Meta:
#         ordering = ['kind', 'abbreviation']


# Path: newsarticles/tagging.py
from functools import lru_cache
from math import isnan
from django.core.exceptions import ObjectDoesNotExist
from newsarticles.models import TrainedCategoryRelevance, TrainedCoding, TrainedLocation, Category
import logging
import tagnews
import numpy as np

MIN_LOCATION_RELEVANCE = 0.5

LOG = logging.getLogger(__name__)

# Memoize taggers, they are slow to instantiate
@lru_cache(maxsize=1)
def crime_tagger():
    return tagnews.CrimeTags()

@lru_cache(maxsize=1)
def geo_tagger():
    return tagnews.GeoCoder()

@lru_cache(maxsize=1)
def sent_evaller():
    return tagnews.SentimentGoogler()

def current_model_info():
    return 'tagnews {}'.format(tagnews.__version__)

def tag_article(article):
    try:
        locations = extract_locations(article)
        category_scores, max_score = tag_categories(article)
    except Exception as e:
        LOG.exception(e)
        return

    TrainedCoding.objects.filter(article=article).delete()


    coding = TrainedCoding.objects.create(