import torch


def gather_data(device):
    """Gather raw data and build character dictionary."""
    characters = {}
    raw_data = []
    with open("./data.tsv", "r") as datafile:
        for line in datafile.readlines():
            name, lang = line.split('\t')
            raw_data.append((name, int(lang)))
            for char in name:
                characters.setdefault(char, len(characters))

    n_chars = len(characters)
    data = []
    for name, lang in raw_data:
        inp = torch.zeros(len(name), n_chars)
        for i, char in enumerate(name):
            inp[i][characters[char]] = 1.
        tgt = torch.tensor([lang], dtype=torch.long)
        data.append((name, inp.to(device), tgt.to(device)))

    return data, characters