From 75ab5af7e1c586a8ec9b4f0f1d077097f21e2b9d Mon Sep 17 00:00:00 2001 From: Alexandre Garanhao Date: Wed, 19 Apr 2023 23:47:27 +0100 Subject: [PATCH] lab done --- your-code/challenge-1.ipynb | 167 ++++++++++++++++++++++++++++++------ your-code/challenge-2.ipynb | 163 +++++++++++++++++++++++++++++------ 2 files changed, 281 insertions(+), 49 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c574eba..5496686 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -33,12 +33,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Durante un tiempo no estuvo segura de si su marido era su marido.\n" + ] + } + ], "source": [ "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", - "# Your code here:\n" + "# Your code here:\n", + "result = ' '.join(str_list)\n", + "result += '.'\n", + "\n", + "print(result)" ] }, { @@ -50,12 +62,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grocery list: bananas, bread, brownie mix, broccoli.\n" + ] + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "filtered_foods = [food.lower() for food in food_list if food.lower().startswith('b')]\n", + "grocery_list = ', '.join(filtered_foods)\n", + "grocery_list = 'Grocery list: ' + grocery_list + '.'\n", + "\n", + "print(grocery_list)" ] }, { @@ -69,9 +94,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "63.61725123519331\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -90,7 +123,10 @@ " # Your code here:\n", " return pi * (x**2)\n", " \n", - "# Your output string here:\n" + "radius = 4.5\n", + "output_string = area(radius)\n", + "# Your output string here:\n", + "print(output_string)" ] }, { @@ -106,9 +142,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax. Perhaps you forgot a comma? (971886667.py, line 12)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[8], line 12\u001b[1;36m\u001b[0m\n\u001b[1;33m poem = [Some, say, the world, will, end, in fire,\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax. Perhaps you forgot a comma?\n" + ] + } + ], "source": [ "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", @@ -120,7 +165,17 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem = [Some, say, the world, will, end, in fire,\n", + "Some say in ice.\n", + "From what I ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.]\n", + "\n" ] }, { @@ -132,9 +187,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'end', 'i', 'was', 'angry', 'with', 'my', 'foe', 'i', 'told', 'not', 'my', 'wrath', 'did', 'grow', 'i', 'waterd', 'fears', 'night', 'morning', 'with', 'my', 'tears', 'i', 'sunned', 'with', 'smiles', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shine', 'he', 'knew', 'that', 'was', 'mine', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +221,18 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem_lower = poem.lower()\n", + "poem_letters_only = ''.join(c for c in poem_lower if c.isalpha() or c.isspace())\n", + "words = poem_letters_only.split()\n", + "words_filtered = [word for word in words if word not in blacklist]\n", + "\n", + "print(words_filtered)\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, { @@ -172,16 +246,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TP\n" + ] + } + ], "source": [ "import re\n", "\n", "poem = \"\"\"The apparition of these faces in the crowd;\n", "Petals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "uppercase_chars = re.findall(r'[A-Z]', poem)\n", + "uppercase_chars_string = ''.join(uppercase_chars)\n", + "\n", + "print(uppercase_chars_string)" ] }, { @@ -193,13 +279,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], "source": [ + "import re\n", "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "filtered_data = [element for element in data if re.search(r'\\d', element) is not None]\n", + "\n", + "print(filtered_data)" ] }, { @@ -215,13 +313,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], "source": [ + "import re\n", "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "filtered_data = [element for element in data if re.search(r'\\d', element) is not None and re.search(r'[a-z]', element) is not None]\n", + "\n", + "print(filtered_data)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -240,7 +357,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.1" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 6873bd2..00b8efa 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -72,11 +72,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bag of Words (BoW):\n", + "['Ironhack.', 'student', 'I', 'am', 'love', 'a', 'cool.', 'Ironhack', 'is', 'at']\n", + "Term Frequency:\n", + "Document 1: [0, 0, 0, 0, 0, 0, 1, 1, 1, 0]\n", + "Document 2: [1, 0, 1, 0, 1, 0, 0, 0, 0, 0]\n", + "Document 3: [1, 1, 1, 1, 0, 1, 0, 0, 0, 1]\n" + ] + } + ], "source": [ - "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']" + "import os\n", + "from collections import Counter\n", + "\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "\n", + "for doc in docs:\n", + " with open(doc, 'r') as f:\n", + " content = f.read()\n", + " corpus.append(content)\n", + "\n", + "words = []\n", + "for document in corpus:\n", + " words += document.split()\n", + "\n", + "bag_of_words = list(set(words)) \n", + "term_freq = []\n", + "for document in corpus:\n", + " word_count = Counter(document.split())\n", + " document_term_freq = [word_count[word] for word in bag_of_words]\n", + " term_freq.append(document_term_freq)\n", + "\n", + "print(\"Bag of Words (BoW):\")\n", + "print(bag_of_words)\n", + "print(\"Term Frequency:\")\n", + "for i, document_tf in enumerate(term_freq, 1):\n", + " print(\"Document {}: {}\".format(i, document_tf))\n" ] }, { @@ -88,11 +126,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "\n", + "corpus = []\n", + "\n", + "for doc in docs:\n", + " with open(doc, 'r') as f:\n", + " content = f.read()\n", + " corpus.append(content)\n", + "\n" ] }, { @@ -104,10 +152,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], + "source": [ + "print(corpus)" + ] }, { "cell_type": "markdown", @@ -132,11 +190,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "# Write your code here\n", + "import re\n", + "\n", + "corpus_lower = [document.lower() for document in corpus]\n", + "\n", + "corpus_processed = []\n", + "for document in corpus_lower:\n", + " # Use regular expression to remove special characters and keep only alphanumeric characters and spaces\n", + " document_processed = re.sub(r'[^a-zA-Z0-9\\s]', '', document)\n", + " corpus_processed.append(document_processed)\n" ] }, { @@ -148,10 +215,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "bag_of_words = []" + ] }, { "cell_type": "markdown", @@ -166,11 +235,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "\n", + "for document in corpus_processed:\n", + " terms = document.split()\n", + " for term in terms:\n", + " if term not in bag_of_words:\n", + " bag_of_words.append(term)\n" ] }, { @@ -186,10 +261,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], + "source": [ + "print(bag_of_words)" + ] }, { "cell_type": "markdown", @@ -200,11 +285,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "\n", + "term_freq = []\n", + "\n", + "for document in corpus_processed:\n", + " terms = document.split()\n", + " \n", + " tf_document = []\n", + " \n", + " for term in bag_of_words:\n", + " tf = terms.count(term)\n", + " tf_document.append(tf)\n", + " \n", + " term_freq.append(tf_document)\n" ] }, { @@ -218,10 +316,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[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": [ + "print(term_freq)" + ] }, { "cell_type": "markdown", @@ -261,8 +369,15 @@ "outputs": [], "source": [ "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", + "import numpy as np\n", + "# Write your code below\n", "\n", - "# Write your code below\n" + "corpus = [\n", + " \"Ironhack is cool\",\n", + " \"I love studying at Ironhack\",\n", + " \"Ironhack has amazing students\"\n", + "]\n", + "\n" ] }, { @@ -318,7 +433,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.1" } }, "nbformat": 4,