This function returns the single most frequent calculated sentiment
        expressed by all stored users or by a list of specified users as a
        dictionary.
320825
def users_sentiments_single_most_frequent(
        self,
        usernames = None,
        ):
        
        users_sentiments_single_most_frequent = dict()
        if usernames is None:
            usernames = self.usernames()
        try:
            for username in usernames:
                sentiment = self.user_sentiments_most_frequent(
                    username             = username,
                    single_most_frequent = True
                )
                users_sentiments_single_most_frequent[username] = sentiment
            return users_sentiments_single_most_frequent
        except:
            log.error("error -- possibly a problem with tweets stored")
            return None
610199
def for_user(self, user):
        
        qs = SharedMemberQuerySet(model=self.model, using=self._db, user=user)
        qs = qs.filter(Q(author=user) | Q(foldershareduser__user=user))
        return qs.distinct() & self.distinct()
526027
def all(self):
        
        if not preferences_settings.ENABLE_CACHE:
            return self.load_from_db()

        preferences = self.registry.preferences()

        # first we hit the cache once for all existing preferences
        a = self.many_from_cache(preferences)
        if len(a) == len(preferences):
            return a  # avoid database hit if not necessary

        # then we fill those that miss, but exist in the database
        # (just hit the database for all of them, filtering is complicated, and
        # in most cases you'd need to grab the majority of them anyway)
        a.update(self.load_from_db(cache=True))
        return a
779145
def _from_dict(cls, _dict):
        """"""
        args = {}
        if 'concepts' in _dict:
            args['concepts'] = ConceptsOptions._from_dict(_dict.get('concepts'))
        if 'emotion' in _dict:
            args['emotion'] = EmotionOptions._from_dict(_dict.get('emotion'))
        if 'entities' in _dict:
            args['entities'] = EntitiesOptions._from_dict(_dict.get('entities'))
        if 'keywords' in _dict:
            args['keywords'] = KeywordsOptions._from_dict(_dict.get('keywords'))
        if 'metadata' in _dict:
            args['metadata'] = MetadataOptions._from_dict(_dict.get('metadata'))
        if 'relations' in _dict:
            args['relations'] = RelationsOptions._from_dict(
                _dict.get('relations'))
        if 'semantic_roles' in _dict:
            args['semantic_roles'] = SemanticRolesOptions._from_dict(
                _dict.get('semantic_roles'))
        if 'sentiment' in _dict:
            args['sentiment'] = SentimentOptions._from_dict(
                _dict.get('sentiment'))
        if 'categories' in _dict:
            args['categories'] = CategoriesOptions._from_dict(
                _dict.get('categories'))
        if 'syntax' in _dict:
            args['syntax'] = SyntaxOptions._from_dict(_dict.get('syntax'))
        return cls(**args)
855809
def majority_value(data, class_attr):
    
    if is_continuous(data[0][class_attr]):
        return CDist(seq=[record[class_attr] for record in data])
    else:
        return most_frequent([record[class_attr] for record in data])
1012676
private double getScore(int pos, Lexicon lexicon, double numdocs) {
		double score = 0;
		int indexTerm = this.indices[pos];
		double occurences = (double) this.freqs[pos];

		int fieldNum = this.indexToField[pos];
		double frequency = occurences / tokensPerField[fieldNum];

		// is there a custom weight for this field?
		String fieldName = lexicon.getFields()[fieldNum];
		WeightingMethod method = lexicon.getMethod(fieldName);

		if (method.equals(Parameters.WeightingMethod.BOOLEAN)) {
			score = 1;
		} else if (method.equals(Parameters.WeightingMethod.OCCURRENCES)) {
			score = occurences;
		} else if (method.equals(Parameters.WeightingMethod.FREQUENCY)) {
			score = frequency;
		} else if (method.equals(Parameters.WeightingMethod.TFIDF)) {
			int df = lexicon.getDocFreq(indexTerm);
			double idf = numdocs / (double) df;
			score = frequency * Math.log(idf);
			if (idf == 1)
				score = frequency;
		}
		return score;
	}
173866
def _default_tokenizer_func(content, keyword_weight_pair):
    
    seg_list = jieba.lcut_for_search(content)
    # Extract keyword-weight list by TF-IDF algorithms and by sorted maximum weight
    return jieba.analyse.extract_tags("".join(seg_list), topK=keyword_weight_pair, withWeight=True)
757279
def key_occurrence(self, key, update=True):
        
        if update:
            self.update()
        result = {}
        for k, v in self.database.items():
            if key in v:
                result[str(v[key])] = k
        return result
545751
def scores(self, text: str) -> Dict[str, float]:
        
        values = extract(text)
        input_fn = _to_func(([values], []))
        prediction = self._classifier.predict_proba(input_fn=input_fn)
        probabilities = next(prediction).tolist()
        sorted_languages = sorted(self.languages)
        return dict(zip(sorted_languages, probabilities))
872629
def _most_common(iterable):
    """"""
    data = Counter(iterable)
    return max(data, key=data.__getitem__)
646798
def _from_dict(cls, _dict):
        """"""
        args = {}
        if 'keywords' in _dict:
            args['keywords'] = NluEnrichmentKeywords._from_dict(
                _dict.get('keywords'))
        if 'entities' in _dict:
            args['entities'] = NluEnrichmentEntities._from_dict(
                _dict.get('entities'))
        if 'sentiment' in _dict:
            args['sentiment'] = NluEnrichmentSentiment._from_dict(
                _dict.get('sentiment'))
        if 'emotion' in _dict:
            args['emotion'] = NluEnrichmentEmotion._from_dict(
                _dict.get('emotion'))
        if 'categories' in _dict:
            args['categories'] = NluEnrichmentCategories._from_dict(
                _dict.get('categories'))
        if 'semantic_roles' in _dict:
            args['semantic_roles'] = NluEnrichmentSemanticRoles._from_dict(
                _dict.get('semantic_roles'))
        if 'relations' in _dict:
            args['relations'] = NluEnrichmentRelations._from_dict(
                _dict.get('relations'))
        if 'concepts' in _dict:
            args['concepts'] = NluEnrichmentConcepts._from_dict(
                _dict.get('concepts'))
        return cls(**args)
1141640
def sort_dict(key_counts, by_key=False):
    ''''''
    sort_key = lambda x: (-1 * x[1], x[0])
    if by_key:
        sort_key = lambda x: x[0]
    return sorted(key_counts.items(), key=sort_key)
296143
def _sentiment(self, distance=True):
    """"""
    sum_pos = 0
    sum_neg = 0
    text = self.parent
    entity_positions = range(self.start, self.end)
    non_entity_positions = set(range(len(text.words))).difference(entity_positions)
    if not distance:
      non_entity_polarities = np.array([text.words[i].polarity for i in non_entity_positions])
      sum_pos = sum(non_entity_polarities == 1)
      sum_neg = sum(non_entity_polarities == -1)
    else:
      polarities = np.array([w.polarity for w in text.words])
      polarized_positions = np.argwhere(polarities != 0)[0]
      polarized_non_entity_positions = non_entity_positions.intersection(polarized_positions)
      sentence_len = len(text.words)
      for i in polarized_non_entity_positions:
        min_dist = min(abs(self.start - i), abs(self.end - i))
        if text.words[i].polarity == 1:
          sum_pos += 1.0 - (min_dist - 1.0) / (2.0 * sentence_len)
        else:
          sum_neg += 1.0 - (min_dist - 1.0) / (2.0 *sentence_len)
    return (sum_pos, sum_neg)
860867
def get_all_users(**kwargs):
    
    users_qry = db.DBSession.query(User)

    filter_type = kwargs.get('filter_type')
    filter_value = kwargs.get('filter_value')

    if filter_type is not None:
        # Filtering the search of users
        if filter_type == "id":
            if isinstance(filter_value, str):
                # Trying to read a csv string
                log.info("[HB.users] Getting user by Filter ID : %s", filter_value)
                filter_value = eval(filter_value)
                if type(filter_value) is int:
                    users_qry = users_qry.filter(User.id==filter_value)
                else:
                    users_qry = users_qry.filter(User.id.in_(filter_value))
        elif filter_type == "username":
            if isinstance(filter_value, str):
                # Trying to read a csv string
                log.info("[HB.users] Getting user by Filter Username : %s", filter_value)
                filter_value = filter_value.split(",")
                for i, em in enumerate(filter_value):
                    log.info("[HB.users] >>> Getting user by single Username : %s", em)
                    filter_value[i] = em.strip()
                if isinstance(filter_value, str):
                    users_qry = users_qry.filter(User.username==filter_value)
                else:
                    users_qry = users_qry.filter(User.username.in_(filter_value))
        else:
            raise Exception("Filter type '{}' not allowed".format(filter_type))

    else:
        log.info('[HB.users] Getting All Users')

    rs = users_qry.all()

    return rs
133749
def sentiment(
    text       = None,
    confidence = False
    ):
    
    try:
        words = text.split(" ")
        # Remove empty strings.
        words = [word for word in words if word]
        features = word_features(words)
        classification = classifier.classify(features)
        confidence_classification = classifier.prob_classify(features).prob(classification)
    except:
        classification = None
        confidence_classification = None
    if confidence:
        return (
            classification,
            confidence_classification
        )
    else:
        return classification
780438
def _get_feature_weights_dict(feature_weights,  # type: FeatureWeights
                              feature_fn        # type: Optional[Callable[[str], str]]
                              ):
    # type: (...) -> Dict[str, Tuple[float, Tuple[str, int]]]
    
    return {
        # (group, idx) is an unique feature identifier, e.g. ('pos', 2)
        feat_name: (fw.weight, (group, idx))
        for group in ['pos', 'neg']
        for idx, fw in enumerate(getattr(feature_weights, group))
        for feat_name in _get_features(fw.feature, feature_fn)
    }
382914
def most_common(self, n=None):
        '''

        '''
        if n is None:
            return sorted(iteritems(self), key=itemgetter(1), reverse=True)
        return nlargest(n, iteritems(self), key=itemgetter(1))