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
6 changes: 3 additions & 3 deletions lab-functional-programming/your-code/Learning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -302,9 +302,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
109 changes: 100 additions & 9 deletions lab-functional-programming/your-code/Q1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# Import required libraries\n",
"import pandas as pd\n",
"\n",
"# Define function\n",
"def get_bow_from_docs(docs, stop_words=[]):\n",
" \n",
" # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n",
" \n",
" for file in docs:\n",
" with open(docs) as archivo:\n",
" corpus = archivo.readlines()\n",
" lis = []\n",
" lis.append(corpus)\n",
"\n",
" \n",
" \n",
" # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n",
"\n",
"\n",
" \n",
" \n",
" \"\"\"\n",
" Loop `docs` and read the content of each doc into a string in `corpus`.\n",
Expand Down Expand Up @@ -58,12 +68,80 @@
" \n",
" # Now return your output as an object\n",
" return {\n",
" \"bag_of_words\": bag_of_words,\n",
" \"term_freq\": term_freq\n",
" \"bag_of_words\": corpus\n",
" \n",
" #'rm_freq\": term_freq\n",
" }\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"def get_bow_from_docs(docs, stop_words=[]):\n",
" \n",
" for file in docs:\n",
" with open(docs) as archivo:\n",
" corpus = archivo.read()\n",
" lis = []\n",
" lis.append(corpus)\n",
" print(lis)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n",
"['Ironhack is cool.\\n']\n"
]
}
],
"source": [
"get_bow_from_docs('doc1.txt')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'bag_of_words': ['Ironhack is cool.\\n']}"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_bow_from_docs('doc1.txt','doc2.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -77,7 +155,20 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "UnboundLocalError",
"evalue": "local variable 'bag_of_words' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [21]\u001b[0m, in \u001b[0;36m<cell line: 5>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m docs \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Obtain BoW from your function\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m bow \u001b[38;5;241m=\u001b[39m \u001b[43mget_bow_from_docs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdocs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Print BoW\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(bow)\n",
"Input \u001b[1;32mIn [18]\u001b[0m, in \u001b[0;36mget_bow_from_docs\u001b[1;34m(docs, stop_words)\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 36\u001b[0m \u001b[38;5;124;03mLoop `corpus` again. For each doc string, count the number of occurrences of each term in `bag_of_words`. \u001b[39;00m\n\u001b[0;32m 37\u001b[0m \u001b[38;5;124;03mCreate an array for each doc's term frequency and append it to `term_freq`.\u001b[39;00m\n\u001b[0;32m 38\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 42\u001b[0m \u001b[38;5;66;03m# Now return your output as an object\u001b[39;00m\n\u001b[0;32m 43\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m {\n\u001b[1;32m---> 44\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbag_of_words\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[43mbag_of_words\u001b[49m\n\u001b[0;32m 45\u001b[0m \n\u001b[0;32m 46\u001b[0m \u001b[38;5;66;03m#'rm_freq\": term_freq\u001b[39;00m\n\u001b[0;32m 47\u001b[0m }\n",
"\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'bag_of_words' referenced before assignment"
]
}
],
"source": [
"# Define doc paths array\n",
"docs = []\n",
Expand Down Expand Up @@ -156,7 +247,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -170,9 +261,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions lab-functional-programming/your-code/Q2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -111,9 +111,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
6 changes: 3 additions & 3 deletions lab-functional-programming/your-code/Q3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -199,9 +199,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading