Update 5. URL-Scraper Agent.ipynb

This commit is contained in:
Masih Moafi 2025-03-03 15:44:19 +03:30 committed by GitHub
parent 88f3ebb48b
commit aedfcfebb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -113,62 +113,4 @@
"execution_count": null,
"id": "cc9f2290-d16f-4722-857a-7996d4722857",
"metadata": {},
"outputs": [],
"source": [
"import wikipedia\n",
"\n",
"def search_wikipedia(query, lang='fa'):\n",
" \"\"\"Search Wikipedia for the given query in Persian and return a summary of the page.\"\"\"\n",
" wikipedia.set_lang(lang) # Set language to Farsi\n",
" try:\n",
" # Fetch summary of the query from Persian Wikipedia\n",
" summary = wikipedia.summary(query, sentences=3)\n",
" return summary\n",
" except wikipedia.exceptions.DisambiguationError as e:\n",
" # Handle ambiguity, if there are multiple results\n",
" return f\"Multiple results found, try being more specific: {e.options}\"\n",
" except wikipedia.exceptions.HTTPTimeoutError:\n",
" return \"Error: Request timed out.\"\n",
" except wikipedia.exceptions.RedirectError:\n",
" return \"Error: The page has been redirected.\"\n",
" except wikipedia.exceptions.PageError:\n",
" return \"Error: The page doesn't exist.\"\n",
" except Exception as e:\n",
" return f\"An error occurred: {str(e)}\"\n",
"\n",
"def generate_answer(query):\n",
" \"\"\"Generate an answer by first checking Wikipedia.\"\"\"\n",
" # check Wikipedia\n",
" wikipedia_answer = search_wikipedia(query)\n",
" return wikipedia_answer\n",
"\n",
"if __name__ == \"__main__\":\n",
" query = \"شاه عباس صفوی که بود و چه کرد؟\"\n",
" \n",
" answer = generate_answer(query)\n",
" print(f\"Answer: {answer}\")"
]
}
],
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
"outputs": []