(plays, test_fraction=0.2)
return users_and_plays, all_train_examples, all_test_examples
Splits character data into train and test sets.
      if test_fraction <= 0, returns {} for all_test_examples
      plays := list of (play, dict) tuples where play is a string and dict
      is a dictionary with character names as keys
Splits character data into train and test sets.
      if test_fraction <= 0, returns {} for all_test_examples
      plays := list of (play, dict) tuples where play is a string and dict
      is a dictionary with character names as keys
Splits character data into train and test sets . if test_fraction < = 0 returns {} for all_test_examples plays : = list of ( play dict ) tuples where play is a string and dict is a dictionary with character names as keys
def _get_train_test_by_character(plays, test_fraction=0.2):
    """
      Splits character data into train and test sets.
      if test_fraction <= 0, returns {} for all_test_examples
      plays := list of (play, dict) tuples where play is a string and dict
      is a dictionary with character names as keys
    """
    skipped_characters = 0
    all_train_examples = collections.defaultdict(list)
    all_test_examples = collections.defaultdict(list)

    def add_examples(example_dict, example_tuple_list):
        for play, character, sound_bite in example_tuple_list:
            example_dict[play_and_character(
                play, character)].append(sound_bite)

    users_and_plays = {}
    for play, characters in plays:
        curr_characters = list(characters.keys())
        for c in curr_characters:
            users_and_plays[play_and_character(play, c)] = play
        for character, sound_bites in characters.items():
            examples = [(play, character, sound_bite)
                        for sound_bite in sound_bites]
            if len(examples) <= 2:
                skipped_characters += 1
                # Skip characters with fewer than 2 lines since we need at least one
                # train and one test line.
                continue
            train_examples = examples
            if test_fraction > 0:
                num_test = max(int(len(examples) * test_fraction), 1)
                train_examples = examples[:-num_test]
                test_examples = examples[-num_test:]
                assert len(test_examples) == num_test
                assert len(train_examples) >= len(test_examples)
                add_examples(all_test_examples, test_examples)
            add_examples(all_train_examples, train_examples)
    return users_and_plays, all_train_examples, all_test_examples
def _get_train_test_by_character ( plays , test_fraction = 0.2 ) : skipped_characters = 0 all_train_examples = collections . defaultdict ( list ) all_test_examples = collections . defaultdict ( list ) def add_examples ( example_dict , example_tuple_list ) : for play , character , sound_bite in example_tuple_list : example_dict [ play_and_character ( play , character ) ] . append ( sound_bite ) users_and_plays = { } for play , characters in plays : curr_characters = list ( characters . keys ( ) ) for c in curr_characters : users_and_plays [ play_and_character ( play , c ) ] = play for character , sound_bites in characters . items ( ) : examples = [ ( play , character , sound_bite ) for sound_bite in sound_bites ] if len ( examples ) <= 2 : skipped_characters += 1 # Skip characters with fewer than 2 lines since we need at least one # train and one test line. continue train_examples = examples if test_fraction > 0 : num_test = max ( int ( len ( examples ) * test_fraction ) , 1 ) train_examples = examples [ : - num_test ] test_examples = examples [ - num_test : ] assert len ( test_examples ) == num_test assert len ( train_examples ) >= len ( test_examples ) add_examples ( all_test_examples , test_examples ) add_examples ( all_train_examples , train_examples ) return users_and_plays , all_train_examples , all_test_examples