<commit_before>from __future__ import print_function
from __future__ import unicode_literals

import argparse
import pprint
import sys

import imdb
import re


def parse_args():
    parser = argparse.ArgumentParser()
    return parser.parse_args()


def find_representative_movies(movies, max_examples=5):
    movies = sorted(movies, key=lambda m: m.get("rating", 0), reverse=True)
    return [m for m in movies[:max_examples] if m.get("rating", 0) >= 6]

def get_aliases(character_name):
    alias_pattern = re.compile(r"(.*\s)['\"](.*)['\"]\s(.*)")

    m = alias_pattern.match(character_name)
    if m:
        plain_name = m.group(1).strip() + " " + m.group(3).strip()
        alias = m.group(2).strip()
        return [plain_name, alias]
    else:
        return [character_name]


def main():
    args = parse_args()

    # GoT id: 0944947

    ia = imdb.IMDb()

    actor2char = dict()
    char2actor = dict()
    actor2known_for = dict()

    for item in ia.search_movie("Game of Thrones")[:1]:
        # fetch the detail page info
        ia.update(item)

        # cast = main cast, guests = other people

        for person in item["cast"]:
            # useful info for Person: name, currentRole, actor/actress lists other movies, canonical name
            actor2char[person["name"]] = unicode(person.currentRole)

            for character_alias in get_aliases(unicode(person.currentRole)):
                char2actor[character_alias] = person["name"]

            print(person["name"], "is", " aka ".join(get_aliases(unicode(person.currentRole))))
            ia.update(person)
            ia.update(person.currentRole)

            # print(person.currentRole.keys())
            # print("AKA", ", ".join(person.currentRole.get("akas", [])))
            # print("Bio", ", ".join(person.currentRole["biography"]))
            # print("Quotes", ", ".join(person.currentRole.get("quotes", [])))

            # useful info for Character: name, akas, biography, quotes

            other_movies = person.get("actor", person.get("actress"))

            if other_movies:
                other_movies = [m for m in other_movies if item["title"] not in m["title"]]
                update_movies(ia, other_movies)
                other_movies = find_representative_movies(other_movies)
                other_movies = sorted(other_movies, key=lambda m: m.get("rating", 0), reverse=True)
                pprint.pprint([(m["title"], m.get("rating", 0)) for m in other_movies[:5]])

                actor2known_for[person["name"]] = [m["title"] for m in other_movies[:5]]

    print("Actor list one per line")
    for actor in sorted(sorted(actor2char.keys())):
        print(actor)

    print("Character list one per line")
    for character in sorted(sorted(char2actor.keys())):
        print(character)

    print("Actor to character")
    pprint.pprint(actor2char)

    print("Char 2 act")
    pprint.pprint(char2actor)
<commit_msg>Minor cleanup<commit_after>from __future__ import print_function
from __future__ import unicode_literals

import argparse
import pprint
import sys

import imdb
import re


def parse_args():
    parser = argparse.ArgumentParser()
    return parser.parse_args()


def find_representative_movies(movies, max_examples=5):
    movies = sorted(movies, key=lambda m: m.get("rating", 0), reverse=True)
    return [m for m in movies[:max_examples] if m.get("rating", 0) >= 6]

def get_aliases(character_name):
    alias_pattern = re.compile(r"(.*\s)['\"](.*)['\"]\s(.*)")

    m = alias_pattern.match(character_name)
    if m:
        plain_name = m.group(1).strip() + " " + m.group(3).strip()
        alias = m.group(2).strip()
        return [plain_name, alias]
    else:
        return [character_name]


def main():
    args = parse_args()

    # GoT id: 0944947

    ia = imdb.IMDb()

    actor2char = dict()
    char2actor = dict()
    actor2known_for = dict()

    show = ia.search_movie("Game of Thrones")[0]

    # fetch the detail page info
    ia.update(show)
    # print("GoT show keys", show.keys())

    # cast = main cast, guests = other people

    for person in show["cast"]:
        # useful info for Person: name, currentRole, actor/actress lists other movies, canonical name
        actor2char[person["name"]] = unicode(person.currentRole)

        for character_alias in get_aliases(unicode(person.currentRole)):
            char2actor[character_alias] = person["name"]

        print(person["name"], "is", " aka ".join(get_aliases(unicode(person.currentRole))))
        ia.update(person)
        ia.update(person.currentRole)

        # print(person.currentRole.keys())
        # print("AKA", ", ".join(person.currentRole.get("akas", [])))
        # print("Bio", ", ".join(person.currentRole["biography"]))
        # print("Quotes", ", ".join(person.currentRole.get("quotes", [])))

        # useful info for Character: name, akas, biography, quotes

        other_movies = person.get("actor", person.get("actress"))

        if other_movies:
            other_movies = [m for m in other_movies if show["title"] not in m["title"]]
            update_movies(ia, other_movies)
            other_movies = find_representative_movies(other_movies)
            other_movies = sorted(other_movies, key=lambda m: m.get("rating", 0), reverse=True)
            pprint.pprint([(m["title"], m.get("rating", 0)) for m in other_movies[:5]])

            actor2known_for[person["name"]] = [m["title"] for m in other_movies[:5]]

    print("Actor list one per line")
    for actor in sorted(sorted(actor2char.keys())):
        print(actor)

    print("Character list one per line")
    for character in sorted(sorted(char2actor.keys())):
        print(character)

    print("Actor to character")
    pprint.pprint(actor2char)

    print("Char 2 act")
    pprint.pprint(char2actor)

{'fraction_non_alphanumeric': 0.14099037138927098, 'fraction_numerical': 0.01031636863823934, 'mean_word_length': 3.0228215767634854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7202956', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1807, 'n_words': 421}