import re
import operator


def extract_words(row, params):
    """ Extract words as features
		Args
            row: A row of data
			stop_words: List of stop words
			case_insensitive: Use case insensitive?
			strip_entities: Strip entities (hashtags, user mentions, URLs)?
			strip_punctuation: Strip punctuations?
			strip_numbers: Strip numeric strings?
			strip_repeated_chars: Strip repeated characters?
			min_token_length: Minimum word token length
			max_features: Maximum number of feature tokens
    """
    word_feat = dict()
    if row and len(row.strip()) > 0:

        if params["case_insensitive"]:
            row = row.lower()

        if params["strip_entities"]:
            row = ' '.join(re.sub("(@[A-Za-z0-9]+)|(#[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ", row).split())

        if params["strip_punctuation"]:
            row = re.sub('[^a-zA-Z0-9]', ' ', row)

        if params["strip_numbers"]:
            row = re.sub('[0-9]', ' ', row)

        if params["strip_repeated_chars"]:
            row = re.sub(r'((\w)\2{2,})', ' ', row)

        if params["stop_words"]:
            row = row.replace('_', ' ').replace('-', ' ')
            x = [i.lower() for i in row.split() if i.lower() not in params["stop_words"]]
            row = ' '.join(x)

        for t in [x for x in row.split() if len(x) >= params["min_token_length"]]:
            if 'word_' + t not in word_feat:
                word_feat['word_' + t] = 0
            word_feat['word_' + t] += 1

        if params["max_features"]:
            # sorts dictionary of word features in descending order of occurence
            word_feat = sorted(word_feat.items(), key=operator.itemgetter(1), reverse=True)
            # creates dictionary of features for top max_features number of features
            word_feat = dict(word_feat[:params["max_features"]])

        return word_feat