Created
February 7, 2020 17:06
-
-
Save Precuero/28dc8c46e602048775f1adfbc1f28933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml\n" | |
] | |
} | |
], | |
"source": [ | |
"# Carga de librerías\n", | |
"import nltk\n", | |
"nltk.download()\n", | |
"from nltk.tokenize import word_tokenize, sent_tokenize\n", | |
"from nltk.corpus import stopwords" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#Carga de datos\n", | |
"\n", | |
"# Unos versos de Gustavo Adolfo Bécquer \n", | |
"\n", | |
"text = \"\"\"Podrá nublarse el sol eternamente;\n", | |
"Podrá secarse en un instante el mar;\n", | |
"Podrá romperse el eje de la Tierra;\n", | |
"Como un débil cristal.\n", | |
"¡Todo sucederá! Podrá la muerte\n", | |
"Cubrirme con su fúnebre crespón;\n", | |
"Pero jamás en mí podrá apagarse\n", | |
"La llama de tu amor.\"\"\"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Podrá nublarse el sol eternamente;\n", | |
"Podrá secarse en un instante el mar;\n", | |
"Podrá romperse el eje de la Tierra;\n", | |
"Como un débil cristal.\n", | |
"¡Todo sucederá! Podrá la muerte\n", | |
"Cubrirme con su fúnebre crespón;\n", | |
"Pero jamás en mí podrá apagarse\n", | |
"La llama de tu amor.\n" | |
] | |
} | |
], | |
"source": [ | |
"print(text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Podrá nublarse el sol eternamente',\n", | |
" '\\nPodrá secarse en un instante el mar',\n", | |
" '\\nPodrá romperse el eje de la Tierra',\n", | |
" '\\nComo un débil cristal.\\n¡Todo sucederá! Podrá la muerte\\nCubrirme con su fúnebre crespón',\n", | |
" '\\nPero jamás en mí podrá apagarse\\nLa llama de tu amor.']" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Tokenizmos con Python Splits por ';' \n", | |
"text.split(';') " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Podrá',\n", | |
" 'nublarse',\n", | |
" 'el',\n", | |
" 'sol',\n", | |
" 'eternamente',\n", | |
" ';',\n", | |
" 'Podrá',\n", | |
" 'secarse',\n", | |
" 'en',\n", | |
" 'un',\n", | |
" 'instante',\n", | |
" 'el',\n", | |
" 'mar',\n", | |
" ';',\n", | |
" 'Podrá',\n", | |
" 'romperse',\n", | |
" 'el',\n", | |
" 'eje',\n", | |
" 'de',\n", | |
" 'la',\n", | |
" 'Tierra',\n", | |
" ';',\n", | |
" 'Como',\n", | |
" 'un',\n", | |
" 'débil',\n", | |
" 'cristal',\n", | |
" '.',\n", | |
" '¡Todo',\n", | |
" 'sucederá',\n", | |
" '!',\n", | |
" 'Podrá',\n", | |
" 'la',\n", | |
" 'muerte',\n", | |
" 'Cubrirme',\n", | |
" 'con',\n", | |
" 'su',\n", | |
" 'fúnebre',\n", | |
" 'crespón',\n", | |
" ';',\n", | |
" 'Pero',\n", | |
" 'jamás',\n", | |
" 'en',\n", | |
" 'mí',\n", | |
" 'podrá',\n", | |
" 'apagarse',\n", | |
" 'La',\n", | |
" 'llama',\n", | |
" 'de',\n", | |
" 'tu',\n", | |
" 'amor',\n", | |
" '.']" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Tokenizamos por palabra (word). Considera los \";\" como token\n", | |
"\n", | |
"word_tokenize(text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Podrá nublarse el sol eternamente;\\nPodrá secarse en un instante el mar;\\nPodrá romperse el eje de la Tierra;\\nComo un débil cristal.',\n", | |
" '¡Todo sucederá!',\n", | |
" 'Podrá la muerte\\nCubrirme con su fúnebre crespón;\\nPero jamás en mí podrá apagarse\\nLa llama de tu amor.']" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Tokenizamos por frase (sentence): separa los versos\n", | |
"\n", | |
"sent_tokenize(text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['arabic', 'azerbaijani', 'danish', 'dutch', 'english', 'finnish', 'french', 'german', 'greek', 'hungarian', 'indonesian', 'italian', 'kazakh', 'nepali', 'norwegian', 'portuguese', 'romanian', 'russian', 'slovene', 'spanish', 'swedish', 'tajik', 'turkish']\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"[nltk_data] Downloading package stopwords to\n", | |
"[nltk_data] C:\\Users\\cx01869\\AppData\\Roaming\\nltk_data...\n", | |
"[nltk_data] Package stopwords is already up-to-date!\n" | |
] | |
} | |
], | |
"source": [ | |
"# Descargamos las stopwords y vemos qué idiomas están disponibles\n", | |
"\n", | |
"nltk.download('stopwords')\n", | |
"\n", | |
"print(stopwords.fileids()) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"313" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Elegimos las stopwords en español. Pedimos que muestre cuántas hay (313)\n", | |
"\n", | |
"stop_words = set(stopwords.words('spanish')) \n", | |
"\n", | |
"len(stop_words)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Lista de stopwords en español:\n", | |
"{'estuviesen', 'algunas', 'muchos', 'habidos', 'habréis', 'habían', 'al', 'fueras', 'sean', 'vuestro', 'estuvieses', 'fuese', 'hubiéramos', 'erais', 'estad', 'él', 'habíais', 'habríais', 'hubiésemos', 'habidas', 'hubierais', 'estaré', 'sin', 'poco', 'estarían', 'estáis', 'me', 'porque', 'ni', 'estabais', 'habías', 'eso', 'tengo', 'el', 'tuvieran', 'todo', 'tienen', 'seríamos', 'fue', 'qué', 'pero', 'tened', 'estos', 'tenga', 'nuestras', 'estado', 'ha', 'una', 'hubieses', 'han', 'estéis', 'habiendo', 'es', 'los', 'hayan', 'habrá', 'tendría', 'tuyo', 'mía', 'vuestras', 'tengan', 'vosotros', 'tienes', 'habida', 'hubiera', 'nada', 'fuisteis', 'eras', 'tendríamos', 'su', 'sentidas', 'más', 'le', 'tuviésemos', 'muy', 'otra', 'estoy', 'sentido', 'estaría', 'ellas', 'seamos', 'tengamos', 'e', 'sería', 'estén', 'desde', 'nosotros', 'estarías', 'otro', 'estuviésemos', 'entre', 'mis', 'esa', 'nuestro', 'sí', 'cuando', 'hubiesen', 'estuvieseis', 'seréis', 'quien', 'estuviera', 'será', 'hubo', 'éramos', 'tuvieron', 'tuviéramos', 'estadas', 'hubieras', 'seáis', 'suya', 'serías', 'son', 'estuvo', 'este', 'tendrías', 'vosotras', 'suyo', 'estados', 'nosotras', 'sobre', 'estarán', 'fueses', 'antes', 'a', 'fuerais', 'teniendo', 'estuvieron', 'tenida', 'sus', 'está', 'tuviera', 'estuvierais', 'esos', 'habrías', 'habido', 'seríais', 'seas', 'yo', 'mucho', 'sentid', 'hayas', 'contra', 'hubieseis', 'vuestros', 'estás', 'sentidos', 'o', 'tendrían', 'teníamos', 'con', 'hasta', 'seré', 'mías', 'cual', 'y', 'mío', 'ti', 'lo', 'había', 'tu', 'hubiese', 'las', 'otros', 'has', 'sea', 'serás', 'fuéramos', 'estas', 'era', 'siente', 'tuvieses', 'estuviste', 'fui', 'se', 'no', 'fuiste', 'estuvimos', 'habría', 'fueron', 'tendrá', 'tendréis', 'un', 'estada', 'algo', 'tú', 'tuya', 'tendrán', 'para', 'tuvo', 'teníais', 'ella', 'fuera', 'estés', 'estuve', 'serán', 'sois', 'tenías', 'nuestra', 'vuestra', 'somos', 'hubisteis', 'hayamos', 'tenéis', 'hay', 'tendremos', 'algunos', 'os', 'ante', 'estaremos', 'que', 'hubimos', 'tiene', 'fueseis', 'tenía', 'como', 'les', 'estuvieran', 'ya', 'hayáis', 'estuvisteis', 'sentida', 'eres', 'eran', 'tuvierais', 'habré', 'del', 'esas', 'estuviese', 'estemos', 'estarás', 'estará', 'soy', 'tendríais', 'tenidos', 'mi', 'tengas', 'también', 'tuvieseis', 'otras', 'mí', 'estaba', 'durante', 'en', 'esta', 'haya', 'ellos', 'habríamos', 'hube', 'fuésemos', 'tenido', 'suyas', 'están', 'esto', 'estar', 'estamos', 'ese', 'hubieron', 'hubieran', 'estaban', 'seremos', 'fueran', 'tuvisteis', 'donde', 'tuviesen', 'quienes', 'tenían', 'tenemos', 'tendrás', 'estábamos', 'unos', 'nuestros', 'fuimos', 'habíamos', 'estaríais', 'habrás', 'tuvieras', 'nos', 'tanto', 'míos', 'de', 'estaréis', 'estando', 'habéis', 'habrán', 'uno', 'serían', 'fuesen', 'tuyos', 'tuve', 'tuviste', 'tuvimos', 'tenidas', 'tengáis', 'la', 'suyos', 'tuviese', 'estabas', 'estuvieras', 'tus', 'por', 'estuviéramos', 'hemos', 'tendré', 'habremos', 'sintiendo', 'todos', 'esté', 'te', 'estaríamos', 'he', 'hubiste', 'habrían', 'tuyas'}\n" | |
] | |
} | |
], | |
"source": [ | |
"# Para ver las stopwords\n", | |
"\n", | |
"print(\"Lista de stopwords en español:\")\n", | |
"\n", | |
"print(stop_words)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['Podrá', 'nublarse', 'sol', 'eternamente', ';', 'Podrá', 'secarse', 'instante', 'mar', ';', 'Podrá', 'romperse', 'eje', 'Tierra', ';', 'Como', 'débil', 'cristal', '.', '¡Todo', 'sucederá', '!', 'Podrá', 'muerte', 'Cubrirme', 'fúnebre', 'crespón', ';', 'Pero', 'jamás', 'podrá', 'apagarse', 'La', 'llama', 'amor', '.']\n" | |
] | |
} | |
], | |
"source": [ | |
"# Para eliminar las stopwords: quita artículos, preposiciones, conjunciones etc\n", | |
"\n", | |
"en_stops = set(stopwords.words('spanish'))\n", | |
"\n", | |
"word_tokens = word_tokenize(text) \n", | |
" \n", | |
"filtered_sentence = [w for w in word_tokens if not w in stop_words] \n", | |
" \n", | |
"filtered_sentence = [] \n", | |
" \n", | |
"for w in word_tokens: \n", | |
" if w not in stop_words: \n", | |
" filtered_sentence.append(w) \n", | |
" \n", | |
"\n", | |
"print(filtered_sentence) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[('Podrá', 'nublarse', 'el'), ('nublarse', 'el', 'sol'), ('el', 'sol', 'eternamente'), ('sol', 'eternamente', ';'), ('eternamente', ';', 'Podrá'), (';', 'Podrá', 'secarse'), ('Podrá', 'secarse', 'en'), ('secarse', 'en', 'un'), ('en', 'un', 'instante'), ('un', 'instante', 'el'), ('instante', 'el', 'mar'), ('el', 'mar', ';'), ('mar', ';', 'Podrá'), (';', 'Podrá', 'romperse'), ('Podrá', 'romperse', 'el'), ('romperse', 'el', 'eje'), ('el', 'eje', 'de'), ('eje', 'de', 'la'), ('de', 'la', 'Tierra'), ('la', 'Tierra', ';'), ('Tierra', ';', 'Como'), (';', 'Como', 'un'), ('Como', 'un', 'débil'), ('un', 'débil', 'cristal'), ('débil', 'cristal', '.'), ('cristal', '.', '¡Todo'), ('.', '¡Todo', 'sucederá'), ('¡Todo', 'sucederá', '!'), ('sucederá', '!', 'Podrá'), ('!', 'Podrá', 'la'), ('Podrá', 'la', 'muerte'), ('la', 'muerte', 'Cubrirme'), ('muerte', 'Cubrirme', 'con'), ('Cubrirme', 'con', 'su'), ('con', 'su', 'fúnebre'), ('su', 'fúnebre', 'crespón'), ('fúnebre', 'crespón', ';'), ('crespón', ';', 'Pero'), (';', 'Pero', 'jamás'), ('Pero', 'jamás', 'en'), ('jamás', 'en', 'mí'), ('en', 'mí', 'podrá'), ('mí', 'podrá', 'apagarse'), ('podrá', 'apagarse', 'La'), ('apagarse', 'La', 'llama'), ('La', 'llama', 'de'), ('llama', 'de', 'tu'), ('de', 'tu', 'amor'), ('tu', 'amor', '.')]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Para generar n-grams, en particular 3-grams\n", | |
"nltk_tokens = nltk.word_tokenize(text) \n", | |
"print(list(nltk.ngrams(nltk_tokens,3)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment