Skip to content
Open
Show file tree
Hide file tree
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
142 changes: 128 additions & 14 deletions module-3/natural-language-processing/your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import re \n",
"\n",
"def clean_up(s):\n",
" \"\"\"\n",
" Cleans up numbers, URLs, and special characters from a string.\n",
Expand All @@ -79,7 +81,33 @@
"\n",
" Returns:\n",
" A string that has been cleaned up.\n",
" \"\"\""
" \"\"\"\n",
" clean_string=re.sub(r\"http\\S+\", ' ',s)\n",
" clean_string=re.sub(r'\\W', ' ',clean_string)\n",
" clean_string=re.sub(r'\\d', '',clean_string)\n",
" return clean_string.lower()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' ironhack s q website is '"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_string= '''@Ironhack's-#Q website 776-is http://ironhack.com [(2018)]\")'''\n",
"clean_string=clean_up(test_string)\n",
"clean_string"
]
},
{
Expand All @@ -101,9 +129,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['ironhack', 's', 'q', 'website', 'is']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tokenize(s):\n",
" \"\"\"\n",
Expand All @@ -114,7 +153,12 @@
"\n",
" Returns:\n",
" A list of words as the result of tokenization.\n",
" \"\"\""
" \"\"\"\n",
" \n",
" return re.findall(r'\\w+', s)\n",
"\n",
"tokens=tokenize(clean_string)\n",
"tokens"
]
},
{
Expand Down Expand Up @@ -145,10 +189,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from nltk.stem import PorterStemmer\n",
"porter=PorterStemmer()\n",
"\n",
"def stem_and_lemmatize(l):\n",
" \"\"\"\n",
" Perform stemming and lemmatization on a list of words.\n",
Expand All @@ -158,7 +205,57 @@
"\n",
" Returns:\n",
" A list of strings after being stemmed and lemmatized.\n",
" \"\"\""
" \"\"\"\n",
" return [porter.stem(x) for x in l]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ironhack', 's', 'q', 'websit', 'is']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stemmed=stem_and_lemmatize(tokens)\n",
"stemmed"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import nltk \n",
"nltk.download()"
]
},
{
Expand All @@ -176,10 +273,22 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['ironhack', 'q', 'websit']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from nltk.corpus import stopwords\n",
"def remove_stopwords(l):\n",
" \"\"\"\n",
" Remove English stopwords from a list of strings.\n",
Expand All @@ -189,7 +298,12 @@
"\n",
" Returns:\n",
" A list of strings after stop words are removed.\n",
" \"\"\""
" \"\"\"\n",
" stop_words=set(stopwords.words('english'))\n",
" return[x for x in l if not x in stop_words]\n",
" \n",
"\n",
"remove_stopwords(stemmed)"
]
},
{
Expand All @@ -204,9 +318,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 +332,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
}
Loading