From 526bf4dee5715f6cfcc6088e0888c866da7b2a50 Mon Sep 17 00:00:00 2001 From: JoshuaMiranda94 Date: Wed, 18 Dec 2024 19:10:04 -0400 Subject: [PATCH] Lab Map-Filter --- your-code/main.ipynb | 91 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 9f0e67b..d79e781 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -70,7 +70,13 @@ "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "# your code here\n", + "# Remove the first 568 words\n", + "prophet = prophet[568:]\n", + "\n", + "# Verify the result\n", + "print(\"First 10 words after removing the initial 568:\", prophet[:10])\n" ] }, { @@ -86,7 +92,9 @@ "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "# your code here\n", + "print(\"First 10 words in the `prophet` list:\", prophet[:10])\n" ] }, { @@ -104,17 +112,17 @@ "metadata": {}, "outputs": [], "source": [ - "def reference(x):\n", - " '''\n", - " Input: A string\n", - " Output: The string with references removed\n", - " \n", - " Example:\n", - " Input: 'the{7}'\n", - " Output: 'the'\n", - " '''\n", - " \n", - " # your code here" + "\n", + " # your code here\n", + "def remove_references(word):\n", + "\n", + " return word.split('{')[0]\n", + "prophet = [remove_references(word) for word in prophet]\n", + "# Example usage\n", + "example_words = ['word1{ref1}', 'word2', 'example{ref2}', 'text']\n", + "cleaned_words = [remove_references(word) for word in example_words]\n", + "\n", + "print(\"Cleaned words:\", cleaned_words)\n" ] }, { @@ -139,7 +147,11 @@ "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "# your code here\n", + "prophet_reference = list(map(remove_references, prophet))\n", + "\n", + "print(\"First 10 words in `prophet_reference`:\", prophet_reference[:10])\n" ] }, { @@ -165,7 +177,8 @@ " Output: ['the', 'beloved']\n", " '''\n", " \n", - " # your code here" + " # your code here\n", + " return word.split('\\n')" ] }, { @@ -183,7 +196,11 @@ }, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "prophet_line = [split_line_break(word) for word in prophet_reference]\n", + "\n", + "\n", + "print(\"First 10 elements in `prophet_line`:\", prophet_line[:10])\n" ] }, { @@ -262,7 +279,20 @@ }, "outputs": [], "source": [ - "# your code here" + "\n", + "# your code here\n", + "# List of words to exclude\n", + "exclude_words = [\"the\", \"a\", \"an\", \"in\", \"on\", \"at\", \"and\", \"or\", \"but\", \"if\", \"is\", \"it\"]\n", + "\n", + "# Define the filter function\n", + "def filter_words(word):\n", + "\n", + " return word.lower() not in exclude_words\n", + "\n", + "# Example usage\n", + "example_words = [\"the\", \"sun\", \"is\", \"shining\", \"on\", \"a\", \"beautiful\", \"day\"]\n", + "filtered_words = [word for word in example_words if filter_words(word)]\n", + "print(\"Filtered words:\", filtered_words)\n" ] }, { @@ -325,7 +355,15 @@ }, "outputs": [], "source": [ - "# your code here" + "\n", + "# your code here\n", + "# Define the concatenation function\n", + "def concatenate_with_space(str1, str2):\n", + " return f\"{str1} {str2}\"\n", + "\n", + "# Example usage\n", + "result = concatenate_with_space(\"Hello\", \"World\")\n", + "print(\"Concatenated string:\", result)\n" ] }, { @@ -341,7 +379,22 @@ "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "from functools import reduce\n", + "\n", + "# Define the concatenation function\n", + "def concatenate_with_space(str1, str2):\n", + "\n", + " return f\"{str1} {str2}\"\n", + "\n", + "# Example list `prophet_filter`\n", + "prophet_filter = [\"This\", \"is\", \"a\", \"cleaned\", \"version\", \"of\", \"the\", \"text\", \"corpus\"]\n", + "\n", + "# Use reduce to combine the list into a single string\n", + "prophet_string = reduce(concatenate_with_space, prophet_filter)\n", + "\n", + "# Display the result\n", + "print(\"Prophet string:\", prophet_string)\n" ] } ],