change data structure of noor result

This commit is contained in:
HamidReza Montazer 2024-12-19 12:12:33 +03:30
parent f3e8c366d5
commit 8752b6225c

View File

@ -2,19 +2,20 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 39,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import requests\n", "import requests\n",
"from bs4 import BeautifulSoup\n", "from bs4 import BeautifulSoup\n",
"from pathlib import Path\n", "from pathlib import Path\n",
"from tqdm import tqdm" "from tqdm import tqdm\n",
"import json"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -27,7 +28,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -57,19 +58,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"100%|██████████| 4167/4167 [00:22<00:00, 186.02it/s]\n" "100%|██████████| 4167/4167 [00:33<00:00, 122.76it/s]\n"
] ]
} }
], ],
"source": [ "source": [
"result = {}\n", "results = []\n",
"\n", "\n",
"for verb in tqdm(verbs):\n", "for verb in tqdm(verbs):\n",
" with open(htmls_path / (verb + \".html\"), \"r\") as f:\n", " with open(htmls_path / (verb + \".html\"), \"r\") as f:\n",
@ -80,10 +81,27 @@
" past_stem = stems[0].text\n", " past_stem = stems[0].text\n",
" present_stem = stems[1].text\n", " present_stem = stems[1].text\n",
"\n", "\n",
" result[verb] = {}\n", " structures = soup.select(\"div.section-wrapper.more-examples\")\n",
"\n", " for structure in structures:\n",
" result[verb][\"past_stem\"] = past_stem\n", " examples = structure.findAll(\"div\", {\"class\": \"example\"})\n",
" result[verb][\"present_stem\"] = present_stem" " for example in examples:\n",
" result = {}\n",
" result[\"verb\"] = verb\n",
" result[\"past_stem\"] = past_stem\n",
" result[\"present_stem\"] = present_stem\n",
" result[\"structure\"] = str(structure.findAll(\"div\", {\"class\": \"valency\"})[0])\n",
" result[\"text\"] = example.text\n",
" results.append(result)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"with open(output_verb_path, \"w\", encoding=\"utf-8\") as f:\n",
" json.dump(results, f, ensure_ascii=False, indent=4)"
] ]
}, },
{ {