import random


def train_test_split(datafile, train_rate=0.8):
    """随机划分训练集和测试集

    Args:
        datafile (fileIOwrapper): 训练集文件指针
        train_rate (float, optional): 训练集占比. Defaults to 0.8.

    Returns:
        （list, list）: (train set, test set)
    """
    with open(datafile, 'r', encoding='utf8') as f:
        dataset = f.readlines()
    train_set_size = round(len(dataset) * train_rate)
    train_set = random.sample(dataset, train_set_size)
    test_set = list(set(dataset).difference(set(train_set)))
    return train_set, test_set