Skip to content

Commit 57ff1e4

Browse files
edit any and list comprehension
1 parent 46cba8c commit 57ff1e4

File tree

10 files changed

+26214
-204
lines changed

10 files changed

+26214
-204
lines changed

Chapter1/list/apply_functions_to_elements.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
"id": "76e8e609",
274274
"metadata": {},
275275
"source": [
276-
"### Use any and List Comprehension for List Condition Checking"
276+
"### Simplify List Condition Evaluation with any and List Comprehensions"
277277
]
278278
},
279279
{
@@ -282,7 +282,7 @@
282282
"id": "2376b557",
283283
"metadata": {},
284284
"source": [
285-
"If you want to check whether a statement is `True` for one or more items in a list, using `any` and list comprehension is simpler than using a for-loop and an if-else statement. "
285+
"When checking if a condition is true for any list element in Python, use any with a list comprehension instead of a for loop and if-else statements for more readable code."
286286
]
287287
},
288288
{

Chapter5/natural_language_processing.ipynb

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23880,78 +23880,6 @@
2388023880
"[Link to inflect](https://github.com/jaraco/inflect)."
2388123881
]
2388223882
},
23883-
{
23884-
"attachments": {},
23885-
"cell_type": "markdown",
23886-
"id": "ccc78b1b",
23887-
"metadata": {},
23888-
"source": [
23889-
"### FlashText: Extract and Replace Keywords in Sentences"
23890-
]
23891-
},
23892-
{
23893-
"cell_type": "code",
23894-
"execution_count": null,
23895-
"id": "d163b267",
23896-
"metadata": {},
23897-
"outputs": [],
23898-
"source": [
23899-
"!pip install flashtext"
23900-
]
23901-
},
23902-
{
23903-
"attachments": {},
23904-
"cell_type": "markdown",
23905-
"id": "d58089ec",
23906-
"metadata": {},
23907-
"source": [
23908-
"Have you ever wanted to extract similar keywords and turn them into one standard keyword? If so, try FlashText. \n",
23909-
"\n",
23910-
"FlashText allows you to extract or replace keywords in sentences. \n",
23911-
"\n",
23912-
"In the code below, we use FlashText to extract the keywords `CEO` and `Python programming language` from a sentence that contains only the keywords `ceo` and `Python`."
23913-
]
23914-
},
23915-
{
23916-
"cell_type": "code",
23917-
"execution_count": 10,
23918-
"id": "87541cd0",
23919-
"metadata": {},
23920-
"outputs": [
23921-
{
23922-
"data": {
23923-
"text/plain": [
23924-
"['CEO', 'Python programming language']"
23925-
]
23926-
},
23927-
"execution_count": 10,
23928-
"metadata": {},
23929-
"output_type": "execute_result"
23930-
}
23931-
],
23932-
"source": [
23933-
"from flashtext import KeywordProcessor\n",
23934-
"\n",
23935-
"kw_processor = KeywordProcessor()\n",
23936-
"kw_dict = {\n",
23937-
" \"CEO\": [\"Chief Executive Officer\", \"ceo\"], # Similar keywords to CEO\n",
23938-
" \"Python programming language\": [\"Python\", \"Python language\"]\n",
23939-
"}\n",
23940-
"kw_processor.add_keywords_from_dict(keyword_dict=kw_dict)\n",
23941-
"\n",
23942-
"sent = \"The ceo of this company is fluent in Python.\"\n",
23943-
"kw_processor.extract_keywords(sent)"
23944-
]
23945-
},
23946-
{
23947-
"attachments": {},
23948-
"cell_type": "markdown",
23949-
"id": "5330a348",
23950-
"metadata": {},
23951-
"source": [
23952-
"[Link to FlashText](https://github.com/vi3k6i5/flashtext)."
23953-
]
23954-
},
2395523883
{
2395623884
"attachments": {},
2395723885
"cell_type": "markdown",

Chapter5/spark.ipynb

Lines changed: 8732 additions & 0 deletions
Large diffs are not rendered by default.

docs/Chapter1/list/apply_functions_to_elements.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ <h2> Contents </h2>
518518
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#filter-get-the-elements-of-an-iterable-that-a-function-evaluates-true">2.3.5.3. filter: Get the Elements of an Iterable that a Function Evaluates True</a></li>
519519
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#map-method-apply-a-function-to-each-item-of-an-iterable">2.3.5.4. map method: Apply a Function to Each Item of an Iterable</a></li>
520520
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#sort-sort-a-list-of-tuples-by-the-first-or-second-item">2.3.5.5. sort: Sort a List of Tuples by the First or Second Item</a></li>
521-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#use-any-and-list-comprehension-for-list-condition-checking">2.3.5.6. Use any and List Comprehension for List Condition Checking</a></li>
521+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simplify-list-condition-evaluation-with-any-and-list-comprehensions">2.3.5.6. Simplify List Condition Evaluation with any and List Comprehensions</a></li>
522522
</ul>
523523
</nav>
524524
</div>
@@ -641,9 +641,9 @@ <h2><span class="section-number">2.3.5.5. </span>sort: Sort a List of Tuples by
641641
</div>
642642
</div>
643643
</section>
644-
<section id="use-any-and-list-comprehension-for-list-condition-checking">
645-
<h2><span class="section-number">2.3.5.6. </span>Use any and List Comprehension for List Condition Checking<a class="headerlink" href="#use-any-and-list-comprehension-for-list-condition-checking" title="Permalink to this heading">#</a></h2>
646-
<p>If you want to check whether a statement is <code class="docutils literal notranslate"><span class="pre">True</span></code> for one or more items in a list, using <code class="docutils literal notranslate"><span class="pre">any</span></code> and list comprehension is simpler than using a for-loop and an if-else statement.</p>
644+
<section id="simplify-list-condition-evaluation-with-any-and-list-comprehensions">
645+
<h2><span class="section-number">2.3.5.6. </span>Simplify List Condition Evaluation with any and List Comprehensions<a class="headerlink" href="#simplify-list-condition-evaluation-with-any-and-list-comprehensions" title="Permalink to this heading">#</a></h2>
646+
<p>When checking if a condition is true for any list element in Python, use any with a list comprehension instead of a for loop and if-else statements for more readable code.</p>
647647
<div class="cell docutils container">
648648
<div class="cell_input docutils container">
649649
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">FRUITS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;grape&#39;</span><span class="p">]</span>
@@ -762,7 +762,7 @@ <h2><span class="section-number">2.3.5.6. </span>Use any and List Comprehension
762762
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#filter-get-the-elements-of-an-iterable-that-a-function-evaluates-true">2.3.5.3. filter: Get the Elements of an Iterable that a Function Evaluates True</a></li>
763763
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#map-method-apply-a-function-to-each-item-of-an-iterable">2.3.5.4. map method: Apply a Function to Each Item of an Iterable</a></li>
764764
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#sort-sort-a-list-of-tuples-by-the-first-or-second-item">2.3.5.5. sort: Sort a List of Tuples by the First or Second Item</a></li>
765-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#use-any-and-list-comprehension-for-list-condition-checking">2.3.5.6. Use any and List Comprehension for List Condition Checking</a></li>
765+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simplify-list-condition-evaluation-with-any-and-list-comprehensions">2.3.5.6. Simplify List Condition Evaluation with any and List Comprehensions</a></li>
766766
</ul>
767767
</nav></div>
768768

docs/Chapter5/natural_language_processing.html

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,10 @@ <h2> Contents </h2>
531531
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#top2vec-quick-topic-modeling-in-python">6.6.16. Top2Vec: Quick Topic Modeling in Python</a></li>
532532
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#expanding-english-contractions-in-text">6.6.17. Expanding English Contractions in Text</a></li>
533533
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#inflect-generate-plurals-singulars-and-indefinite-articles">6.6.18. inflect: Generate Plurals, Singulars, and Indefinite Articles</a></li>
534-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#flashtext-extract-and-replace-keywords-in-sentences">6.6.19. FlashText: Extract and Replace Keywords in Sentences</a></li>
535-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#ekphrasis-text-processing-tool-for-social-media-text">6.6.20. ekphrasis: Text Processing Tool For Social Media Text</a></li>
536-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chroma-the-lightning-fast-solution-to-text-embeddings-and-querying">6.6.21. Chroma: The Lightning-Fast Solution to Text Embeddings and Querying</a></li>
537-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#galatic-clean-and-analyze-massive-text-datasets">6.6.22. Galatic: Clean and Analyze Massive Text Datasets</a></li>
538-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#efficient-keyword-extraction-and-replacement-with-flashtext">6.6.23. Efficient Keyword Extraction and Replacement with FlashText</a></li>
534+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#ekphrasis-text-processing-tool-for-social-media-text">6.6.19. ekphrasis: Text Processing Tool For Social Media Text</a></li>
535+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chroma-the-lightning-fast-solution-to-text-embeddings-and-querying">6.6.20. Chroma: The Lightning-Fast Solution to Text Embeddings and Querying</a></li>
536+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#galatic-clean-and-analyze-massive-text-datasets">6.6.21. Galatic: Clean and Analyze Massive Text Datasets</a></li>
537+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#efficient-keyword-extraction-and-replacement-with-flashtext">6.6.22. Efficient Keyword Extraction and Replacement with FlashText</a></li>
539538
</ul>
540539
</nav>
541540
</div>
@@ -2084,44 +2083,8 @@ <h2><span class="section-number">6.6.18. </span>inflect: Generate Plurals, Singu
20842083
</div>
20852084
<p><a class="reference external" href="https://github.com/jaraco/inflect">Link to inflect</a>.</p>
20862085
</section>
2087-
<section id="flashtext-extract-and-replace-keywords-in-sentences">
2088-
<h2><span class="section-number">6.6.19. </span>FlashText: Extract and Replace Keywords in Sentences<a class="headerlink" href="#flashtext-extract-and-replace-keywords-in-sentences" title="Permalink to this heading">#</a></h2>
2089-
<div class="cell docutils container">
2090-
<div class="cell_input docutils container">
2091-
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>pip<span class="w"> </span>install<span class="w"> </span>flashtext
2092-
</pre></div>
2093-
</div>
2094-
</div>
2095-
</div>
2096-
<p>Have you ever wanted to extract similar keywords and turn them into one standard keyword? If so, try FlashText.</p>
2097-
<p>FlashText allows you to extract or replace keywords in sentences.</p>
2098-
<p>In the code below, we use FlashText to extract the keywords <code class="docutils literal notranslate"><span class="pre">CEO</span></code> and <code class="docutils literal notranslate"><span class="pre">Python</span> <span class="pre">programming</span> <span class="pre">language</span></code> from a sentence that contains only the keywords <code class="docutils literal notranslate"><span class="pre">ceo</span></code> and <code class="docutils literal notranslate"><span class="pre">Python</span></code>.</p>
2099-
<div class="cell docutils container">
2100-
<div class="cell_input docutils container">
2101-
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flashtext</span> <span class="kn">import</span> <span class="n">KeywordProcessor</span>
2102-
2103-
<span class="n">kw_processor</span> <span class="o">=</span> <span class="n">KeywordProcessor</span><span class="p">()</span>
2104-
<span class="n">kw_dict</span> <span class="o">=</span> <span class="p">{</span>
2105-
<span class="s2">&quot;CEO&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;Chief Executive Officer&quot;</span><span class="p">,</span> <span class="s2">&quot;ceo&quot;</span><span class="p">],</span> <span class="c1"># Similar keywords to CEO</span>
2106-
<span class="s2">&quot;Python programming language&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;Python&quot;</span><span class="p">,</span> <span class="s2">&quot;Python language&quot;</span><span class="p">]</span>
2107-
<span class="p">}</span>
2108-
<span class="n">kw_processor</span><span class="o">.</span><span class="n">add_keywords_from_dict</span><span class="p">(</span><span class="n">keyword_dict</span><span class="o">=</span><span class="n">kw_dict</span><span class="p">)</span>
2109-
2110-
<span class="n">sent</span> <span class="o">=</span> <span class="s2">&quot;The ceo of this company is fluent in Python.&quot;</span>
2111-
<span class="n">kw_processor</span><span class="o">.</span><span class="n">extract_keywords</span><span class="p">(</span><span class="n">sent</span><span class="p">)</span>
2112-
</pre></div>
2113-
</div>
2114-
</div>
2115-
<div class="cell_output docutils container">
2116-
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&#39;CEO&#39;, &#39;Python programming language&#39;]
2117-
</pre></div>
2118-
</div>
2119-
</div>
2120-
</div>
2121-
<p><a class="reference external" href="https://github.com/vi3k6i5/flashtext">Link to FlashText</a>.</p>
2122-
</section>
21232086
<section id="ekphrasis-text-processing-tool-for-social-media-text">
2124-
<h2><span class="section-number">6.6.20. </span>ekphrasis: Text Processing Tool For Social Media Text<a class="headerlink" href="#ekphrasis-text-processing-tool-for-social-media-text" title="Permalink to this heading">#</a></h2>
2087+
<h2><span class="section-number">6.6.19. </span>ekphrasis: Text Processing Tool For Social Media Text<a class="headerlink" href="#ekphrasis-text-processing-tool-for-social-media-text" title="Permalink to this heading">#</a></h2>
21252088
<div class="cell tag_hide-cell docutils container">
21262089
<details class="hide above-input">
21272090
<summary aria-label="Toggle hidden content">
@@ -2180,7 +2143,7 @@ <h2><span class="section-number">6.6.20. </span>ekphrasis: Text Processing Tool
21802143
<p><a class="reference external" href="https://github.com/cbaziotis/ekphrasis">Link to ekphrasis</a>.</p>
21812144
</section>
21822145
<section id="chroma-the-lightning-fast-solution-to-text-embeddings-and-querying">
2183-
<h2><span class="section-number">6.6.21. </span>Chroma: The Lightning-Fast Solution to Text Embeddings and Querying<a class="headerlink" href="#chroma-the-lightning-fast-solution-to-text-embeddings-and-querying" title="Permalink to this heading">#</a></h2>
2146+
<h2><span class="section-number">6.6.20. </span>Chroma: The Lightning-Fast Solution to Text Embeddings and Querying<a class="headerlink" href="#chroma-the-lightning-fast-solution-to-text-embeddings-and-querying" title="Permalink to this heading">#</a></h2>
21842147
<div class="cell tag_hide-cell docutils container">
21852148
<details class="hide above-input">
21862149
<summary aria-label="Toggle hidden content">
@@ -2233,7 +2196,7 @@ <h2><span class="section-number">6.6.21. </span>Chroma: The Lightning-Fast Solut
22332196
<p><a class="reference external" href="https://github.com/chroma-core/chroma">Link to Chroma</a>.</p>
22342197
</section>
22352198
<section id="galatic-clean-and-analyze-massive-text-datasets">
2236-
<h2><span class="section-number">6.6.22. </span>Galatic: Clean and Analyze Massive Text Datasets<a class="headerlink" href="#galatic-clean-and-analyze-massive-text-datasets" title="Permalink to this heading">#</a></h2>
2199+
<h2><span class="section-number">6.6.21. </span>Galatic: Clean and Analyze Massive Text Datasets<a class="headerlink" href="#galatic-clean-and-analyze-massive-text-datasets" title="Permalink to this heading">#</a></h2>
22372200
<div class="cell tag_hide-cell docutils container">
22382201
<details class="hide above-input">
22392202
<summary aria-label="Toggle hidden content">
@@ -2370,7 +2333,7 @@ <h2><span class="section-number">6.6.22. </span>Galatic: Clean and Analyze Massi
23702333
<p><a class="reference external" href="https://github.com/taylorai/galactic">Link to Galatic</a>.</p>
23712334
</section>
23722335
<section id="efficient-keyword-extraction-and-replacement-with-flashtext">
2373-
<h2><span class="section-number">6.6.23. </span>Efficient Keyword Extraction and Replacement with FlashText<a class="headerlink" href="#efficient-keyword-extraction-and-replacement-with-flashtext" title="Permalink to this heading">#</a></h2>
2336+
<h2><span class="section-number">6.6.22. </span>Efficient Keyword Extraction and Replacement with FlashText<a class="headerlink" href="#efficient-keyword-extraction-and-replacement-with-flashtext" title="Permalink to this heading">#</a></h2>
23742337
<div class="cell tag_hide-cell docutils container">
23752338
<details class="hide above-input">
23762339
<summary aria-label="Toggle hidden content">
@@ -2492,11 +2455,10 @@ <h2><span class="section-number">6.6.23. </span>Efficient Keyword Extraction and
24922455
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#top2vec-quick-topic-modeling-in-python">6.6.16. Top2Vec: Quick Topic Modeling in Python</a></li>
24932456
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#expanding-english-contractions-in-text">6.6.17. Expanding English Contractions in Text</a></li>
24942457
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#inflect-generate-plurals-singulars-and-indefinite-articles">6.6.18. inflect: Generate Plurals, Singulars, and Indefinite Articles</a></li>
2495-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#flashtext-extract-and-replace-keywords-in-sentences">6.6.19. FlashText: Extract and Replace Keywords in Sentences</a></li>
2496-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#ekphrasis-text-processing-tool-for-social-media-text">6.6.20. ekphrasis: Text Processing Tool For Social Media Text</a></li>
2497-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chroma-the-lightning-fast-solution-to-text-embeddings-and-querying">6.6.21. Chroma: The Lightning-Fast Solution to Text Embeddings and Querying</a></li>
2498-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#galatic-clean-and-analyze-massive-text-datasets">6.6.22. Galatic: Clean and Analyze Massive Text Datasets</a></li>
2499-
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#efficient-keyword-extraction-and-replacement-with-flashtext">6.6.23. Efficient Keyword Extraction and Replacement with FlashText</a></li>
2458+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#ekphrasis-text-processing-tool-for-social-media-text">6.6.19. ekphrasis: Text Processing Tool For Social Media Text</a></li>
2459+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#chroma-the-lightning-fast-solution-to-text-embeddings-and-querying">6.6.20. Chroma: The Lightning-Fast Solution to Text Embeddings and Querying</a></li>
2460+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#galatic-clean-and-analyze-massive-text-datasets">6.6.21. Galatic: Clean and Analyze Massive Text Datasets</a></li>
2461+
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#efficient-keyword-extraction-and-replacement-with-flashtext">6.6.22. Efficient Keyword Extraction and Replacement with FlashText</a></li>
25002462
</ul>
25012463
</nav></div>
25022464

0 commit comments

Comments
 (0)