# Path: nfq/tweemanager/tools.py
# def textclean(rawtext):
#     """
#     """
#     finetweet = ""
#     try:
#         # Proceso original pero se estaba perdiendo alguna información:
#         # Quita los RT's:
#         text_clean = rawtext.replace('RT ', '')
#         # todo en letras pequeñas:
#         text_clean = text_clean.lower()
#         # reemplaza urls por URL:
#         text_clean = re.sub(
#             '((www\.[^\s]+)|(https?://[^\s]+))', 'URL', text_clean)
#         # Quita mentions y pone AT_MENTION
#         text_clean = re.sub('@[^\s]+', 'AT_MENTION', text_clean)
#         # Reemplaza #palabra por palabra:
#         text_clean = re.sub(r'#([^\s]+)', r'\1', text_clean)
#         # Limpia puntuacion:
#         text_clean = repunctuacion.sub('', text_clean)
#         # quita acentos:
#         try: # Python 2
#             text_clean = unicode(text_clean)
#         except: # Python 3
#             pass
#         text_clean = ''.join((c for c in unicodedata.normalize(
#             'NFD', text_clean) if unicodedata.category(c) != 'Mn'))
#         # Limpia palabras repetidas y consecutivas:
#         text_clean = re.sub(r'\b(\w+)( \1\b)+', r'\1', text_clean)
#         # Reemplaza espacios extra
#         text_clean = re.sub('[\s]+', ' ', text_clean)
#         # y añade un trimzito
#         finetweet = text_clean.strip('\'"')
#     except:
#         logging.debug("unable to clean text {}".format(rawtext))
#     finally:
#         return finetweet


import pymongo
import datetime
import json
import datetime as dt
from bson.code import Code
from nfq.tweemanager.tools import textclean
        for result in coll.aggregate(pipeline):
            result = result
        return result

    else: # Only tweets in database are counted
        pipeline = [
            {"$match": {"$and": [{"created_at": {"$gte": StartDate}}, {"created_at": {"$lt": EndDate}}]}},
            {"$group": {"_id": "",
            "count": {"$sum": 1},
            "npos": {"$sum": {"$cond":[{"$eq":["$valuation." + classifier + ".value","positive"]},1,0]}},
            "nneg": {"$sum": {"$cond":[{"$eq":["$valuation." + classifier + ".value","negative"]},1,0]}},
            "met": {"$sum": "$valuation." + classifier + ".metric"}
            }},
        ]

        result = {'count':0, 'npos':0, 'nneg':0, 'met':0}
        for result in coll.aggregate(pipeline):
            result = result
        return result

def alert_words(StartDate, EndDate, coll, alert_words, db):
    """
    List of alert words - a list of warning words
    alert_words = cfgmanager.TextPatterns['alert_words']
    """
    aux_list = []
    aux_dict = {}
    for word in alert_words:
        aux_list.append(str(textclean(word)))
        try:

# Path: nfq/tweemanager/tools.py
# def textclean(rawtext):
#     """
#     """
#     finetweet = ""
#     try:
#         # Proceso original pero se estaba perdiendo alguna información:
#         # Quita los RT's:
#         text_clean = rawtext.replace('RT ', '')
#         # todo en letras pequeñas:
#         text_clean = text_clean.lower()
#         # reemplaza urls por URL:
#         text_clean = re.sub(
#             '((www\.[^\s]+)|(https?://[^\s]+))', 'URL', text_clean)
#         # Quita mentions y pone AT_MENTION
#         text_clean = re.sub('@[^\s]+', 'AT_MENTION', text_clean)
#         # Reemplaza #palabra por palabra:
#         text_clean = re.sub(r'#([^\s]+)', r'\1', text_clean)
#         # Limpia puntuacion:
#         text_clean = repunctuacion.sub('', text_clean)
#         # quita acentos:
#         try: # Python 2
#             text_clean = unicode(text_clean)
#         except: # Python 3
#             pass
#         text_clean = ''.join((c for c in unicodedata.normalize(
#             'NFD', text_clean) if unicodedata.category(c) != 'Mn'))
#         # Limpia palabras repetidas y consecutivas:
#         text_clean = re.sub(r'\b(\w+)( \1\b)+', r'\1', text_clean)
#         # Reemplaza espacios extra
#         text_clean = re.sub('[\s]+', ' ', text_clean)
#         # y añade un trimzito
#         finetweet = text_clean.strip('\'"')
#     except:
#         logging.debug("unable to clean text {}".format(rawtext))
#     finally:
#         return finetweet


# Path: nfq/tweemanager/reportermanager.py
import pymongo
import datetime
import json
import datetime as dt
from bson.code import Code
from nfq.tweemanager.tools import textclean

        for result in coll.aggregate(pipeline):
            result = result
        return result

    else: # Only tweets in database are counted
        pipeline = [
            {"$match": {"$and": [{"created_at": {"$gte": StartDate}}, {"created_at": {"$lt": EndDate}}]}},
            {"$group": {"_id": "",
            "count": {"$sum": 1},
            "npos": {"$sum": {"$cond":[{"$eq":["$valuation." + classifier + ".value","positive"]},1,0]}},
            "nneg": {"$sum": {"$cond":[{"$eq":["$valuation." + classifier + ".value","negative"]},1,0]}},
            "met": {"$sum": "$valuation." + classifier + ".metric"}
            }},
        ]

        result = {'count':0, 'npos':0, 'nneg':0, 'met':0}
        for result in coll.aggregate(pipeline):
            result = result
        return result

def alert_words(StartDate, EndDate, coll, alert_words, db):
    """
    List of alert words - a list of warning words
    alert_words = cfgmanager.TextPatterns['alert_words']
    """
    aux_list = []
    aux_dict = {}
    for word in alert_words:
        aux_list.append(str(textclean(word)))
        try:

            aux_dict.update({str(textclean(word)): word})