{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: sparqlwrapper in /home/alvaro/Desktop/ME/SpanishMedicaLLM/.venv/lib/python3.10/site-packages (2.0.0)\n",
"Requirement already satisfied: rdflib>=6.1.1 in /home/alvaro/Desktop/ME/SpanishMedicaLLM/.venv/lib/python3.10/site-packages (from sparqlwrapper) (7.0.0)\n",
"Requirement already satisfied: isodate<0.7.0,>=0.6.0 in /home/alvaro/Desktop/ME/SpanishMedicaLLM/.venv/lib/python3.10/site-packages (from rdflib>=6.1.1->sparqlwrapper) (0.6.1)\n",
"Requirement already satisfied: pyparsing<4,>=2.1.0 in /home/alvaro/Desktop/ME/SpanishMedicaLLM/.venv/lib/python3.10/site-packages (from rdflib>=6.1.1->sparqlwrapper) (3.1.2)\n",
"Requirement already satisfied: six in /home/alvaro/Desktop/ME/SpanishMedicaLLM/.venv/lib/python3.10/site-packages (from isodate<0.7.0,>=0.6.0->rdflib>=6.1.1->sparqlwrapper) (1.16.0)\n"
]
}
],
"source": [
"!pip install sparqlwrapper\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from SPARQLWrapper import SPARQLWrapper, JSON"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Set the DBpedia SPARQL endpoint\n",
"sparql = SPARQLWrapper(\"http://dbpedia.org/sparql\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Define your SPARQL query\n",
"query = \"\"\"\n",
"PREFIX dbo: \n",
"\n",
"SELECT ?disease ?diseaseLabel ?abstract\n",
"WHERE {\n",
" ?disease rdf:type dbo:Disease ;\n",
" rdfs:label ?diseaseLabel ;\n",
" dbo:abstract ?abstract .\n",
" FILTER(LANG(?diseaseLabel) = 'es' && LANG(?abstract) = 'es')\n",
"}\n",
"\"\"\"\n",
"\n",
"# Set the query and response format\n",
"sparql.setQuery(query)\n",
"sparql.setReturnFormat(JSON)\n",
"\n",
"# Execute the query\n",
"results = sparql.query().convert()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_blind = results[\"results\"][\"bindings\"]\n",
"len(results_blind)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Disease: Acoplamiento de Cadiot-Chodkiewicz\n",
"Abstract: El acoplamiento de Cadiot-Chodkiewicz es una reacción orgánica que consiste en el acoplamiento entre un alquino terminal y un haloalquino catalizada por una sal cuprosa Cu(I) como por ejemplo el bromuro de cobre (I) y una amina como base. El producto de la reacción es un dialaquino.\n",
"\n",
"Disease: Cesárea\n",
"Abstract: Una cesárea es un tipo de intervención quirúrgica el cual se realiza una incisión quirúrgica en el abdomen (laparotomía) y el útero de la madre para extraer uno o más bebés. La OMS recomienda su uso cuando sea necesaria para salvar la vida de las madres y los neonatos por razones médicas, pero puede aumentar el riesgo de complicaciones por ser un procedimiento de cirugía mayor, y estima que el porcentaje de cesáreas en una región no debería superar el 15 %. Actualmente las tasas de cesárea están aumentando en la mayoría de países muy por encima de las tasas recomendadas por la OMS en todos los rangos de edad, sobre todo en los países más ricos. No se debe confundir con la episiotomía, que es una incisión en el periné para facilitar el parto. La cesárea se hace por encima de la pelvis. Contrariamente a lo sostenido por algunos autores, la palabra «cesárea» no tiene nada que ver con Julio César, ni este nació por medio de esa cirugía.\n",
"\n"
]
}
],
"source": [
"for result in results_blind[:2]:\n",
" disease_label = result[\"diseaseLabel\"][\"value\"]\n",
" abstract = result[\"abstract\"][\"value\"]\n",
" print(f\"Disease: {disease_label}\\nAbstract: {abstract}\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"def get_disease_names():\n",
" \n",
" # Define the SPARQL query to retrieve disease names\n",
" query = \"\"\"\n",
" PREFIX dbo: \n",
"\n",
" SELECT DISTINCT ?diseaseLabel\n",
" WHERE {\n",
" ?disease rdf:type dbo:Disease ;\n",
" rdfs:label ?diseaseLabel .\n",
" FILTER(LANG(?diseaseLabel) = 'es')\n",
" }\n",
" \"\"\"\n",
" \n",
" # Set the query and response format\n",
" sparql.setQuery(query)\n",
" sparql.setReturnFormat(JSON)\n",
" \n",
" # Execute the query\n",
" results = sparql.query().convert()\n",
" \n",
" # Extract and return the disease names\n",
" disease_names = [result[\"diseaseLabel\"][\"value\"] for result in results[\"results\"][\"bindings\"]]\n",
" return disease_names\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Example usage\n",
"disease_names = get_disease_names()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7223\n"
]
}
],
"source": [
"print(len(disease_names))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of Disease Names:\n",
"Acoplamiento de Cadiot-Chodkiewicz\n",
"Cesárea\n",
"Enfermedad por depósito de pirofosfato de calcio\n",
"Campilobacteriosis\n",
"Neoplasia\n",
"Cáncer\n",
"Candidiasis\n",
"Moquillo\n",
"Reacción de Cannizzaro\n",
"Síndrome de Capgras\n"
]
}
],
"source": [
"\n",
"# Print the retrieved disease names\n",
"print(\"List of Disease Names:\")\n",
"for name in disease_names[:10]:\n",
" print(name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"file_path = 'enfermedades.json'\n",
"\n",
"dic_names = {\"enfermedades\": disease_names}\n",
"with open(file_path, 'w', encoding=\"utf-8\") as file:\n",
" loaded_list = json.dump(dic_names, file, ensure_ascii=False, indent=2)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}