From 7fecbcbd8375b8fddad7cce940b96deda1f72a7e Mon Sep 17 00:00:00 2001 From: JoaquinGonzalezSimon Date: Fri, 25 Mar 2022 16:29:12 -0600 Subject: [PATCH 1/2] Challenges complete --- .../challenge-1-checkpoint.ipynb | 574 ++++++++++++++++++ .../challenge-2-checkpoint.ipynb | 245 +++++++- your-code/challenge-1.ipynb | 389 +++++++++++- your-code/challenge-2.ipynb | 245 +++++++- 4 files changed, 1384 insertions(+), 69 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb new file mode 100644 index 0000000..350be1e --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,574 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# String Operations Lab\n", + "\n", + "**Before your start:**\n", + "\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Combining Strings\n", + "\n", + "Combining strings is an important skill to acquire. There are multiple ways of combining strings in Python, as well as combining strings with variables. We will explore this in the first challenge. In the cell below, combine the strings in the list and add spaces between the strings (do not add a space after the last string). Insert a period after the last string." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "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", + "\n", + "string = \"\"\n", + "for i in range (len(str_list)):\n", + " if i < len(str_list)-1:\n", + " string = string + str(str_list[i]) + \" \"\n", + " else:\n", + " string = string + str(str_list[i]) + \".\"\n", + "\n", + "print (string)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "string = \" \".join(str_list) + \".\"\n", + "string" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, use the list of strings to create a grocery list. Start the list with the string `Grocery list: ` and include a comma and a space between each item except for the last one. Include a period at the end. Only include foods in the list that start with the letter 'b' and ensure all foods are lower case." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "grocery list: bananas, brownie mix, broccoli.\n" + ] + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n", + "\n", + "Grocery_List = \"Grocery List: \"\n", + "for i in range(len(food_list)):\n", + " if i < len(food_list)-1:\n", + " if food_list[i][0] == \"B\": \n", + " Grocery_List = Grocery_List + str(food_list[i]) + \", \"\n", + " else: \n", + " Grocery_List = Grocery_List + str(food_list[i]) + \".\"\n", + "\n", + "print (Grocery_List.lower())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, write a function that computes the area of a circle using its radius. Compute the area of the circle and insert the radius and the area between the two strings. Make sure to include spaces between the variable and the strings. \n", + "\n", + "Note: You can use the techniques we have learned so far or use f-strings. F-strings allow us to embed code inside strings. You can read more about f-strings [here](https://www.python.org/dev/peps/pep-0498/)." + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5, is: 63.61725123519331\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "string1 = \"The area of the circle with radius:\"\n", + "string2 = \"is:\"\n", + "radius = 4.5\n", + "\n", + "def area(x, pi = math.pi):\n", + " # This function takes a radius and returns the area of a circle. We also pass a default value for pi.\n", + " # Input: Float (and default value for pi)\n", + " # Output: Float\n", + " \n", + " # Sample input: 5.0\n", + " # Sample Output: 78.53981633\n", + " \n", + " # Your code here:\n", + " if isinstance(x,float):\n", + " x = float(x)\n", + " area_circle = pi * x**2\n", + " return area_circle\n", + " else:\n", + " print (x, \"is not a number\")\n", + " \n", + "# Your output string here:\n", + "print (f'{string1} {radius}, {string2} {area(radius)}')" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number: 5\n", + "78.53981633974483\n" + ] + } + ], + "source": [ + "# No fue necesario, esto lo veremos más adelante\n", + "import math\n", + "a = input(\"Enter a number: \")\n", + "pi = math.pi\n", + "if a.replace('.','',1).isdigit():\n", + " a = float(a)\n", + " area = pi * a**2\n", + "else:\n", + " print (a, \"is not a number\")\n", + "\n", + "print (area)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Splitting Strings\n", + "\n", + "We have first looked at combining strings into one long string. There are times where we need to do the opposite and split the string into smaller components for further analysis. \n", + "\n", + "In the cell below, split the string into a list of strings using the space delimiter. Count the frequency of each word in the string in a dictionary. Strip the periods, line breaks and commas from the text. Make sure to remove empty strings from your dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire,', 'Some', 'say', 'in', 'ice.', 'From', 'what', 'I’ve', 'tasted', 'of', 'desire', 'I', 'hold', 'with', 'those', 'who', 'favor', 'fire.', 'But', 'if', 'it', 'had', 'to', 'perish', 'twice,', 'I', 'think', 'I', 'know', 'enough', 'of', 'hate', 'To', 'say', 'that', 'for', 'destruction', 'ice', 'Is', 'also', 'great', 'And', 'would', 'suffice.']\n" + ] + } + ], + "source": [ + "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", + "# Your code here:\n", + "lst_str = poem.split()\n", + "print (lst_str)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire,': 1, 'ice.': 1, 'From': 1, 'what': 1, 'I’ve': 1, 'tasted': 1, 'of': 2, 'desire': 1, 'I': 3, 'hold': 1, 'with': 1, 'those': 1, 'who': 1, 'favor': 1, 'fire.': 1, 'But': 1, 'if': 1, 'it': 1, 'had': 1, 'to': 1, 'perish': 1, 'twice,': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 1, 'To': 1, 'that': 1, 'for': 1, 'destruction': 1, 'ice': 1, 'Is': 1, 'also': 1, 'great': 1, 'And': 1, 'would': 1, 'suffice.': 1}\n" + ] + } + ], + "source": [ + "# lst_dict = {x:lst_str.count(i) for x in lst_str} esta es otra alternativa\n", + "\n", + "for i in lst_str:\n", + " lst_dict[i] = lst_str.count(i)\n", + "print (lst_dict)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Some say the world will end in fireSome say in iceFrom what I’ve tasted of desireI hold with those who favor fireBut if it had to perish twiceI think I know enough of hateTo say that for destruction iceIs also greatAnd would suffice\n" + ] + } + ], + "source": [ + "poem2 = poem.replace(\".\",\"\")\n", + "poem3 = poem2.replace(\",\",\"\")\n", + "poem4 = poem3.replace(\"\\n\",\"\")\n", + "\n", + "print (poem4)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, find all the words that appear in the text and do not appear in the blacklist. You must parse the string but can choose any data structure you wish for the words that do not appear in the blacklist. Remove all non letter characters and convert all words to lower case." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", + "\n", + "poem = \"\"\"I was angry with my friend; \n", + "I told my wrath, my wrath did end.\n", + "I was angry with my foe: \n", + "I told it not, my wrath did grow. \n", + "\n", + "And I waterd it in fears,\n", + "Night & morning with my tears: \n", + "And I sunned it with smiles,\n", + "And with soft deceitful wiles. \n", + "\n", + "And it grew both day and night. \n", + "Till it bore an apple bright. \n", + "And my foe beheld it shine,\n", + "And he knew that it was mine. \n", + "\n", + "And into my garden stole, \n", + "When the night had veild the pole; \n", + "In the morning glad I see; \n", + "My foe outstretched beneath the tree.\"\"\"\n", + "\n", + "# Your code here:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "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.', 'And', 'I', 'waterd', 'fears,', 'Night', '&', 'morning', 'with', 'my', 'tears:', 'And', 'I', 'sunned', 'with', 'smiles,', 'And', 'with', 'soft', 'deceitful', 'wiles.', 'And', 'grew', 'both', 'day', 'night.', 'Till', 'bore', 'apple', 'bright.', 'And', 'my', 'foe', 'beheld', 'shine,', 'And', 'he', 'knew', 'that', 'was', 'mine.', 'And', 'into', 'my', 'garden', 'stole,', 'When', 'night', 'had', 'veild', 'pole;', 'In', 'morning', 'glad', 'I', 'see;', 'My', 'foe', 'outstretched', 'beneath', 'tree.']\n" + ] + } + ], + "source": [ + "poem_lst = poem.split()\n", + "\n", + "poem_noblacklist=[]\n", + "\n", + "for i in poem_lst:\n", + " if i in blacklist:\n", + " pass\n", + " else:\n", + " poem_noblacklist.append(i)\n", + "\n", + "print (poem_noblacklist)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "IwasangrywithmyfriendItoldmywrathmywrathdidendIwasangrywithmyfoeItolditnotmywrathdidgrowAndIwaterditinfearsNightmorningwithmytearsAndIsunneditwithsmilesAndwithsoftdeceitfulwilesAnditgrewbothdayandnightTillitboreanapplebrightAndmyfoebehelditshineAndheknewthatitwasmineAndintomygardenstoleWhenthenighthadveildthepoleInthemorninggladIseeMyfoeoutstretchedbeneaththetree\n", + "iwasangrywithmyfrienditoldmywrathmywrathdidendiwasangrywithmyfoeitolditnotmywrathdidgrowandiwaterditinfearsnightmorningwithmytearsandisunneditwithsmilesandwithsoftdeceitfulwilesanditgrewbothdayandnighttillitboreanapplebrightandmyfoebehelditshineandheknewthatitwasmineandintomygardenstolewhenthenighthadveildthepoleinthemorninggladiseemyfoeoutstretchedbeneaththetree\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "no_alphanum = re.findall(r\"\\W\", poem)\n", + "\n", + "#print (no_alphanum)\n", + "\n", + "poem_no_alphanum = \"\"\n", + "\n", + "for i in poem:\n", + " if i in no_alphanum:\n", + " pass\n", + " else:\n", + " poem_no_alphanum = poem_no_alphanum + i\n", + "\n", + "print (poem_no_alphanum)\n", + "print (poem_no_alphanum.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i was angry with my friend; \n", + "i told my wrath, my wrath did end.\n", + "i was angry with my foe: \n", + "i told it not, my wrath did grow. \n", + "\n", + "and i waterd it in fears,\n", + "night & morning with my tears: \n", + "and i sunned it with smiles,\n", + "and with soft deceitful wiles. \n", + "\n", + "and it grew both day and night. \n", + "till it bore an apple bright. \n", + "and my foe beheld it shine,\n", + "and he knew that it was mine. \n", + "\n", + "and into my garden stole, \n", + "when the night had veild the pole; \n", + "in the morning glad i see; \n", + "my foe outstretched beneath the tree.\n" + ] + } + ], + "source": [ + "print (poem.lower())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 3 - Regular Expressions\n", + "\n", + "Sometimes, we would like to perform more complex manipulations of our string. This is where regular expressions come in handy. In the cell below, return all characters that are upper case from the string specified below." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['T', 'P']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "import re\n", + "\n", + "re.findall(r'[A-Z]', poem)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, filter the list provided and return all elements of the list containing a number. To filter the list, use the `re.search` function. Check if the function does not return `None`. You can read more about the `re.search` function [here](https://docs.python.org/3/library/re.html)." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "text/plain": [ + "['1', '2', '3', '1', '2', '3', '1', '4']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "data2=\" \".join(data)\n", + "\n", + "print (type(data))\n", + "# Your code here:\n", + "re.findall(r'[0-9]', data2)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "text/plain": [ + "['1', '2', '3', '1', '2', '3', '1', '4']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "data = str(data)\n", + "print (type(data))\n", + "# Your code here:\n", + "re.findall(r'[0-9]', data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Regular Expressions II\n", + "\n", + "In the cell below, filter the list provided to keep only strings containing at least one digit and at least one lower case letter. As in the previous question, use the `re.search` function and check that the result is not `None`.\n", + "\n", + "To read more about regular expressions, check out [this link](https://developers.google.com/edu/python/regular-expressions)." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "# Your code here:\n", + "data = str(data)\n", + "match = re.search(r'', data)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index d76069e..0a53500 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,98 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], + "source": [ + "import os\n", + "import pandas as pd\n", + "import numpy as np\n", + "import math\n", + "import re\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ironhack is cool.\n" + ] + } + ], + "source": [ + "with open('../your-code/doc1.txt', 'r') as fr:\n", + " texto = fr.readline()\n", + "print(texto)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "docts = [f for f in os.listdir('../your-code') if f.endswith('.txt')]\n", + "path = '../your-code/'\n", + "dataset = []\n", + "x = 0\n", + "for i in docts:\n", + " filename = path + docts[x]\n", + " x += 1\n", + " with open(filename,'r')as fr:\n", + " texto = fr.readline()\n", + " dataset.append(texto) \n", + "dataset" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "# Write your code here\n", + "docts = [f for f in os.listdir('../your-code') if f.endswith('.txt')]\n", + "path = '../your-code/'\n", + "\n", + "x = 0\n", + "for i in docts:\n", + " filename = path + docts[x]\n", + " x += 1\n", + " with open(filename,'r')as fr:\n", + " texto = fr.readline()\n", + " corpus.append(texto) \n", + "\n", + "#corpus = [pd.concat(dataset, axis=1)]\n", + "#print (doctos)\n", + "#print (dataset)\n", + "print (corpus)" ] }, { @@ -106,9 +191,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +229,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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 I love Ironhack I am a student at Ironhack\n", + "ironhack is cool i love ironhack i am a student at ironhack\n" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "corpus2 = str(corpus)\n", + "corpus3 = corpus2.replace(\".\",\"\").replace(\",\",\"\").replace(\"'\",\"\").replace(\"[\",\"\").replace(\"]\",\"\")\n", + "corpus4 = corpus3.lower()\n", + "\n", + "print(corpus2)\n", + "print(corpus3)\n", + "print(corpus4)" ] }, { @@ -152,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +282,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack',\n", + " 'is',\n", + " 'cool',\n", + " 'i',\n", + " 'love',\n", + " 'ironhack',\n", + " 'i',\n", + " 'am',\n", + " 'a',\n", + " 'student',\n", + " 'at',\n", + " 'ironhack']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "corpus5 = corpus4.split()\n", + "corpus5\n", + "#corpus7 = corpus5.replace(\"[\",\"\").replace(\"]\",\"\").replace('\"',\"\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], + "source": [ + "for i in corpus5:\n", + " if i in bag_of_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(i)\n", + "\n", + "print (bag_of_words)" ] }, { @@ -192,9 +350,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,12 +374,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ironhack is cool.\n", + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + }, + { + "data": { + "text/plain": [ + "[0, 1, 1, 1, 0, 0, 1, 0, 0]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "print(corpus[0])\n", + "print(bag_of_words)\n", "term_freq = []\n", "\n", + "for i in bag_of_words:\n", + " countword = corpus[0].count(i)\n", + " term_freq.append(countword)\n", + "term_freq\n", + "#for i in corpus:\n", + "# for word in bag_of_words:\n", + " \n", + "\n", "# Write your code here" ] }, @@ -228,9 +423,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 1, 0, 0, 1, 0, 0]\n" + ] + } + ], "source": [ "print(term_freq)" ] @@ -268,7 +471,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -309,7 +512,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +526,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.2" } }, "nbformat": 4, diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..2b1c457 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "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", + "\n", + "string = \"\"\n", + "for i in range (len(str_list)):\n", + " if i < len(str_list)-1:\n", + " string = string + str(str_list[i]) + \" \"\n", + " else:\n", + " string = string + str(str_list[i]) + \".\"\n", + "\n", + "print (string)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "string = \" \".join(str_list) + \".\"\n", + "string" ] }, { @@ -50,14 +91,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "grocery list: bananas, 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", + "\n", + "Grocery_List = \"Grocery List: \"\n", + "for i in range(len(food_list)):\n", + " if i < len(food_list)-1:\n", + " if food_list[i][0] == \"B\": \n", + " Grocery_List = Grocery_List + str(food_list[i]) + \", \"\n", + " else: \n", + " Grocery_List = Grocery_List + str(food_list[i]) + \".\"\n", + "\n", + "print (Grocery_List.lower())" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -69,9 +135,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5, is: 63.61725123519331\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -88,9 +162,43 @@ " # Sample Output: 78.53981633\n", " \n", " # Your code here:\n", - " \n", - " \n", - "# Your output string here:" + " if isinstance(x,float):\n", + " x = float(x)\n", + " area_circle = pi * x**2\n", + " return area_circle\n", + " else:\n", + " print (x, \"is not a number\")\n", + " \n", + "# Your output string here:\n", + "print (f'{string1} {radius}, {string2} {area(radius)}')" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number: 5\n", + "78.53981633974483\n" + ] + } + ], + "source": [ + "# No fue necesario, esto lo veremos más adelante\n", + "import math\n", + "a = input(\"Enter a number: \")\n", + "pi = math.pi\n", + "if a.replace('.','',1).isdigit():\n", + " a = float(a)\n", + " area = pi * a**2\n", + "else:\n", + " print (a, \"is not a number\")\n", + "\n", + "print (area)" ] }, { @@ -106,9 +214,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire,', 'Some', 'say', 'in', 'ice.', 'From', 'what', 'I’ve', 'tasted', 'of', 'desire', 'I', 'hold', 'with', 'those', 'who', 'favor', 'fire.', 'But', 'if', 'it', 'had', 'to', 'perish', 'twice,', 'I', 'think', 'I', 'know', 'enough', 'of', 'hate', 'To', 'say', 'that', 'for', 'destruction', 'ice', 'Is', 'also', 'great', 'And', 'would', 'suffice.']\n" + ] + } + ], "source": [ "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", @@ -120,7 +236,51 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "lst_str = poem.split()\n", + "print (lst_str)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire,': 1, 'ice.': 1, 'From': 1, 'what': 1, 'I’ve': 1, 'tasted': 1, 'of': 2, 'desire': 1, 'I': 3, 'hold': 1, 'with': 1, 'those': 1, 'who': 1, 'favor': 1, 'fire.': 1, 'But': 1, 'if': 1, 'it': 1, 'had': 1, 'to': 1, 'perish': 1, 'twice,': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 1, 'To': 1, 'that': 1, 'for': 1, 'destruction': 1, 'ice': 1, 'Is': 1, 'also': 1, 'great': 1, 'And': 1, 'would': 1, 'suffice.': 1}\n" + ] + } + ], + "source": [ + "# lst_dict = {x:lst_str.count(i) for x in lst_str} esta es otra alternativa\n", + "\n", + "for i in lst_str:\n", + " lst_dict[i] = lst_str.count(i)\n", + "print (lst_dict)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Some say the world will end in fireSome say in iceFrom what I’ve tasted of desireI hold with those who favor fireBut if it had to perish twiceI think I know enough of hateTo say that for destruction iceIs also greatAnd would suffice\n" + ] + } + ], + "source": [ + "poem2 = poem.replace(\".\",\"\")\n", + "poem3 = poem2.replace(\",\",\"\")\n", + "poem4 = poem3.replace(\"\\n\",\"\")\n", + "\n", + "print (poem4)\n" ] }, { @@ -132,7 +292,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -161,6 +321,101 @@ "# Your code here:\n" ] }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "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.', 'And', 'I', 'waterd', 'fears,', 'Night', '&', 'morning', 'with', 'my', 'tears:', 'And', 'I', 'sunned', 'with', 'smiles,', 'And', 'with', 'soft', 'deceitful', 'wiles.', 'And', 'grew', 'both', 'day', 'night.', 'Till', 'bore', 'apple', 'bright.', 'And', 'my', 'foe', 'beheld', 'shine,', 'And', 'he', 'knew', 'that', 'was', 'mine.', 'And', 'into', 'my', 'garden', 'stole,', 'When', 'night', 'had', 'veild', 'pole;', 'In', 'morning', 'glad', 'I', 'see;', 'My', 'foe', 'outstretched', 'beneath', 'tree.']\n" + ] + } + ], + "source": [ + "poem_lst = poem.split()\n", + "\n", + "poem_noblacklist=[]\n", + "\n", + "for i in poem_lst:\n", + " if i in blacklist:\n", + " pass\n", + " else:\n", + " poem_noblacklist.append(i)\n", + "\n", + "print (poem_noblacklist)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "IwasangrywithmyfriendItoldmywrathmywrathdidendIwasangrywithmyfoeItolditnotmywrathdidgrowAndIwaterditinfearsNightmorningwithmytearsAndIsunneditwithsmilesAndwithsoftdeceitfulwilesAnditgrewbothdayandnightTillitboreanapplebrightAndmyfoebehelditshineAndheknewthatitwasmineAndintomygardenstoleWhenthenighthadveildthepoleInthemorninggladIseeMyfoeoutstretchedbeneaththetree\n", + "iwasangrywithmyfrienditoldmywrathmywrathdidendiwasangrywithmyfoeitolditnotmywrathdidgrowandiwaterditinfearsnightmorningwithmytearsandisunneditwithsmilesandwithsoftdeceitfulwilesanditgrewbothdayandnighttillitboreanapplebrightandmyfoebehelditshineandheknewthatitwasmineandintomygardenstolewhenthenighthadveildthepoleinthemorninggladiseemyfoeoutstretchedbeneaththetree\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "no_alphanum = re.findall(r\"\\W\", poem)\n", + "\n", + "#print (no_alphanum)\n", + "\n", + "poem_no_alphanum = \"\"\n", + "\n", + "for i in poem:\n", + " if i in no_alphanum:\n", + " pass\n", + " else:\n", + " poem_no_alphanum = poem_no_alphanum + i\n", + "\n", + "print (poem_no_alphanum)\n", + "print (poem_no_alphanum.lower())" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i was angry with my friend; \n", + "i told my wrath, my wrath did end.\n", + "i was angry with my foe: \n", + "i told it not, my wrath did grow. \n", + "\n", + "and i waterd it in fears,\n", + "night & morning with my tears: \n", + "and i sunned it with smiles,\n", + "and with soft deceitful wiles. \n", + "\n", + "and it grew both day and night. \n", + "till it bore an apple bright. \n", + "and my foe beheld it shine,\n", + "and he knew that it was mine. \n", + "\n", + "and into my garden stole, \n", + "when the night had veild the pole; \n", + "in the morning glad i see; \n", + "my foe outstretched beneath the tree.\n" + ] + } + ], + "source": [ + "print (poem.lower())" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -172,14 +427,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['T', 'P']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "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", + "import re\n", + "\n", + "re.findall(r'[A-Z]', poem)" ] }, { @@ -191,13 +460,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "text/plain": [ + "['1', '2', '3', '1', '2', '3', '1', '4']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "data2=\" \".join(data)\n", "\n", - "# Your code here:\n" + "print (type(data))\n", + "# Your code here:\n", + "re.findall(r'[0-9]', data2)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "data": { + "text/plain": [ + "['1', '2', '3', '1', '2', '3', '1', '4']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "data = str(data)\n", + "print (type(data))\n", + "# Your code here:\n", + "re.findall(r'[0-9]', data)" ] }, { @@ -213,18 +535,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "data = str(data)\n", + "match = re.search(r'', data)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -238,7 +573,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.2" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index d76069e..0a53500 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,98 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], + "source": [ + "import os\n", + "import pandas as pd\n", + "import numpy as np\n", + "import math\n", + "import re\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ironhack is cool.\n" + ] + } + ], + "source": [ + "with open('../your-code/doc1.txt', 'r') as fr:\n", + " texto = fr.readline()\n", + "print(texto)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "docts = [f for f in os.listdir('../your-code') if f.endswith('.txt')]\n", + "path = '../your-code/'\n", + "dataset = []\n", + "x = 0\n", + "for i in docts:\n", + " filename = path + docts[x]\n", + " x += 1\n", + " with open(filename,'r')as fr:\n", + " texto = fr.readline()\n", + " dataset.append(texto) \n", + "dataset" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "# Write your code here\n", + "docts = [f for f in os.listdir('../your-code') if f.endswith('.txt')]\n", + "path = '../your-code/'\n", + "\n", + "x = 0\n", + "for i in docts:\n", + " filename = path + docts[x]\n", + " x += 1\n", + " with open(filename,'r')as fr:\n", + " texto = fr.readline()\n", + " corpus.append(texto) \n", + "\n", + "#corpus = [pd.concat(dataset, axis=1)]\n", + "#print (doctos)\n", + "#print (dataset)\n", + "print (corpus)" ] }, { @@ -106,9 +191,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +229,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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 I love Ironhack I am a student at Ironhack\n", + "ironhack is cool i love ironhack i am a student at ironhack\n" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "corpus2 = str(corpus)\n", + "corpus3 = corpus2.replace(\".\",\"\").replace(\",\",\"\").replace(\"'\",\"\").replace(\"[\",\"\").replace(\"]\",\"\")\n", + "corpus4 = corpus3.lower()\n", + "\n", + "print(corpus2)\n", + "print(corpus3)\n", + "print(corpus4)" ] }, { @@ -152,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +282,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack',\n", + " 'is',\n", + " 'cool',\n", + " 'i',\n", + " 'love',\n", + " 'ironhack',\n", + " 'i',\n", + " 'am',\n", + " 'a',\n", + " 'student',\n", + " 'at',\n", + " 'ironhack']" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "corpus5 = corpus4.split()\n", + "corpus5\n", + "#corpus7 = corpus5.replace(\"[\",\"\").replace(\"]\",\"\").replace('\"',\"\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], + "source": [ + "for i in corpus5:\n", + " if i in bag_of_words:\n", + " pass\n", + " else:\n", + " bag_of_words.append(i)\n", + "\n", + "print (bag_of_words)" ] }, { @@ -192,9 +350,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,12 +374,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ironhack is cool.\n", + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + }, + { + "data": { + "text/plain": [ + "[0, 1, 1, 1, 0, 0, 1, 0, 0]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "print(corpus[0])\n", + "print(bag_of_words)\n", "term_freq = []\n", "\n", + "for i in bag_of_words:\n", + " countword = corpus[0].count(i)\n", + " term_freq.append(countword)\n", + "term_freq\n", + "#for i in corpus:\n", + "# for word in bag_of_words:\n", + " \n", + "\n", "# Write your code here" ] }, @@ -228,9 +423,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 1, 1, 0, 0, 1, 0, 0]\n" + ] + } + ], "source": [ "print(term_freq)" ] @@ -268,7 +471,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -309,7 +512,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +526,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.2" } }, "nbformat": 4, From 007b8bdb59f85b8dd09dd4941dd38dcd8ea19a15 Mon Sep 17 00:00:00 2001 From: JoaquinGonzalezSimon Date: Fri, 25 Mar 2022 20:44:18 -0600 Subject: [PATCH 2/2] Lab complete with challenge 2 --- .../challenge-2-checkpoint.ipynb | 105 ++++++++++++------ your-code/challenge-2.ipynb | 105 ++++++++++++------ 2 files changed, 144 insertions(+), 66 deletions(-) diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index 0a53500..c335fb2 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -134,6 +134,33 @@ "output_type": "execute_result" } ], + "source": [ + "corpusb = []\n", + "\n", + "for i in range(1,4):\n", + " with open(f'doc{i}.txt','r') as fr:\n", + " doc = fr.readline()\n", + " corpusb.append(doc)\n", + "\n", + "corpusb" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "docts = [f for f in os.listdir('../your-code') if f.endswith('.txt')]\n", "path = '../your-code/'\n", @@ -150,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -191,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -229,7 +256,28 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "corpusb = [doc.lower().replace('.','') for doc in corpusb]\n", + "corpusb" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -262,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -282,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -302,7 +350,7 @@ " 'ironhack']" ] }, - "execution_count": 9, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -316,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -350,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -374,42 +422,33 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Ironhack is cool.\n", - "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\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" ] - }, - { - "data": { - "text/plain": [ - "[0, 1, 1, 1, 0, 0, 1, 0, 0]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "print(corpus[0])\n", - "print(bag_of_words)\n", "term_freq = []\n", "\n", - "for i in bag_of_words:\n", - " countword = corpus[0].count(i)\n", - " term_freq.append(countword)\n", - "term_freq\n", - "#for i in corpus:\n", - "# for word in bag_of_words:\n", - " \n", + "for i in range(len(corpusb)):\n", + " doc_list = corpusb[i].split()\n", + " print(doc_list)\n", + " temp = []\n", + " for word in bag_of_words:\n", + " conteo = doc_list.count(word)\n", + " temp.append(conteo)\n", + " term_freq.append(temp)\n", "\n", - "# Write your code here" + "print(term_freq)" ] }, { @@ -423,14 +462,14 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[0, 1, 1, 1, 0, 0, 1, 0, 0]\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" ] } ], diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 0a53500..c335fb2 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -134,6 +134,33 @@ "output_type": "execute_result" } ], + "source": [ + "corpusb = []\n", + "\n", + "for i in range(1,4):\n", + " with open(f'doc{i}.txt','r') as fr:\n", + " doc = fr.readline()\n", + " corpusb.append(doc)\n", + "\n", + "corpusb" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "docts = [f for f in os.listdir('../your-code') if f.endswith('.txt')]\n", "path = '../your-code/'\n", @@ -150,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -191,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -229,7 +256,28 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "corpusb = [doc.lower().replace('.','') for doc in corpusb]\n", + "corpusb" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -262,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -282,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -302,7 +350,7 @@ " 'ironhack']" ] }, - "execution_count": 9, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -316,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -350,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -374,42 +422,33 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Ironhack is cool.\n", - "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\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" ] - }, - { - "data": { - "text/plain": [ - "[0, 1, 1, 1, 0, 0, 1, 0, 0]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "print(corpus[0])\n", - "print(bag_of_words)\n", "term_freq = []\n", "\n", - "for i in bag_of_words:\n", - " countword = corpus[0].count(i)\n", - " term_freq.append(countword)\n", - "term_freq\n", - "#for i in corpus:\n", - "# for word in bag_of_words:\n", - " \n", + "for i in range(len(corpusb)):\n", + " doc_list = corpusb[i].split()\n", + " print(doc_list)\n", + " temp = []\n", + " for word in bag_of_words:\n", + " conteo = doc_list.count(word)\n", + " temp.append(conteo)\n", + " term_freq.append(temp)\n", "\n", - "# Write your code here" + "print(term_freq)" ] }, { @@ -423,14 +462,14 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[0, 1, 1, 1, 0, 0, 1, 0, 0]\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" ] } ],