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
85 changes: 43 additions & 42 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"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",
" \"\"\""
" import re\n",
" s = re.sub(r'http\\S+', '', s)\n",
" \n",
" clean_s = \"\"\n",
" for _ in range(0,len(s)):\n",
" if s[_].isalpha():\n",
" clean_s += s[_].lower()\n",
" else:\n",
" clean_s += \" \"\n",
" \n",
" return clean_s"
]
},
{
Expand All @@ -101,20 +103,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"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",
" \"\"\""
" import nltk\n",
" from nltk.tokenize import word_tokenize\n",
" nltk.download(\"punkt\")\n",
" \n",
" tokens = word_tokenize(s)\n",
" \n",
" return tokens"
]
},
{
Expand Down Expand Up @@ -145,20 +145,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"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",
" \"\"\""
" from nltk.stem import PorterStemmer, LancasterStemmer, SnowballStemmer\n",
" \n",
" ps = SnowballStemmer(language = \"english\")\n",
" stemmed = [ps.stem(w) for w in l]\n",
" \n",
" nltk.download('wordnet')\n",
" from nltk.stem import WordNetLemmatizer\n",
" from nltk.corpus import wordnet\n",
" nltk.download('omw-1.4')\n",
"\n",
" lemmatizer = WordNetLemmatizer()\n",
" lemmatized = [lemmatizer.lemmatize(word) for word in stemmed]\n",
" \n",
" return lemmatized"
]
},
{
Expand All @@ -176,20 +181,16 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def remove_stopwords(l):\n",
" \"\"\"\n",
" Remove English stopwords from a list of strings.\n",
"\n",
" Args:\n",
" l: A list of strings.\n",
"\n",
" Returns:\n",
" A list of strings after stop words are removed.\n",
" \"\"\""
" from nltk.corpus import stopwords\n",
" nltk.download('stopwords')\n",
" \n",
" without_sw = [word for word in l if word not in stopwords.words()]\n",
" return without_sw"
]
},
{
Expand All @@ -204,7 +205,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -218,7 +219,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.11.4"
}
},
"nbformat": 4,
Expand Down
Loading