#!/usr/bin/env python
# coding=utf-8
"""
Annotate module
"""
from __future__ import unicode_literals
from __future__ import absolute_import

__author__ = "Clare Corthell"
__copyright__ = "Copyright 2015, summer.ai"
__date__ = "2015-12-07"
__email__ = "clare@summer.ai"

from nltk import pos_tag, word_tokenize, pos_tag_sents
from .readability import Readability


def batch_tag_sentences(message_dict):
    """Uses a more efficient way of tagging all sentences for a given
    message at once."""
    num_sentences = [len(page['sentences']) for page in message_dict['urls']]
    all_sentences = [word_tokenize(s['s_clean']) for page in message_dict['urls'] for s in page['sentences']]
    all_tags = pos_tag_sents(all_sentences)

    for page_index, slice_length in enumerate(num_sentences):
        slice_start = sum(num_sentences[:page_index])
        slice_end = slice_start + slice_length
        for sentence_index, tags in enumerate(all_tags[slice_start:slice_end]):
            pos_tags = ['/'.join(b) for b in tags]
            message_dict['urls'][page_index]['sentences'][sentence_index]['pos_tags'] = ' '.join(pos_tags)


def annotate_sentence(sentence_dict, term):
    """Annotates a sentence object from a message with Penn Treebank POS tags.

    Args:
        sentence_dict: dict -- Must contain 's' and 's_clean', which is the
                       sentence with all occurrences of the search term
                       replaced with '_TERM-'
    Returns:
        dict -- updated sentence_dict with 'pos_tags' field.
    """
    tags = pos_tag(word_tokenize(sentence_dict['s_clean']))
    pos_tags = ['/'.join(b) for b in tags]
    sentence_dict['pos_tags'] = " ".join(pos_tags)
    sentence_dict['features'] = {}
    return sentence_dict


def readability_score(url_object):
    """
    Calculates the Fleisch Reading Ease (https://simple.wikipedia.org/wiki/Flesch_Reading_Ease)
    for a document and saves it as 'readability_score' into the url_object

    Args:
        url_object: dict
    """
    scores = Readability(url_object['doc'])
    url_object['readability_score'] = scores.fleisch_reading_ease()

@@ -403,16 +403,21 @@
 %0A    %22%22%22
+%0A    
 Uses a m
@@ -491,16 +491,22 @@
 at once.
+%0A%0A    
 %22%22%22%0A    
@@ -1137,16 +1137,506 @@
 tags)%0A%0A%0A
+def annotate_single_sentence(sentence):%0A    tags = pos_tag(word_tokenize(sentence))%0A    pos_tags = %5B'/'.join(b) for b in tags%5D%0A    return %22 %22.join(pos_tags)%0A%0A%0Adef annotate_pos_with_term(sentence):%0A    %22%22%22%0A    POS-tag single sentence while preserving _TERM_%0A%0A    %22%22%22%0A    pos_term = %5B%5D%0A    tags = pos_tag(word_tokenize(sentence))%0A    for tag in tags:%0A        if tag%5B0%5D == '_TERM_':%0A            pos_term.append('_TERM_')%0A        else:%0A            pos_term.append(tag%5B1%5D)%0A    return pos_term%0A%0A%0A
 def anno
@@ -1678,16 +1678,21 @@
 %0A    %22%22%22
+%0A    
 Annotate
@@ -2034,16 +2034,17 @@
  field.%0A
+%0A
     %22%22%22%0A

annotate with pos term