Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding my answers to the tuples, dics ans sets lab #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
146 changes: 124 additions & 22 deletions module-1_labs/lab-functional-programming/your-code/Q1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,98 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n",
"['ironhack', 'is', 'cool']\n",
"['i', 'love', 'ironhack']\n",
"['i', 'am', 'a', 'student', 'at', 'ironhack']\n",
"[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]\n",
"{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]}\n",
"['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n",
"['ironhack', 'is', 'cool']\n",
"['i', 'love', 'ironhack']\n",
"['i', 'am', 'a', 'student', 'at', 'ironhack']\n",
"[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n",
"{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}\n"
]
}
],
"source": [
"# Import required libraries\n",
"\n",
"import sys\n",
"import sklearn\n",
"# Define function\n",
"def get_bow_from_docs(docs, stop_words=[]):\n",
"\n",
" \n",
"# # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n",
"stop_words = ['all', 'six', 'less', 'being', 'indeed', 'over', 'move', 'anyway', 'fifty', 'four', 'not', 'own', 'through', 'yourselves', 'go', 'where', 'mill', 'only', 'find', 'before', 'one', 'whose', 'system', 'how', 'somewhere', 'with', 'thick', 'show', 'had', 'enough', 'should', 'to', 'must', 'whom', 'seeming', 'under', 'ours', 'has', 'might', 'thereafter', 'latterly', 'do', 'them', 'his', 'around', 'than', 'get', 'very', 'de', 'none', 'cannot', 'every', 'whether', 'they', 'front', 'during', 'thus', 'now', 'him', 'nor', 'name', 'several', 'hereafter', 'always', 'who', 'cry', 'whither', 'this', 'someone', 'either', 'each', 'become', 'thereupon', 'sometime', 'side', 'two', 'therein', 'twelve', 'because', 'often', 'ten', 'our', 'eg', 'some', 'back', 'up', 'namely', 'towards', 'are', 'further', 'beyond', 'ourselves', 'yet', 'out', 'even', 'will', 'what', 'still', 'for', 'bottom', 'mine', 'since', 'please', 'forty', 'per', 'its', 'everything', 'behind', 'un', 'above', 'between', 'it', 'neither', 'seemed', 'ever', 'across', 'she', 'somehow', 'be', 'we', 'full', 'never', 'sixty', 'however', 'here', 'otherwise', 'were', 'whereupon', 'nowhere', 'although', 'found', 'alone', 're', 'along', 'fifteen', 'by', 'both', 'about', 'last', 'would', 'anything', 'via', 'many', 'could', 'thence', 'put', 'against', 'keep', 'etc', 'amount', 'became', 'ltd', 'hence', 'onto', 'or', 'con', 'among', 'already', 'co', 'afterwards', 'formerly', 'within', 'seems', 'into', 'others', 'while', 'whatever', 'except', 'down', 'hers', 'everyone', 'done', 'least', 'another', 'whoever', 'moreover', 'couldnt', 'throughout', 'anyhow', 'yourself', 'three', 'from', 'her', 'few', 'together', 'top', 'there', 'due', 'been', 'next', 'anyone', 'eleven', 'much', 'call', 'therefore', 'interest', 'then', 'thru', 'themselves', 'hundred', 'was', 'sincere', 'empty', 'more', 'himself', 'elsewhere', 'mostly', 'on', 'fire', 'am', 'becoming', 'hereby', 'amongst', 'else', 'part', 'everywhere', 'too', 'herself', 'former', 'those', 'he', 'me', 'myself', 'made', 'twenty', 'these', 'bill', 'cant', 'us', 'until', 'besides', 'nevertheless', 'below', 'anywhere', 'nine', 'can', 'of', 'your', 'toward', 'my', 'something', 'and', 'whereafter', 'whenever', 'give', 'almost', 'wherever', 'is', 'describe', 'beforehand', 'herein', 'an', 'as', 'itself', 'at', 'have', 'in', 'seem', 'whence', 'ie', 'any', 'fill', 'again', 'hasnt', 'inc', 'thereby', 'thin', 'no', 'perhaps', 'latter', 'meanwhile', 'when', 'detail', 'same', 'wherein', 'beside', 'also', 'that', 'other', 'take', 'which', 'becomes', 'you', 'if', 'nobody', 'see', 'though', 'may', 'after', 'upon', 'most', 'hereupon', 'eight', 'but', 'serious', 'nothing', 'such', 'why', 'a', 'off', 'whereby', 'third', 'i', 'whole', 'noone', 'sometimes', 'well', 'amoungst', 'yours', 'their', 'rather', 'without', 'so', 'five', 'the', 'first', 'whereas', 'once']\n",
"docs = ['doc1.txt', 'doc2.txt', 'doc3.txt'] \n",
"\n",
"def lowecases_n_remove_punctuation(corpus):\n",
" new_corpus = []\n",
" for i in corpus:\n",
" i = i.replace('.', '').lower()\n",
" new_corpus.append(i)\n",
" return new_corpus\n",
" \n",
" \n",
" # In the function, first define the variables you will use such as `corpus`, `bag_of_words`, and `term_freq`.\n",
"def make_a_unique_word_list(corpus, stop_words= []):\n",
" bag_of_words = []\n",
" for i in corpus:\n",
" i = i.split(\" \")\n",
" for j in i:\n",
" if j not in bag_of_words and j not in stop_words:\n",
" bag_of_words.append(j)\n",
" return bag_of_words\n",
"\n",
"def find_freq_word_in_list(corpus, bag_of_words):\n",
" term_freq = []\n",
" for string in corpus:\n",
" new_string=string.split()\n",
" print(new_string)\n",
" temp=[]\n",
" for word in bag_of_words:\n",
" temp.append(new_string.count(word))\n",
" term_freq.append(temp)\n",
" print(term_freq)\n",
" return term_freq\n",
"\n",
"\n",
"def get_bow_from_docs(docs, stop_words= []):\n",
" corpus = []\n",
" bag_of_words = []\n",
" term_freq = []\n",
" for string in docs: \n",
" with open(string) as file:\n",
" corpus.append(file.read())\n",
" corpus = lowecases_n_remove_punctuation(corpus)\n",
" print(corpus)\n",
" bag_of_words = make_a_unique_word_list(corpus, stop_words)\n",
" term_freq = find_freq_word_in_list(corpus, bag_of_words)\n",
" return {\n",
" \"bag_of_words\": bag_of_words,\n",
" \"term_freq\": term_freq }\n",
" \n",
" \n",
" \n",
" \"\"\"\n",
" \n",
"\n",
" \n",
" \n",
"\"\"\"\n",
" Loop `docs` and read the content of each doc into a string in `corpus`.\n",
" Remember to convert the doc content to lowercases and remove punctuation.\n",
" \"\"\"\n",
"\n",
" \n",
" \n",
" \"\"\"\n",
"\"\"\"\n",
" Loop `corpus`. Append the terms in each doc into the `bag_of_words` array. The terms in `bag_of_words` \n",
" should be unique which means before adding each term you need to check if it's already added to the array.\n",
" In addition, check if each term is in the `stop_words` array. Only append the term to `bag_of_words`\n",
Expand All @@ -49,19 +120,16 @@
" \n",
" \n",
" \n",
" \"\"\"\n",
"\"\"\"\n",
" Loop `corpus` again. For each doc string, count the number of occurrences of each term in `bag_of_words`. \n",
" Create an array for each doc's term frequency and append it to `term_freq`.\n",
" \"\"\"\n",
"\n",
" \n",
" \n",
" # Now return your output as an object\n",
" return {\n",
" \"bag_of_words\": bag_of_words,\n",
" \"term_freq\": term_freq\n",
" }\n",
" "
"print(get_bow_from_docs(docs))\n",
"print(get_bow_from_docs(docs, stop_words))"
]
},
{
Expand All @@ -75,12 +143,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n",
"['ironhack', 'is', 'cool']\n",
"['i', 'love', 'ironhack']\n",
"['i', 'am', 'a', 'student', 'at', 'ironhack']\n",
"[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]\n",
"{'bag_of_words': ['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at'], 'term_freq': [[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]}\n"
]
}
],
"source": [
"# Define doc paths array\n",
"docs = []\n",
"docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n",
"\n",
"# Obtain BoW from your function\n",
"bow = get_bow_from_docs(docs)\n",
Expand All @@ -100,9 +181,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"frozenset({'enough', 'eleven', 'not', 'once', 'wherein', 'anyhow', 'together', 'found', 'as', 'thereby', 'sometimes', 'beside', 'sixty', 'while', 'ourselves', 'have', 'via', 'otherwise', 'de', 'name', 'could', 'back', 'own', 'at', 'why', 'twelve', 'anyone', 'amongst', 'thick', 'however', 'down', 'nowhere', 'of', 'hundred', 'front', 'their', 'ours', 'another', 'before', 'than', 'from', 'bill', 'when', 'several', 'show', 'there', 'seeming', 'rather', 'yourself', 'inc', 'myself', 'amoungst', 'nor', 'next', 'further', 'much', 'last', 'to', 'no', 'been', 'under', 'here', 'take', 'they', 'we', 'both', 'or', 'these', 'then', 'too', 'mostly', 'couldnt', 'top', 'was', 'whole', 'whoever', 'cry', 'nevertheless', 'everyone', 'in', 'be', 'am', 'itself', 'your', 'my', 'due', 'were', 'she', 'forty', 'since', 'fire', 'eg', 'over', 'thereupon', 'this', 'still', 'between', 'two', 'i', 'call', 'side', 'might', 'hereby', 'noone', 'until', 'whether', 'somewhere', 'already', 'yourselves', 'seemed', 'whence', 'nine', 'fifteen', 'namely', 'again', 'indeed', 'thin', 'hereupon', 'along', 'seems', 'yet', 'amount', 'will', 'less', 'everything', 'full', 'empty', 'done', 'what', 'being', 'do', 'that', 'becomes', 'where', 'those', 'such', 'its', 'against', 'whom', 'fill', 'during', 'onto', 'anywhere', 'others', 'whereafter', 'among', 'go', 'should', 'became', 'interest', 'perhaps', 'made', 'any', 'wherever', 'formerly', 'only', 'six', 'if', 'even', 'etc', 'see', 'it', 'somehow', 'system', 'becoming', 'therefore', 'can', 'though', 'thus', 'mill', 'cannot', 'fifty', 'hers', 'must', 'the', 'give', 'across', 'behind', 'except', 'yours', 'he', 'beyond', 'also', 'because', 'few', 'within', 'through', 'un', 'mine', 'around', 'always', 'get', 'most', 'same', 'five', 'how', 'sometime', 'whither', 'whenever', 'beforehand', 'ten', 'more', 'our', 'serious', 'whereas', 'after', 'whatever', 'into', 'something', 'former', 'neither', 'each', 'herself', 'but', 'first', 'alone', 'keep', 'upon', 'every', 'off', 'moreover', 'detail', 'con', 'co', 'whereby', 'eight', 'you', 'sincere', 'which', 'without', 'never', 'either', 'third', 'is', 'anything', 'become', 'would', 'bottom', 'afterwards', 'toward', 'one', 'although', 'find', 'who', 'besides', 'therein', 'many', 'hence', 'are', 'her', 'with', 'on', 'else', 'ever', 'well', 'meanwhile', 'thru', 'has', 'describe', 'put', 'twenty', 're', 'all', 'anyway', 'up', 'for', 'part', 'whereupon', 'whose', 'other', 'four', 'now', 'above', 'cant', 'a', 'and', 'nothing', 'throughout', 'someone', 'may', 'least', 'almost', 'themselves', 'below', 'per', 'himself', 'us', 'three', 'everywhere', 'none', 'me', 'ltd', 'out', 'latterly', 'ie', 'seem', 'hasnt', 'nobody', 'herein', 'thence', 'some', 'about', 'often', 'by', 'had', 'latter', 'hereafter', 'elsewhere', 'him', 'an', 'so', 'please', 'move', 'them', 'very', 'his', 'towards', 'thereafter'})\n"
]
}
],
"source": [
"from sklearn.feature_extraction import stop_words\n",
"print(stop_words.ENGLISH_STOP_WORDS)"
Expand All @@ -128,11 +217,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n",
"['ironhack', 'is', 'cool']\n",
"['i', 'love', 'ironhack']\n",
"['i', 'am', 'a', 'student', 'at', 'ironhack']\n",
"[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n",
"{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}\n"
]
}
],
"source": [
"bow = get_bow_from_docs(bow, stop_words.ENGLISH_STOP_WORDS)\n",
"bow = get_bow_from_docs(docs, stop_words.ENGLISH_STOP_WORDS)\n",
"\n",
"print(bow)"
]
Expand Down Expand Up @@ -170,7 +272,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down
171 changes: 156 additions & 15 deletions module-1_labs/lab-functional-programming/your-code/Q2.ipynb

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions module-1_labs/lab-functional-programming/your-code/Q3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1, -100, -99]\n"
]
}
],
"source": [
"numbers = [1, 4, -1, -100, 0, 5, -99]\n",
"\n",
"filtered_numbers =list(filter(lambda x : x < 0, numbers))\n",
"print(filtered_numbers)\n",
"# Enter your code below"
]
},
Expand All @@ -113,14 +122,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-7-6b3c5693021c>, line 4)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-7-6b3c5693021c>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m only_english = reduce((lambda i, \" \".join(i) : langdetect.detect(i) == 'eb' ) , words)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"import langdetect\n",
"from functools import reduce\n",
"words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n",
"\n",
"only_english = reduce((lambda i, b \" \".join(i) : langdetect.detect(i) == 'eb' ) , words)\n",
"# Enter your code below"
]
},
Expand Down Expand Up @@ -199,7 +217,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down
73 changes: 73 additions & 0 deletions module-1_labs/lab-functional-programming/your-code/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-9]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[0-9]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'A' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-01d412d7a875>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mZ\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'A' is not defined"
]
}
],
"source": [
"print[A-Z]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading