diff --git a/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb new file mode 100644 index 0000000..fc03d17 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q1-checkpoint.ipynb @@ -0,0 +1,253 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, create a Python function that wraps your previous solution for the Bag of Words lab.\n", + "\n", + "Requirements:\n", + "\n", + "1. Your function should accept the following parameters:\n", + " * `docs` [REQUIRED] - array of document paths.\n", + " * `stop_words` [OPTIONAL] - array of stop words. The default value is an empty array.\n", + "\n", + "1. Your function should return a Python object that contains the following:\n", + " * `bag_of_words` - array of strings of normalized unique words in the corpus.\n", + " * `term_freq` - array of the term-frequency vectors." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'bag_of_words': ['ironhack',\n", + " 'is',\n", + " 'cooli',\n", + " 'love',\n", + " 'ironhacki',\n", + " 'am',\n", + " 'a',\n", + " 'student',\n", + " 'at'],\n", + " 'term_freq': [[1, 1, 0, 0, 0, 0, 0, 0, 0],\n", + " [1, 0, 0, 1, 0, 0, 0, 0, 0],\n", + " [1, 0, 0, 0, 0, 1, 1, 1, 1]]}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Import required libraries\n", + "import re\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", + " corpus = []\n", + " bag_of_words=[]\n", + " term_freq=[] \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", + " path='C:/Users/User/lab-string-operations/your-code/'\n", + " for archivo in docs:\n", + " with open(path+archivo,\"r\") as f:\n", + " lines=f.readlines()\n", + " for line in lines:\n", + " corpus.append(line)\n", + " corpus2=[]\n", + " for word in corpus:\n", + " w=word.lower()\n", + " w=w.replace('.','')\n", + " corpus2.append(w)\n", + " corpus=corpus2\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", + " if it is not a stop word.\n", + " \"\"\"\n", + " break_array_strings=(''.join(corpus)).split()\n", + " \n", + " for word in break_array_strings:\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)\n", + " \n", + " for i in stop_words:\n", + " if i in bag_of_words:\n", + " bag_of_words.remove(i)\n", + " \n", + " for doc in corpus:\n", + " x=[]\n", + " for w in bag_of_words:\n", + " d=doc.split()\n", + " r=d.count(w)\n", + " x.append(r)\n", + " term_freq.append(x)\n", + "\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", + "docs=['doc1.txt','doc2.txt','doc3.txt']\n", + "get_bow_from_docs(docs)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function without stop words. You should see the output like below:\n", + "\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]]}```" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'is', 'cooli', 'love', 'ironhacki', 'am', 'a', 'student', 'at'], 'term_freq': [[1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 1, 1, 1, 1]]}\n" + ] + } + ], + "source": [ + "# Define doc paths array\n", + "docs =['doc1.txt','doc2.txt','doc3.txt']\n", + "\n", + "# Obtain BoW from your function\n", + "bow = get_bow_from_docs(docs)\n", + "\n", + "# Print BoW\n", + "print(bow)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If your attempt above is successful, nice work done!\n", + "\n", + "Now test your function again with the stop words. In the previous lab we defined the stop words in a large array. In this lab, we'll import the stop words from Scikit-Learn." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "frozenset({'keep', 'down', 'along', 'cant', 'should', 'all', 'both', 'beforehand', 'someone', 'nobody', 'over', 'anyone', 'on', 'between', 'whole', 'is', 'indeed', 'twelve', 'while', 'will', 'no', 'made', 'cannot', 'few', 'becomes', 'i', 'anywhere', 'could', 'again', 'where', 'in', 'hereupon', 'are', 'whom', 'full', 'can', 'often', 'mine', 'after', 'here', 'herein', 'hence', 'ie', 'un', 'noone', 'sixty', 'out', 'be', 'now', 'across', 'still', 'would', 'interest', 'during', 'our', 'seems', 'what', 'even', 'put', 'everything', 'being', 'your', 'afterwards', 'something', 'through', 'was', 'show', 'onto', 'former', 'latterly', 'many', 'either', 'sincere', 'hundred', 'next', 'detail', 'of', 'most', 'besides', 'with', 'were', 'fire', 'top', 'until', 'at', 'himself', 'must', 'who', 'though', 'mill', 'became', 'otherwise', 'also', 'why', 'might', 'upon', 'find', 'see', 'however', 'more', 'always', 'another', 'my', 'fill', 'off', 'wherever', 'describe', 'above', 'us', 'give', 'couldnt', 'not', 'when', 'already', 'none', 'herself', 'ours', 'than', 'once', 'him', 'per', 'into', 'to', 'front', 'inc', 'part', 'thereby', 'below', 'own', 'whereby', 'somehow', 'behind', 'well', 'hers', 'how', 'from', 'thin', 'third', 'whose', 'ltd', 'those', 'side', 'whither', 'wherein', 'about', 'ourselves', 'thick', 'am', 'itself', 'which', 'then', 'for', 'system', 'anyhow', 'eg', 'nowhere', 'against', 'whence', 'same', 'too', 'except', 'serious', 'elsewhere', 'seemed', 'moreover', 'so', 'amount', 'go', 'since', 'everyone', 'yourselves', 'their', 'do', 'call', 'one', 'the', 'almost', 'alone', 'as', 'hereby', 'six', 'myself', 'nine', 'anyway', 'because', 'co', 'up', 'thence', 'sometime', 'name', 'but', 'three', 'formerly', 'me', 'meanwhile', 'fifty', 'hasnt', 'somewhere', 'ever', 'else', 'it', 'some', 'found', 'nor', 'take', 'and', 'whereupon', 'yet', 'without', 'perhaps', 'they', 'last', 'nevertheless', 'therefore', 'this', 'further', 'move', 'mostly', 'an', 'eight', 'each', 'towards', 'has', 'a', 'latter', 'you', 'due', 'around', 'whoever', 'back', 'whereas', 'or', 'everywhere', 'bill', 'beside', 'neither', 'etc', 'first', 'yours', 'anything', 'seeming', 'thereupon', 'two', 'within', 'namely', 'these', 'other', 'every', 'its', 'them', 'under', 'empty', 'themselves', 'toward', 'whether', 'others', 'twenty', 'she', 'yourself', 'thru', 'get', 'together', 'several', 'con', 'becoming', 'hereafter', 'any', 're', 'de', 'therein', 'that', 'seem', 'before', 'among', 'via', 'please', 'he', 'such', 'been', 'beyond', 'become', 'bottom', 'less', 'thus', 'by', 'if', 'much', 'eleven', 'forty', 'thereafter', 'whatever', 'only', 'her', 'amongst', 'have', 'whereafter', 'his', 'there', 'enough', 'we', 'cry', 'five', 'rather', 'fifteen', 'nothing', 'least', 'whenever', 'throughout', 'although', 'sometimes', 'ten', 'very', 'done', 'amoungst', 'had', 'four', 'never', 'may'})\n" + ] + } + ], + "source": [ + "from sklearn.feature_extraction import _stop_words\n", + "print(_stop_words.ENGLISH_STOP_WORDS)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You should have seen a large list of words that looks like:\n", + "\n", + "```frozenset({'across', 'mine', 'cannot', ...})```\n", + "\n", + "`frozenset` is a type of Python object that is immutable. In this lab you can use it just like an array without conversion." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, test your function with supplying `stop_words.ENGLISH_STOP_WORDS` as the second parameter." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'cooli', 'love', 'ironhacki', 'student'], 'term_freq': [[1, 0, 0, 0, 0], [1, 0, 1, 0, 0], [1, 0, 0, 0, 1]]}\n" + ] + } + ], + "source": [ + "bow = get_bow_from_docs(docs, _stop_words.ENGLISH_STOP_WORDS)\n", + "\n", + "print(bow)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You should have seen:\n", + "\n", + "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" + ] + }, + { + "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.9.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb new file mode 100644 index 0000000..963dc4a --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb @@ -0,0 +1,273 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Lambda** is a special Python function type that is **anonymous**. By *anonymous* it means a lambda function does not have name. Lambda functions are embedded inside codes so that you don't call them like calling regular Python functions.\n", + "\n", + "**`Map`** applies a function to all the items in an input list. The function that is applied can be a standard or a lambda function.\n", + "\n", + "For instance, below is an example of multiplying number tuples in a list:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = [(1,2), (3,4), (5,6)]\n", + "\n", + "def multiply(num_tuple):\n", + " return num_tuple[0]*num_tuple[1]\n", + "list(map(multiply, items))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...is the same as:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = [(1,2), (3,4), (5,6)]\n", + "list(map(lambda item: item[0]*item[1], items))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Why do we sometimes use `lambda` and `map`? Because, as you see in the example above, they make your code really concise by combining 3 lines of code to 1 line.\n", + "\n", + "Besides `map`, there is also **`filter`** that selectively returns elements in an array according to whether you return `True`. There is also **`reduce`** that performs computation on a list of items then returns result.\n", + "\n", + "Here is a [good tutorial](http://book.pythontips.com/en/latest/map_filter.html) about `map`, `filter`, and `reduce`. Read it if you are not familiar with how they are used. Then proceed to the next cell." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the next cell, use `filter` and `lambda` to return a list that contains positive numbers only. The output should be:\n", + "\n", + "```[1, 4, 5]```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 5]\n" + ] + } + ], + "source": [ + "numbers = [1, 4, -1, -100, 0, 5, -99]\n", + "posnumbers=list(filter(lambda x: x>0,numbers))\n", + "print(posnumbers)\n", + "# Enter your code below" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, use `reduce` and `lambda` to return a string that only contains English terms. The English terms are separated with a whitespace ` `.\n", + "\n", + "Hints: \n", + "\n", + "* If your Jupyter Notebook cannot import `langdetect`, you need to install it with `pip install langdetect`. If Jupyter Notebook still cannot find the library, try install with `python3 -m pip install langdetect`. This is because you need to install `langdetect` in the same Python run environment where Jupyter Notebook is running.\n", + "\n", + "* If a word is English, `langdetect.detect(word)=='en'` will return `True`.\n", + "\n", + "Your output should read:\n", + "\n", + "```'good morning everyone'```" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 4)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m english_word=reduce((lambda x: x if detect(x)=='en' print(\"hola\"),words )\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "import langdetect\n", + "from functools import reduce\n", + "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", + "english_word=reduce((lambda x: x if detect(x)=='en' print(\"hola\"),words )\n", + "# Enter your code below" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "expected string or bytes-like object", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mwords\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'good morning'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'早上好'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'доброго'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'おはようございます'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'everyone'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'大家'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'каждый'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'みんな'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mdetect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwords\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m\"en\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hola\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\langdetect\\detector_factory.py\u001b[0m in \u001b[0;36mdetect\u001b[1;34m(text)\u001b[0m\n\u001b[0;32m 127\u001b[0m \u001b[0minit_factory\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 128\u001b[0m \u001b[0mdetector\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_factory\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcreate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 129\u001b[1;33m \u001b[0mdetector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 130\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mdetector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdetect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 131\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\langdetect\\detector.py\u001b[0m in \u001b[0;36mappend\u001b[1;34m(self, text)\u001b[0m\n\u001b[0;32m 102\u001b[0m \u001b[0mDetector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_max_text_length\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mrest\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mcut\u001b[0m \u001b[0mdown\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 103\u001b[0m '''\n\u001b[1;32m--> 104\u001b[1;33m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mURL_RE\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 105\u001b[0m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMAIL_RE\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 106\u001b[0m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mNGram\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnormalize_vi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: expected string or bytes-like object" + ] + } + ], + "source": [ + "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", + "if detect(words)==\"en\":\n", + " print(\"hola\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "nl\n" + ] + } + ], + "source": [ + "from langdetect import detect\n", + "\n", + "lang = detect(\"hello, word\")\n", + "\n", + "print(lang)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Question" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, if you still have time, convert your response in Q2 by using `lambda`, `map`, `filter`, or `reduce` where applicable. Enter your function in the cell below.\n", + "\n", + "As you write Python functions, generally you don't want to make your function too long. Long functions are difficult to read and difficult to debug. If a function is getting too long, consider breaking it into sever shorter functions where the main function calls other functions. If anything goes wrong, you can output debug messages in each of the functions to check where exactly the error is." + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [], + "source": [ + "# Enter your code below" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function below with the Bag of Words lab docs (it's easier for you to debug your code). Your output should be:\n", + "\n", + "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction import stop_words\n", + "bow = get_bow_from_docs([\n", + " '../../lab-bag-of-words/your-code/doc1.txt', \n", + " '../../lab-bag-of-words/your-code/doc2.txt', \n", + " '../../lab-bag-of-words/your-code/doc3.txt'],\n", + " stop_words.ENGLISH_STOP_WORDS\n", + ")\n", + "\n", + "print(bow)" + ] + }, + { + "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.9.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 0239d17..a07973d 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -100,7 +100,7 @@ "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } @@ -456,7 +456,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.4" } }, "nbformat": 4, diff --git a/your-code/Q1.ipynb b/your-code/Q1.ipynb index 8b07d3d..fc03d17 100644 --- a/your-code/Q1.ipynb +++ b/your-code/Q1.ipynb @@ -19,24 +19,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'bag_of_words': ['ironhack',\n", + " 'is',\n", + " 'cooli',\n", + " 'love',\n", + " 'ironhacki',\n", + " 'am',\n", + " 'a',\n", + " 'student',\n", + " 'at'],\n", + " 'term_freq': [[1, 1, 0, 0, 0, 0, 0, 0, 0],\n", + " [1, 0, 0, 1, 0, 0, 0, 0, 0],\n", + " [1, 0, 0, 0, 0, 1, 1, 1, 1]]}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Import required libraries\n", - "\n", + "import re\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", - " \n", - " \n", - " \n", + " corpus = []\n", + " bag_of_words=[]\n", + " term_freq=[] \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", + " path='C:/Users/User/lab-string-operations/your-code/'\n", + " for archivo in docs:\n", + " with open(path+archivo,\"r\") as f:\n", + " lines=f.readlines()\n", + " for line in lines:\n", + " corpus.append(line)\n", + " corpus2=[]\n", + " for word in corpus:\n", + " w=word.lower()\n", + " w=w.replace('.','')\n", + " corpus2.append(w)\n", + " corpus=corpus2\n", " \n", " \n", " \"\"\"\n", @@ -45,6 +78,23 @@ " In addition, check if each term is in the `stop_words` array. Only append the term to `bag_of_words`\n", " if it is not a stop word.\n", " \"\"\"\n", + " break_array_strings=(''.join(corpus)).split()\n", + " \n", + " for word in break_array_strings:\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)\n", + " \n", + " for i in stop_words:\n", + " if i in bag_of_words:\n", + " bag_of_words.remove(i)\n", + " \n", + " for doc in corpus:\n", + " x=[]\n", + " for w in bag_of_words:\n", + " d=doc.split()\n", + " r=d.count(w)\n", + " x.append(r)\n", + " term_freq.append(x)\n", "\n", " \n", " \n", @@ -61,7 +111,8 @@ " \"bag_of_words\": bag_of_words,\n", " \"term_freq\": term_freq\n", " }\n", - " " + "docs=['doc1.txt','doc2.txt','doc3.txt']\n", + "get_bow_from_docs(docs)" ] }, { @@ -75,12 +126,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'is', 'cooli', 'love', 'ironhacki', 'am', 'a', 'student', 'at'], 'term_freq': [[1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 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", @@ -100,12 +159,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "frozenset({'keep', 'down', 'along', 'cant', 'should', 'all', 'both', 'beforehand', 'someone', 'nobody', 'over', 'anyone', 'on', 'between', 'whole', 'is', 'indeed', 'twelve', 'while', 'will', 'no', 'made', 'cannot', 'few', 'becomes', 'i', 'anywhere', 'could', 'again', 'where', 'in', 'hereupon', 'are', 'whom', 'full', 'can', 'often', 'mine', 'after', 'here', 'herein', 'hence', 'ie', 'un', 'noone', 'sixty', 'out', 'be', 'now', 'across', 'still', 'would', 'interest', 'during', 'our', 'seems', 'what', 'even', 'put', 'everything', 'being', 'your', 'afterwards', 'something', 'through', 'was', 'show', 'onto', 'former', 'latterly', 'many', 'either', 'sincere', 'hundred', 'next', 'detail', 'of', 'most', 'besides', 'with', 'were', 'fire', 'top', 'until', 'at', 'himself', 'must', 'who', 'though', 'mill', 'became', 'otherwise', 'also', 'why', 'might', 'upon', 'find', 'see', 'however', 'more', 'always', 'another', 'my', 'fill', 'off', 'wherever', 'describe', 'above', 'us', 'give', 'couldnt', 'not', 'when', 'already', 'none', 'herself', 'ours', 'than', 'once', 'him', 'per', 'into', 'to', 'front', 'inc', 'part', 'thereby', 'below', 'own', 'whereby', 'somehow', 'behind', 'well', 'hers', 'how', 'from', 'thin', 'third', 'whose', 'ltd', 'those', 'side', 'whither', 'wherein', 'about', 'ourselves', 'thick', 'am', 'itself', 'which', 'then', 'for', 'system', 'anyhow', 'eg', 'nowhere', 'against', 'whence', 'same', 'too', 'except', 'serious', 'elsewhere', 'seemed', 'moreover', 'so', 'amount', 'go', 'since', 'everyone', 'yourselves', 'their', 'do', 'call', 'one', 'the', 'almost', 'alone', 'as', 'hereby', 'six', 'myself', 'nine', 'anyway', 'because', 'co', 'up', 'thence', 'sometime', 'name', 'but', 'three', 'formerly', 'me', 'meanwhile', 'fifty', 'hasnt', 'somewhere', 'ever', 'else', 'it', 'some', 'found', 'nor', 'take', 'and', 'whereupon', 'yet', 'without', 'perhaps', 'they', 'last', 'nevertheless', 'therefore', 'this', 'further', 'move', 'mostly', 'an', 'eight', 'each', 'towards', 'has', 'a', 'latter', 'you', 'due', 'around', 'whoever', 'back', 'whereas', 'or', 'everywhere', 'bill', 'beside', 'neither', 'etc', 'first', 'yours', 'anything', 'seeming', 'thereupon', 'two', 'within', 'namely', 'these', 'other', 'every', 'its', 'them', 'under', 'empty', 'themselves', 'toward', 'whether', 'others', 'twenty', 'she', 'yourself', 'thru', 'get', 'together', 'several', 'con', 'becoming', 'hereafter', 'any', 're', 'de', 'therein', 'that', 'seem', 'before', 'among', 'via', 'please', 'he', 'such', 'been', 'beyond', 'become', 'bottom', 'less', 'thus', 'by', 'if', 'much', 'eleven', 'forty', 'thereafter', 'whatever', 'only', 'her', 'amongst', 'have', 'whereafter', 'his', 'there', 'enough', 'we', 'cry', 'five', 'rather', 'fifteen', 'nothing', 'least', 'whenever', 'throughout', 'although', 'sometimes', 'ten', 'very', 'done', 'amoungst', 'had', 'four', 'never', 'may'})\n" + ] + } + ], "source": [ - "from sklearn.feature_extraction import stop_words\n", - "print(stop_words.ENGLISH_STOP_WORDS)" + "from sklearn.feature_extraction import _stop_words\n", + "print(_stop_words.ENGLISH_STOP_WORDS)" ] }, { @@ -128,11 +195,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'bag_of_words': ['ironhack', 'cooli', 'love', 'ironhacki', 'student'], 'term_freq': [[1, 0, 0, 0, 0], [1, 0, 1, 0, 0], [1, 0, 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)" ] @@ -170,7 +245,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.4" } }, "nbformat": 4, diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index 75055ac..963dc4a 100644 --- a/your-code/Q3.ipynb +++ b/your-code/Q3.ipynb @@ -85,12 +85,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 5]\n" + ] + } + ], "source": [ "numbers = [1, 4, -1, -100, 0, 5, -99]\n", - "\n", + "posnumbers=list(filter(lambda x: x>0,numbers))\n", + "print(posnumbers)\n", "# Enter your code below" ] }, @@ -113,17 +122,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 4)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m english_word=reduce((lambda x: x if detect(x)=='en' print(\"hola\"),words )\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], "source": [ "import langdetect\n", "from functools import reduce\n", "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", - "\n", + "english_word=reduce((lambda x: x if detect(x)=='en' print(\"hola\"),words )\n", "# Enter your code below" ] }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "expected string or bytes-like object", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mwords\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'good morning'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'早上好'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'доброго'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'おはようございます'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'everyone'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'大家'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'каждый'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'みんな'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mdetect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwords\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m\"en\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hola\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\langdetect\\detector_factory.py\u001b[0m in \u001b[0;36mdetect\u001b[1;34m(text)\u001b[0m\n\u001b[0;32m 127\u001b[0m \u001b[0minit_factory\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 128\u001b[0m \u001b[0mdetector\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_factory\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcreate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 129\u001b[1;33m \u001b[0mdetector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 130\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mdetector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdetect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 131\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\langdetect\\detector.py\u001b[0m in \u001b[0;36mappend\u001b[1;34m(self, text)\u001b[0m\n\u001b[0;32m 102\u001b[0m \u001b[0mDetector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_max_text_length\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mrest\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mcut\u001b[0m \u001b[0mdown\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 103\u001b[0m '''\n\u001b[1;32m--> 104\u001b[1;33m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mURL_RE\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 105\u001b[0m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMAIL_RE\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 106\u001b[0m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mNGram\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnormalize_vi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: expected string or bytes-like object" + ] + } + ], + "source": [ + "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", + "if detect(words)==\"en\":\n", + " print(\"hola\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "nl\n" + ] + } + ], + "source": [ + "from langdetect import detect\n", + "\n", + "lang = detect(\"hello, word\")\n", + "\n", + "print(lang)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -199,7 +265,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.4" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0239d17..ae3f71e 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -100,7 +100,7 @@ "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mStopIteration\u001b[0m: " ] } @@ -173,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -456,7 +456,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.4" } }, "nbformat": 4,