Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 159 additions & 43 deletions module-3/natural-language-processing/your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"def clean_up(s):\n",
" \"\"\"\n",
" Cleans up numbers, URLs, and special characters from a string.\n",
"\n",
" Args:\n",
" s: The string to be cleaned up.\n",
"\n",
" Returns:\n",
" A string that has been cleaned up.\n",
" \"\"\""
" clean = re.sub('[ ]+', ' ', re.sub('([^a-z])|(http(.*)) ', ' ', s.lower()).lstrip().rstrip())\n",
" \n",
" return clean"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ironhack s q website is'"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"clean_up(\"\"\"@Ironhack's-#Q website 776-is http://ironhack.com [(2018)]\")\"\"\")"
]
},
{
Expand All @@ -101,20 +124,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"def tokenize(s):\n",
" \"\"\"\n",
" Tokenize a string.\n",
"\n",
" Args:\n",
" s: String to be tokenized.\n",
"\n",
" Returns:\n",
" A list of words as the result of tokenization.\n",
" \"\"\""
" token = s.split(' ')\n",
" return token"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ironhack', 's', 'q', 'website', 'is']"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tokenize('ironhack s q website is')"
]
},
{
Expand Down Expand Up @@ -145,20 +181,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import spacy"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def stem_and_lemmatize(l):\n",
" \"\"\"\n",
" Perform stemming and lemmatization on a list of words.\n",
"\n",
" Args:\n",
" l: A list of strings.\n",
"\n",
" Returns:\n",
" A list of strings after being stemmed and lemmatized.\n",
" \"\"\""
" nlp = spacy.load('en_core_web_sm')\n",
" tokens = nlp(l)\n",
" for token in tokens:\n",
" print(token.text, token.lemma_, token.pos_)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ironhack ironhack NOUN\n",
"s s PART\n",
"q q NOUN\n",
"website website NOUN\n",
"is be AUX\n"
]
}
],
"source": [
"stem_and_lemmatize('ironhack s q website is')"
]
},
{
Expand All @@ -176,20 +237,75 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package stopwords to /home/emily/nltk_data...\n",
"[nltk_data] Package stopwords is already up-to-date!\n",
"[nltk_data] Downloading package punkt to /home/emily/nltk_data...\n",
"[nltk_data] Package punkt is already up-to-date!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import nltk\n",
"nltk.download('stopwords')\n",
"nltk.download('punkt')"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"from nltk.corpus import stopwords\n",
"from nltk.tokenize import word_tokenize"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"def remove_stopwords(l):\n",
" \"\"\"\n",
" Remove English stopwords from a list of strings.\n",
" stop_words = set(stopwords.words('english'))\n",
" word_tokens = word_tokenize(l)\n",
" filtered = [w for w in word_tokens if not w in stop_words]\n",
"\n",
" Args:\n",
" l: A list of strings.\n",
"\n",
" Returns:\n",
" A list of strings after stop words are removed.\n",
" \"\"\""
" print(filtered)\n"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ironhack', 'q', 'website']\n"
]
}
],
"source": [
"remove_stopwords('ironhack s q website is')"
]
},
{
Expand All @@ -204,9 +320,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [conda env:ironhack_env]",
"language": "python",
"name": "python3"
"name": "conda-env-ironhack_env-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -218,9 +334,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}