from functools import partial from glob import glob from typing import Iterable, Optional from datasets import Dataset def is_query(text: str): n_question = text.count("?") n_statement = text.count(".") if n_question + n_statement < 2: return False if n_question > (n_statement * 0.5): return True def first_answer(answers: list[str]) -> Optional[str]: for answer in answers: if answer and (not is_query(answer)) and ("tak" not in answer.lower()): return answer return None def generate_dataset(threads: list[list[str]]) -> Iterable[dict]: for thread in threads: question = thread[0] if is_query(question): answer = first_answer(thread[1:]) if answer: yield {"question": question, "answer": answer} def main(): files = glob("dagw/sektioner/hest/hest_forum*") threads = [] for file in files: with open(file) as in_file: threads.append(in_file.read().split("\n")) dataset = Dataset.from_generator(partial(generate_dataset, threads)) dataset = dataset.train_test_split(test_size=0.2, shuffle=True) dataset.push_to_hub("kardosdrur/hestenet-qa") if __name__ == "__main__": main()