def get_sentence_emotions(self, sentence: str) -> List[Emotion]:
     """
     Get list of all emotions in a given sentence.
     """
     sentence_emotions = []
     negated_terms = get_negated_words(sentence)
     for term in process_content(sentence,
                                 self.process_params.terms_mapping):
         for emotion in self.get_term_emotions(term,
                                               is_negated=term
                                               in negated_terms):
             sentence_emotions.append(emotion)
     return sentence_emotions
Beispiel #2
0
def _get_emotions_for_series_folder_fast(
    folder_path: str,
    limbic_model: LexiconLimbicModel,
    output_file_path: Optional[str] = None
) -> Dict[int, Dict[int, List[TimeEmotion]]]:
    """
    Given a folder (assuming all files have S*E* pattern), computes the
    emotions for each episode of each season

    If parameter "fast" is given as input, emotions will be computed with a
    proxy to identify negation sentences
    which might not guarantee to catch all linguistic negations. (e.g. "this is far from good").
    """
    seasons_episodes_subtitles_emotions: Dict[int, Dict[
        int, List[TimeEmotion]]] = defaultdict(lambda: defaultdict(list))
    season_episode_time_words: Dict[int, Dict[int, Dict[
        int, Set[Optional[str]]]]] = defaultdict(
            lambda: defaultdict(lambda: defaultdict(set)))
    season_episode_time_sentence: Dict[int, Dict[int, Dict[
        int, List[str]]]] = defaultdict(
            lambda: defaultdict(lambda: defaultdict(list)))
    subtitle_words = set()
    for srt_file in os.listdir(folder_path):
        subtitles = load_subtitles(os.path.join(folder_path, srt_file))
        season, episode = _get_season_episode(srt_file)
        for sub in subtitles:
            if _contains_negation(sub.content):
                season_episode_time_sentence[season][episode][
                    sub.start.seconds].append(sub.content)
                season_episode_time_words[season][episode][
                    sub.start.seconds].add(None)
            else:
                for term in process_content(sub.content,
                                            limbic_model.terms_mapping):
                    subtitle_words.add(term)
                    season_episode_time_words[season][episode][
                        sub.start.seconds].add(term)

    _update_seasons_emotions_data(season_episode_time_words,
                                  seasons_episodes_subtitles_emotions,
                                  season_episode_time_sentence, subtitle_words,
                                  limbic_model)
    if output_file_path:
        _save_seasons_emotions_to_file(seasons_episodes_subtitles_emotions,
                                       output_file_path)
    return seasons_episodes_subtitles_emotions
Beispiel #3
0
 def test_process_content_with_terms_mapping(self):
     for input_test, expected_output in TEST_CASES.items():
         output = process_content(input_test, terms_mapping=TERMS_MAPPING)
         self.assertEqual(output, expected_output)
Beispiel #4
0
 def test_process_content(self):
     for input_test, expected_output in TEST_CASES.items():
         output = process_content(input_test)
         self.assertEqual(output, expected_output)