{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f398ee08-a15a-41b7-83d9-85ee038b21be", "metadata": {}, "outputs": [], "source": [ "import jsonlines\n", "import re\n", "import urllib.parse" ] }, { "cell_type": "code", "execution_count": 2, "id": "e5361bd5-6cb7-4131-9402-bc07f59f3b00", "metadata": {}, "outputs": [], "source": [ "def parse_question(match, pagetitle, url):\n", " question = match.group('question').replace('
', '').strip()\n", " params = [p for p in match.group('info').strip('{}').split('|') if p != '']\n", " if params[0] not in ['Kvízkérdés', 'kvízkérdés']:\n", " print(params[0])\n", " return None\n", " params_dict = {p.split('=')[0]: p.split('=')[1] for p in params[1:]}\n", " title = match.group('title').strip()\n", " filepart = None\n", " if '[[' in question:\n", " filepart = re.search(r'\\[\\[.+\\]\\]', question)[0]\n", " question = question.replace(filepart, '').strip()\n", " filepart = filepart.strip('[]')\n", " type = {'egy': 'single', 'több': 'multi'}[params_dict['típus']]\n", " answer = params_dict['válasz']\n", " if '-' in answer or '/' in answer:\n", " return None\n", " score = None\n", " if 'pont' in params_dict:\n", " score = params_dict['pont']\n", " if 'pontozás' in params_dict:\n", " score = params_dict['pontozás']\n", " answers = [a.strip('# ') for a in match.group('answers').split('\\n') if a.strip('# ') != '']\n", " #print(answer)\n", " return {\n", " 'title': title,\n", " 'question': question, \n", " 'answers': answers, \n", " 'correct_answers': [int(a) if a.isnumeric() else ord(a.strip())-ord('a')+1 for a in answer.split(',') if a != ''] if answer != '*' else [a+1 for a in range(len(answers))],\n", " 'type': type, \n", " 'score': score,\n", " 'quiz_title': str(urllib.parse.unquote(pagetitle)), \n", " 'url': url,\n", " 'file': filepart,\n", " }" ] }, { "cell_type": "code", "execution_count": 3, "id": "91d930c8-dbd3-459e-b256-4d79d5469e14", "metadata": {}, "outputs": [], "source": [ "regex = r\"==(?P.+)==$\\n*(?P<question>(?:[^\\=]*\\n)*?)\\n*{{(?P<info>Kvízkérdés|.+)}}\\n*(?P<answers>(?:#.+\\n?)+)\\n*(?P<footnote>.+\\n)*?(?=\\n*==)\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "05ab6c0d-977b-490d-83c4-d3f701c36003", "metadata": {}, "outputs": [], "source": [ "data = []\n", "with jsonlines.open('kikerdezok.jsonl') as reader:\n", " for obj in reader:\n", " text = obj['text']\n", " matches = re.finditer(regex, text, re.MULTILINE)\n", " if not matches:\n", " continue\n", " #print(list(matches))\n", " \n", " title = obj['url'].strip('/').split('/')[-1]\n", " questions = [e.strip() for e in text.split('\\n\\n') if 'Kvízkérdés' in e]\n", " parsed_questions = [parse_question(m, title, obj['url']) for m in matches]\n", " data += [p for p in parsed_questions if p]" ] }, { "cell_type": "code", "execution_count": 5, "id": "a7421eac-77bd-478e-9a08-a0cecfa1e8a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "132" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len([d for d in data if d['question']])" ] }, { "cell_type": "code", "execution_count": 6, "id": "427ae355-0314-44a3-82eb-34b052ab10a2", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "46" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len([d for d in data if d['file']])" ] }, { "cell_type": "code", "execution_count": 7, "id": "25ff213c-4eff-46c9-bee3-f47b20991e10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8225" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(data)" ] }, { "cell_type": "code", "execution_count": 8, "id": "d3a65a8f-6f91-4e36-9b1d-c01163c04953", "metadata": {}, "outputs": [], "source": [ "with jsonlines.open('kvizek.jsonl', mode='w') as writer:\n", " for d in data:\n", " writer.write(d)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }