<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Bryan Anthonio</title><description>Bryan Anthonio - Web developer, photographer, and blogger. Welcome to my digital garden!</description><link>https://bryananthonio.com/</link><language>en-us</language><image><url>https://bryananthonio.com/favicon.svg</url><title>Bryan Anthonio</title><link>https://bryananthonio.com</link></image><item><title>A Straight Line Predicts How Fast My Laptop Generates LLM Tokens</title><link>https://bryananthonio.com/blog/straight-line-predicts-how-fast-my-laptop-generates-llm-tokens/</link><guid isPermaLink="true">https://bryananthonio.com/blog/straight-line-predicts-how-fast-my-laptop-generates-llm-tokens/</guid><description>I went down a rabbit hole to see how memory bandwidth limits the rate at which LLMs generate tokens.</description><pubDate>Fri, 13 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://bryananthonio.com/blog/one-reason-input-tokens-are-cheaper-than-output-tokens/&quot;&gt;My previous post&lt;/a&gt; ended on a cliffhanger. For the model Qwen2.5-0.5B, I found that the time per output token (TPOT) was roughly 9 ms for different prompt sizes. But I didn’t have an explanation for why.&lt;/p&gt;
&lt;p&gt;I’ve since learned that in the decode phase for large language model (LLM) inference the primary bottleneck is memory bandwidth, the rate at which the GPU reads LLM model weights from RAM. To explore this, I measured the TPOT of six models of varying sizes on my laptop.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#the-memory-bandwidth-bottleneck&quot;&gt;The Memory Bandwidth Bottleneck&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#tpot-vs-model-size&quot;&gt;TPOT vs Model Size&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#how-i-measured-this&quot;&gt;How I Measured This&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#caveats-of-this-measurement&quot;&gt;Caveats of This Measurement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#what-i-took-away-from-this&quot;&gt;What I Took Away From This&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-memory-bandwidth-bottleneck&quot;&gt;The Memory Bandwidth Bottleneck&lt;a href=&quot;#the-memory-bandwidth-bottleneck&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In LLM inference, the decode phase produces one token at a time. Each time, the weights have to be streamed from RAM to the GPU chip, where the next token is computed.&lt;/p&gt;
&lt;p&gt;In other words, to generate &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;N&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; tokens, you’d have to re-read the model weights &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;N&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; times. This is because the GPU chip has a limited amount of memory, on the order of kilobytes or megabytes depending on the hardware, so it can’t hold all of the weights at once.&lt;/p&gt;
&lt;figure class=&quot;figure figure--bleed-mobile&quot; style=&quot;max-width: 38rem; margin-inline: auto; margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;svg version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 610.984375 720&quot; class=&quot;diagram&quot; role=&quot;img&quot; aria-label=&quot;The path model weights take on a MacBook from the disk, to RAM, and into the GPU chip.&quot;&gt;&lt;!-- svg-source:excalidraw --&gt;&lt;defs&gt;&lt;style class=&quot;style-fonts&quot;&gt;
      @font-face { font-family: Excalifont-135a73x; src: url(data:font/woff2;base64,d09GMgABAAAAAB5UAA4AAAAANUAAAB3+AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhwbjC4cghoGYACBLBEICtB0vAwLZgABNgIkA4FIBCAFgxgHIBt9KSMDpVwWk+yvDsxDc3UeIKUs+EmZdBSpaSAhHcX40h4t59NtrSHDJLEpIw1P2/x3d3BHCBKpoICAYiLqxIZWLEDR3oyczmFvrW5zro1VuWrn9hfl0h+1KBf1FxF/ElVL7Z0/ES4GVAJEPEUnbBoRMGEm3IiGfEa/X/tXrR1qvoIkppMKLRAp0Z8MkXtt2q/fdXaJcT4wVEh2/NtUv7qi17ajdkaz4KwDC+bk7myH6BAk7fC62Sdr3moRrpwAAU4MemKGnEx8Gf+wl7VdE0cqMaD2r9D2RPwl70v/A4QX/L+50mZOAeadMFSSJ3QBNTxfY+bNZjv5+3dZbVKCQCmXspuyO1Al1vcq1BGw27iTdRWmdZWnTF2FrpNnnOk1nBbSwub/x/WoOCNmRJ6mE7sZQABwAQBjKYoAECUCLRUAJEoauMk0KR24bcbXVQO3Q11pFXC7LGyoAW5wAADew3Wxq6sBhEQAHJWC2ysoGEBKg4cXlg7cXRNhRvq6YPgFOLLD9x2t4b+xIlbu1/a3Xut/IwnnsOM83ldUVF4p+t8L/h2GVwLujD8+8Ooc72VVD59hxCONxTUfCzQC49xCtBf53gLl59HM8w/hwcNQuaJhYmHj4OLhcycgJCIhI+fNj78AX1M/IhANAQMHiYVICaHr+iSMeAR8BUgAKJo4XPgtMBXlhFw7CuOChY6CPZOpALUASGr+GNEeXFSEdRh5L5RAiMCyImTFPyKFI7VCCgANZTqAZB9nwG4jyUwVBjijgY6QHfMwryScFcShAXUlY+g3X4XgwIVlW5T5XSYRWipVUdbPAiEDgAcMgcQqoQAxhsTGeBafBEBPSjK6AMeAqIXs0l+cRLt6ojjcUDlh5Jbz3stTeSEQ3dSVcDah+/r1s5A7EHJYLTtMFy2Qi/PKROIrih4oSAG0RYrzIzKjDRmJWTEbJkUqPoSqECPxTByNSRZUze1sEQUPjJjbI2iCTBGqDhFmGQHb5wFAMrZRvqCoHeOSXp3wCJmdLAAk6m+B5lS+gf0hLRpm5hQwQGT80PQP8E+uahoDa96ET8mcFiG45yQArMyplnGWgsQEODF+EYUAKiNEMDBLZpXGoZJTs/F9OBHTh78gc4GaVk5lLy5UpW4jrHf/+QdD/fLZR2+89spLa+ZmpkEAVIj7eyGqAlIixadTinJcLzxDsOQYgpcFCID5EdNcCvsKF80cJYQtvRwYpPCQSbhBWf6CcK6HkuVg6zhMi97DXiJURIjH2tJDWHJuWDRVwUrL0LsJkmRJer6Hr2uYJjfRnJqgcoeeK9aeq/74YrG8nL7Ej1Ofde4nuUK+DRFmFsafv9d1d1Oz4b+w+3oPjdspjKpg9PmCa5qEH31TNlnNKkgwgJI1Ok3FDUMw+xHHmdmN6NBtmgO79I478K7bJeyamB2XDIFEpFvIoyndns2M4GR9mhuQ8BIDeYbeIRr5qC96+zFYxUUcDqtYVIvo/6eCHersuuSp2Rmq2XafQjVP8zoEeMvzmeVpTS3ex7X1sYixttysQwoAR5TuVOSNRxE66E72leTo5KMAJdyFhqxhWi79aAAGgw94N+3+ny7E9H1//FTZJMxUdgnD/AGZJZtzms2Fpp+Pq/FqnwzYOfkItfnZYCW98fIgeEMvEr1668mkZmgGjB/Kk2PHny7dqMgC3Uv2+p5Jecvfo9dHFJqGWZxNPoiUh6EAwSq2CS8fNGyanSTpPHvItvKn7I0V0i0uvjp8WUNj89mhbSCr/Sd/T6d0kAzN0By4Sthx+sEqTmkeo2hepBdo3IDc9dTXZUQ9xVbBnJqf8RpK4ZinfCSfiA7Xk79TNJ/RqFDhViy8UZTXMlsMN2DlGH0sRx/Ayl16vbtC11ESo1Kbv5FAQQyBoAQL9HjNWiN+qERMbj9cxvWZ6sHA8fWPyQE7dJ69HRfeILvFiYYjPK7Tzql5lxkJu25aBcFQ0EB4MmJeAKsdNTWHXVkJtZhmYgFwER8/hvgZAh/tbJ/nKWZLV1YUt4uHal32xCZOW3BtmnJUkv6dA5EGcb+qMeZoxbYg/ibXQjGzIam/VFgqIIUjClFvQA0SnAZ9vK5mQJ75uRTV3CTJXUxeLQJIBjWwnu0dYlaS+E2MIwZrtIIaMjdmeotWtLPwXbi/pFilb5NBKGGMBDSPXJa+T27ktf3aJBm/LsNlSI7uJWMjytgvf1KS/MYSuySTCN3otHjaE+bjRY82B/XcFgy9SJuHmoaEJCuY4w2iCdg7rDXeEWzlcA28lqDXTs5QDbBadBK+ELExNSC1mk1viyGQqJog808zYbrRipC1xqYDr4Yw/9137dz8jDC/GCVPv7UqEvJj0dA0+KKAlYtoODhK66CradP6CruWm8FrPyYL0tbpq6evxkNv3XusfYaaa/GZGduX51aVJI0DR4lUWvN9RIAY4jV0q0cDl+fhRQxwujiLg0RWieziQPCWKeyV9gZn4MOu7B8kbcF0T6AMTzFbypWQKWXwRtKTSEfVlkDMyJW6cgdE+qhKPUQx8ickQ3qBKPodkpp1nq9KGctSnL0VKRa4p0VFB2g5vidk+Ojbq7a8d5IcnAtwGCMufjbEA4gW9hnRJv9Jr9Cv7vG5u0zZqKV4oATPaKlMlkACjBPGausWHO3foZnp+NV+hGjZ4up20jQLHtzLmDqazoWEm85zBnie5CB4g9q6TzkqleRfgPdODpLifQVK4BIuH5blu7VtdXg/d1tzASrU1m1Ok2K0kvIe46m8XssV1HkQkvplJfyU7aNrAxJ9QsCwcwsOee70HIyHFDr9ahJnq9Wq5N7oS6JtQfxdK8V7fDAg2wUq7J6RMRs6loMEW837xWBLeEiuAR6Ej8JPhD99avrxC7ItbvTqSJ4Dz4Mxhbrf8XdjZHgp1aGlm88wbjePahFTXEYI934OES2IuNwqXZ3WmjR+kk2AJLxWfXWAMRLa0Pu7Q9nt8MGh7YKk3D0zIQq6DXM95hNgNU6KynAMp0m67NlXTI6BrJKvw2qCqBG0MapOOtFxBZaDa0GN5Nzqna3zLrdLCn34PP92b3u/cVVWtqsdsi18FeoLkBs6VNQc7rtRkRdmccpf9mXU0Eb4MpcKcBk1hPqEDps+ulaD4eQqtruTevfVsWNWzh70Cr4tehA8A0dYrcLp922iZSP/QhKrbepx4vNpFYoKB4lSyDNnsVR/rv0cz7ldEjI+VOkB8jc1RcWjotP4wF+IkQ9RgNxR7aRfH19V5wmQJDcGgt3XXmenctczdC32rNHTsQAXX/W4NXNudum17FwmOQcfv6wXtawTQLo/NQWyZ0p48VVYj/ILLdVwc49tSWHbFQyyPCJLUb17a3iI10hrhki3kbAt6OVUH6cl8PCcVnTPtWbct0YoyqAACZ4LkHEWnI6RhOb9eVtp0JcjdUJewBe9VyTJVhgJmXK1DGf3krENdmgfyBMCCgGwOpuuz+cRotaQHkiAZN/a8qVSsr1aW6GiRmd52yiDDchNEtkOxp8jDUf7UYAdinedpkm6bNbKG8mWl5RpmroOEmjbQMQbc2dAFg7jTaZdEEMH9zD4iNOOEj7KkqlDXYiOUAv+FxcHmHzovP6wTJJo2zNItCwugnOfqfzIDCdr1vcorKkH79ZJ+3zekNQM9Su9cSvVx65QRCp2MlN5kmbX5bnDX4gS1srZbLeI4xafa1GVZFOi48zyGyy9zjaHQBaEjlrErWpa+Ka9IhHE6lRWDl8IgeyhhrfpvGso72eT7c4wVzbVWeFS1Wu3RSF6OJXv6hizfCBgBGdg5euKbkuDcYAVXygqqIGN1Axse1WHaX0BDj1HRS/3bSFkxJUV7w6QlHxOdDjeXCI8GJDhPvYXdMkULdHm/Ap61evve4tjbdUk+/V9IOMTZloDh97bumSsSznX9T7aMsAYYgR3RLTaHI1Rcge1aa+MlZs0cyqM/7pUfx4wfIQOPTtNjhYF5TR9sXdP7sw1eHt6p+If2C0n964cOj53Y6XPiUpPgAAXp/nXRxDn4apEtmZpg+DMU3kpwrEqoco+Ip0TrTa75NoGjUb+OyBVdnGaYIc04uDo0J+5pT/V1nO3kofX2tTzIFpoNy6YQjSvptV5Y15U9zuK7QvHAhxDGGKq6Lfqkq+2z9ULw3Jfohi9kr0LP/lklup5Sw1KlfF8h9hOvsoAxL4PYZZ1qbEKR9Hn8zhsQBDc/LBWimps+9Q3nuJg1mE88wW1hQeER6CkbVVG3Oo75OtAbs39XNf/jA+w7qN+SsDORLxhJY9VMPDg5Q7bGZyMlIzGOFT5iWTY8O0eIsLPzCVz4JIwfWUrZwl4mcDT7g5EmIAI1uRkFbxY7t4TzM7Ki67DP+tG02z1lnrIcfxnCPBgidTDFXbmnNRt6BcaSyS9NnzF2ngvH2PTJ/3UZDvH68J96b/X2oQvbNZDlj4JZSju1YLmHFO6QKK+iPnVqv8ky+HAXRr+FrumSYbkTPWPN9HiUMMO3YWaZole5v/UkXEd3izDT97dOgzVbA1gwCVbZwmHXmCQcNpaqPag6pqiDb2ocnv/8hKtLX4794TeA1L99SX9dDXNWrvfgFwAtzaojcaVyoiGteqSyW63tu7CQQQXfEB1gotAn0nl+5Galf3l7UlhLRPnin7yJ7u6oSrj8Mbaa/lIf7n0A0zCNBMLnyro/EfPpVE9/VYj9349wB8w/p+ZbnLKoO76o/jFnxykRHp73FF7yhKtTEYf2RaDxLDiMwwNo6oPxRrnEHWUAsPGj3top5L+pC0+O32WSysYO4h9RJkEyDwbkq4efW+Ley+mo6/wzKjNmH3M8APQaXcO8QFaUwmNHQrJFxqqxcX8xcQsYdCOvvRyYPrxSUJAfKL5eZHTkvDBAhyZ6a329KGcqY9hIz4sJ41Zvw7KC6+YrgE0UHmJvnybkBv/nsv6vAe7X7B2XdHp7HtlLd2cAu/OXw0gYFP/cV7+jpm4NSO7YDcDmHs8WxhGj69717OLbPtyZZYs0zey0TS7+pQ651+ligBQwafkhFteo/OjO8gkbq5/88TwpToyXdOQboRkBLxdZh02bboqtvlN270ypGhfer5n4r9gi6LXTRIU9Ovi/oUt6tnYSCz9xvfTgEdnPocM0IAHnOFNRv3K5xkC/abVExrXHvjaGuuftjet+FtJD54UpuzGbmE2XCoZOZswHgfmERa/ymTvHGH6BV7Yub4DLJzzQzvv0Rv33k2rfuvO84I4kAwKEdm1mtAjsqDCuqgLGaKIu/ufnv1Umj25MPqw7+uhN1p1cw2+a3GuB/ORq6qmE9eDp52b321iFVOS4DobiGLroOmGz59Zs44JbLktxtjgMmNG65Jg76gEdy8ifsduz9LgnGpNBjvtw0klWaq1ezVEvxJeUpiaLKps6NrCIy8Nz22KxrN72OG2fWayLeQS8OKAw6JzwMATGnTh6WxY35jquAtMTEvehjm5NeYSUVQrqe/q2/VMZ6/gsQBp2CDbhY+9Kc8OUurgyPVsrUPFvflbm1aaWpCA/cMYZzSxIKx3m1Jif5zbMjAPTlI1z+7BrX7NDMwNeoo580rcLp+hd6+DbsJllDIs0UdctdB96TEwzCyYCE0Q3iVCuqpQcGhjSdi33j378txdSfWkv2fnWGiOCtJhyVwLRkD/mQNXQyjnE45uMRVfafolT3P03XchVFkhAthsH1UKJI3xYWYCfghl//OGBJfzAtJiB7TQ0MFZxiK7PEBVh0tKNtO2eDGtF0dmfmrs/bA6wKj/ysqD2qCxzj2sIwCrdPXyQCEILnJbRoUZXadnyiAytPWRMiSZnoAnpDOX/7YR/Yg6Rbr6q/tKlIEgM/CrWH9Tme1cBfRAp0kyoJgRrxE3OpN+PxHNIc4dj5yiDpCRecCEqynxZF8iIQzC3a4z+gB8CHxbmwhZVXWU8TlMs+fH6nTUMC0hT+tBk9N1jMPq9BUHnQYd3kqhjWzt9nklVbFp+jLW+GYzIiGu4NOWqwWsEmSi5XMr3GNXnrHz42ayTY25kd/iGojkePg6WARFz/A5f1DnWM2uzO3W5WgF5fUiFG6i4eAHYuOXmqnek39NhU+4VejpNABd3GesSJ+Tf55Oynvx+pMxZ2mKNVRbI+gBiB1diDCpJsd4/jhbSyIPJIjfEg61l1zaC++x1tr7+1E8vfFbTjnJBJO4hFNm4hkIeCVxrd57I265k9d/0Ys65NQOm8NkN3lpDo26br574XrB2ftazxL6XFdps+nlxDWkslgVw+TA8LZgS2Zxe26NADOMOAppY6EcnI6ZUGORE9OOAoeicPAtXZisUXQIckFj9YhkY5F6jL28qml0/K5omxStNTxikdanwFjD0W8zfbu2uBmK8nHxmjQDPWw2uyZBogs/DUbhivFasF/qcdQJKTtmObDudCS92yCaSqJXyhMsBJ+v4Pf48w/+sFg/q7cxC5cFYBkNAFMgLQaucVRNfZZEOyIoEiK6Zrhbo3slJnBY/BtyRVXAbl08vjcl0SuNwDbk6Zk9xx+QscvmjnPwJKks9j+kbptnZu4SNIvj5ABcXVcvbYPxAFEDgb42rCp8DQeHN0eBKCi/ZxQt2bvdL10YXzSuzRfK57T0ZHMEqN4QqIECCYu8+ImS+LL85AC7pyWLKjf+XXhvHDf4b3fDeb/MlM7xbkqnvAratDVjT7I/e1VcsjxoQ/n4loH3oIyZHwDpSDkW9/lJj+A0n0Pj1eowN8p+x0oHuhIdwyySLvAzjWT3J4JM05cHv/FKxuqk80J4SVFcIKKgcYldMOjrYNTTH2g+nm7izgBkYLJF76ZLu4PW47T3wrzuAhe3IpjkDiZBhMthNx9VOKz1SaZvTPHZn0vEF9Rs0b8/Kb2ybUGTfW/6h2oZzLeRWS7EGtxGGJ8YJhIx2e+R7YWkRsJu2HwEMkhYrvUO7WucojBkf1ki3uplxbd65QQ4zGHXBa47+r8+61CRh3T4VjSrPdZqjjEsY0CQqKwFT1MYSwEfrodTJJu35XrRAu/JlwRD2z7INZCF+KxzSZox2DHGe2K069VVe2qtSmgicvDunBYzqOv+pju5qKhdQ8Z3tsN5kJVZodw1KXR/soCEnlyR6FnfGRaXMmPkR230mFIQiApOpfAEJ5g5BejsMYjMKUcUI0Jmvx741BTM1n25RcRIVk9Lu6zm6R8KyZMelxE148rKbisWrPeabOHCqb5MOjyALFXgamcp+FRHYIG/z4LGeR/LZ1H1e5u4+8Ep9IGWJ583+lqqg+0L+RoP+ZdIuuJdUzF65XL1n3m587kpXEkubx41Ffcd4P2KtphOS0O3crTe+tp0XT79KjSpNB4Zh4VacCE9Wf1OXKUbIJFLzApLIDEPSpuMpruzX1HYo2/Sl5AEb/CuMwWucxFri/SJR/xyz0CrRO2ctSQpNDg7gzY44dfBdLr5w7PV/tJU4gnTbccf9ByQRxlws70rpt34hppqFrRxRnk/0QyKy87HyQYHkAsfIy4setr11if0CKyL2vBe9T0Z2luFrwvFeOPywSZG8G9Du76ithiMmDptQorLPYtPLIIh4zFUySlJYdBGwtEH39jdl4QCq8ylSuqZLNUUBmUK3DUWj+dhGGvMK35Njas54fRg6AbnfjH3c6Z/zCJ2L2wmppeGAO+MM9Ky0bG2JN674oFaW5wcZ8tGOOyQ6tu/UTcXJU1jtg7e0NfJJKz77VaOLRWnUxi8cp414+s/aklCOO2/kPm1bNYfqvJe0Efq+jKzTTGBzdC3TNiczZ7q5UnrjOyHqjh822xeaH99T50Vnrhp2rbPDHx1cN/d14VnpGvToCZlZFgdJYUQf+FcwiTm54tLYv3A+wZ3PXutnMtvIuAJasFUMnYzGbCamKbwnDErNPyUQi7jiKH7swQBQ23E5JEMnA2ePyI4amZkPCksq+JzSrgZ4MlKMr75F6P1/3dVJokhKiphW9lh+g2pSMca5UloWoVsbpcCd/XKVM9KarOoZMVN/yfgbQNBfsmvjuuqf3JEURWaUEFxuULmdkgAgqMEhSjhRAaSC3OoPxRMvFrXVbAHAsN/z20q7n0cVmjKcdpDOBxxPMsza40gyxcKZxhrwYKT978KRaRNbYVMutW+SNOhmuA1HoqgZ9Jn8v7cAChCd8yKZWFuwqfys/+wx4y9GECQBEFwkGI02Ox+vdg4V6t8tMmFjpYjqOnLqQbr6WngxWBMQU6C8NTO81OMVN0Q3ORdWCLxPQjBBBTOD77ibxyc+ROp8mEQxmPCMKQaMpHmK3+R4uFUtFKW6wWtiwetTO7gbWgyj4KRyL+6weS//cdzBJ7wVDxiJlECKIewRQkQYhTtXk+ZMAgmgA0F0tXw+0iOS1fuqLCV3x9kKQ5+ZIXXM2PS7l3bvdAyQMiGMk8IV8hdmk+lD9fMzw6YBBcTg9MM4+irUleFJL3sgW/GZZLr4WAwIilk1BzQD5P4jLJyhjZPviueK0EJYm14mJ0ZmFLbazjqmUBeJMQnI0Sa/o3o9Gg/ajik33vZK2Os15BGvH50Z9VeD7ODYUNOQm7PRBXgJs638njCd1zlaN/KXVqfpXGWjrjBENPOuDGTBR9LnUpRok8GJ0FtImvXlkOS9EB/z7OH6eldBoK3HxkijXmztwqsrHM6hzKO0b3NpM0EUOYG2fOIhqlvfmMaKVhPo4cy/n+XKa9qYiVD9tBVOCLcLsZGR5Z0Lxux+Hi0Ozsh3pS+S/aJnOi6ZjJ1Aljg1UM1sPB9EhduiIQCrV/L3Tg3t20UC090mxfPctlinjyLxJTNBnUcytyIqI33zsdQkQSEAxuA1si8LFOrngs39GnIgt97wpoCVifrHG7aMFPRBXd0zOM8aRoeFiNvg/sNTfFs+wy87c2+xu9XL834/Lrf8TYrXDfyPkavFU+ZAljq87myC7/IyBPlrPgM/alrRGFf0Y27+ghmlsuAuvxgBWdDoDfiLJMFD/Vpz2/cDloLP5tofdx8k6ualLrb7WdpqPK9tV873dhcOcmMa2TtGjh/y6YpmBiebK0VTQFDLigWqZ31ZGopzw7HThzvKdqwtz0sZEsXDzKSHRQ08BNTEtxqRkSBsFQWGWT/p8jnNFF41FImsPio/9qJIbiC9VRxkJ9cjYuhmN/9YGSD2xj99KHf1Pl86+5mrrAVSCAxkFo/AhWi51fNnHlw9fsHPyS8zLWr3K7kXnqUhi7tE+EGklyxu2rtOEO6MX9u+C3tJnqJWK/Uohg4R0VE5Iu6exVLZowNiYJWwqicQveT0DNbsCZ6er5wsgm/yFefmaitK7svRxL04kTUjyE8G1i6Mjhtb8R/0BrH20JN5y5zbahFoos1/X2P3exuj6wAt+tFi2Ril5ER83fEdF+4ZooQ84svu+dWawtJ8dwWH/rnFjPq9wYpoPqb/52w7O7VzB8RqeU4Q/S8tEk8gSR5Qmxt6Cs4Uubk7YndqQqPJMZ6uZGAfr/9i6w+tPYiW9aukDTrV7SJb7m3wcFzTtCYYZqsIvodIradUtOwLuJ0JhkppjSRXpM4c4fWf/z7KL7z8YXFRgJiof46pnsWWjSXDu7s21ccgZAzE2dg/7gyptPbpcBJfy3IZivM2gW9sUwq440oKHGmt7/Fn2Bik6yubHbC1B03RpC7wbFbpBwWU/73GDtLSZGc+n1dUDXdV3d23L16TEqh+V++ebvbUCPorDtXYkXTXqv/7WPaOu/2f3D7ELp8oXkNCs0yEpz9T3dXmtrlv6aHtGx1onxyV6lzZa+f1xrBQde804ta1ACCkKq96fXupFGu0W8EMvIEFN279Z2Aeu8/P584PP1/YnvWVn4XDsEiCH4Rj44YhvS/H+xIhNlSDUWM1QlAL/yrm09BQF4rMBMMl6WiaztOjqqiFp6ZVFujoGwA4C/krcEdXACFrYvh//MTK8OAkcO0ucuSGsQMCYyGPH7C6gtR8inC515v+1f8+CVcmA9oYQtwT2iWiA4VRCSLMnzhtVJwPPlEAYqUtUcBlOwXEAZVbSJg2uL+s4i+ShWrXIXy/0RcAkADOsPkQECNCACodmG7FcJyfCuMas9WhK/2rThSRVvxYkkBFbMFgNE4xQpVq1RmrBoNAtiVKteoWqE6Gc/JOvVGJtwiwQIpMRnFbDbHc6rg9CwJAZUGH0lUrw6nSHwnIjExp0EasxT+0EiieLgR9DrpNN4AlVZYkRH4WIBCJ1WUCkKfochYSSQqlFibhZoFCtAeUfmsKDrW70GpJqXQJKDEKoGAYw3/hAcA); }&lt;/style&gt;&lt;/defs&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(10 10) rotate(0 0.5 0.5)&quot;&gt;&lt;path d=&quot;M0.25 0 C0.35 -0.01, 0.48 0, 0.75 0 M0.25 0 C0.44 0.01, 0.65 0, 0.75 0 M0.75 0 C0.49 -0.38, 0.44 0.56, 1 0.25 M0.75 0 C0.52 0.12, 0.27 0.42, 1 0.25 M1 0.25 C1.01 0.38, 1 0.5, 1 0.75 M1 0.25 C0.99 0.43, 1 0.61, 1 0.75 M1 0.75 C0.83 1.24, 0.72 1.25, 0.75 1 M1 0.75 C0.63 1.58, 1.66 0.4, 0.75 1 M0.75 1 C0.61 1, 0.48 0.99, 0.25 1 M0.75 1 C0.59 0.99, 0.43 1, 0.25 1 M0.25 1 C0.74 0.53, 0.16 1, 0 0.75 M0.25 1 C-0.45 1.43, 0.02 1.3, 0 0.75 M0 0.75 C0.01 0.63, -0.01 0.49, 0 0.25 M0 0.75 C0.01 0.57, 0.01 0.39, 0 0.25 M0 0.25 C-0.28 -0.28, 0.24 0.66, 0.25 0 M0 0.25 C0.7 -0.59, 0.56 0.17, 0.25 0&quot; stroke=&quot;transparent&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(105.4921875 10) rotate(0 200 42)&quot;&gt;&lt;path d=&quot;M21 0 C124.52 2.66, 231.19 1.39, 379 0 M21 0 C111.92 -0.12, 202.46 0.51, 379 0 M379 0 C391.15 -0.02, 398.93 6.31, 400 21 M379 0 C392.63 1.94, 400.84 5.61, 400 21 M400 21 C400.5 32.05, 401.09 45.51, 400 63 M400 21 C399.56 32, 400.43 42.06, 400 63 M400 63 C401.29 78.06, 391.18 82.63, 379 84 M400 63 C398.04 79.26, 395.13 81.71, 379 84 M379 84 C297.58 82.02, 215.03 82.5, 21 84 M379 84 C271.24 85.44, 162.68 85.64, 21 84 M21 84 C8.14 82.16, 1.73 77.6, 0 63 M21 84 C6.61 84.4, -1.04 77.67, 0 63 M0 63 C-1.67 51.78, 0.69 37.34, 0 21 M0 63 C0.08 50.51, -0.74 36.67, 0 21 M0 21 C1.4 8.29, 5.29 -1.92, 21 0 M0 21 C0.27 8.01, 8.56 0.05, 21 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(239.4921875 22) rotate(0 66 15)&quot;&gt;&lt;text x=&quot;66&quot; y=&quot;21.144&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;24px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Disk (SSD)&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(251.0421875 58) rotate(0 54.45000000000002 11.25)&quot;&gt;&lt;text x=&quot;54.45&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;model files&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(305.4921875 94) rotate(0 0 60)&quot;&gt;&lt;path d=&quot;M-0.62 -0.22 C-0.57 19.56, -0.02 99.56, -0.09 119.6&quot; stroke=&quot;#555&quot; stroke-width=&quot;2.5&quot; fill=&quot;none&quot; stroke-dasharray=&quot;8 10&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(305.4921875 94) rotate(0 0 60)&quot;&gt;&lt;path d=&quot;M-8.7 96.13 C-6.59 102.31, -4.58 107.01, -0.09 119.6&quot; stroke=&quot;#555&quot; stroke-width=&quot;2.5&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(305.4921875 94) rotate(0 0 60)&quot;&gt;&lt;path d=&quot;M8.4 96.09 C5.93 102.19, 3.36 106.9, -0.09 119.6&quot; stroke=&quot;#555&quot; stroke-width=&quot;2.5&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(333.7500747592552 139.28348532506152) rotate(0 128.89179687500004 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;once, when the model loads&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(105.4921875 214) rotate(0 200 90)&quot;&gt;&lt;path d=&quot;M32 0 C133.07 1.49, 235.3 1.47, 368 0 M32 0 C147.3 1.06, 263.34 0.24, 368 0 M368 0 C388.56 -0.81, 401.24 9.55, 400 32 M368 0 C391.63 -1.6, 401.11 9.17, 400 32 M400 32 C397.66 72.73, 399.32 110.42, 400 148 M400 32 C399.43 74.08, 400.57 113.81, 400 148 M400 148 C399.48 170.34, 388.74 178.18, 368 180 M400 148 C399.46 168.25, 387.81 181.9, 368 180 M368 180 C259.36 180.27, 149.77 179.99, 32 180 M368 180 C292.93 181.81, 217.55 181.6, 32 180 M32 180 C8.67 179.23, 0.76 169.3, 0 148 M32 180 C12.63 181.41, -2.08 167.57, 0 148 M0 148 C-0.07 115.44, 0.88 78.63, 0 32 M0 148 C0.86 124.21, 0.33 100.37, 0 32 M0 32 C-0.21 10.73, 12.06 -1.43, 32 0 M0 32 C0.89 10.08, 9.48 1.78, 32 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(232.89218749999998 228) rotate(0 72.60000000000002 15)&quot;&gt;&lt;text x=&quot;72.60000000000001&quot; y=&quot;21.144&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;24px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;RAM (16 GB)&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(201.5421875 262) rotate(0 103.94999999999999 11.25)&quot;&gt;&lt;text x=&quot;103.95&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;shared by CPU and GPU&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(145.4921875 300) rotate(0 160 37)&quot;&gt;&lt;path d=&quot;M18.5 0 C120.48 1.24, 221.03 -0.46, 301.5 0 C312.19 2.35, 321 9, 320 18.5 C317.32 27.43, 320.75 33.16, 320 55.5 C319.76 67.96, 311.48 75.32, 301.5 74 C190.66 73.32, 76.24 76.48, 18.5 74 C6.91 76.22, 0.64 65.8, 0 55.5 C-0.68 46.73, 0.77 34.44, 0 18.5 C0.18 4.7, 3.51 -0.25, 18.5 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#ffec99&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M18.5 0 C128.29 -0.39, 236.87 -0.83, 301.5 0 M18.5 0 C111.93 1.42, 205.04 2.52, 301.5 0 M301.5 0 C313.78 -0.76, 321.68 4.64, 320 18.5 M301.5 0 C313.85 -0.02, 320.62 6.16, 320 18.5 M320 18.5 C321.7 32.78, 319.15 46.29, 320 55.5 M320 18.5 C321.02 30.62, 321.04 41.43, 320 55.5 M320 55.5 C318.07 66.28, 313.63 72.41, 301.5 74 M320 55.5 C322.22 65.71, 313.99 73.64, 301.5 74 M301.5 74 C210.9 72.46, 122.09 72.52, 18.5 74 M301.5 74 C219.13 72.36, 137.48 72.8, 18.5 74 M18.5 74 C7.09 75.57, 1.34 66.3, 0 55.5 M18.5 74 C4.34 75.72, -0.22 67.27, 0 55.5 M0 55.5 C-1.5 46.23, -1.19 35.04, 0 18.5 M0 55.5 C0.27 41.63, -0.55 25.89, 0 18.5 M0 18.5 C-2 6.64, 7.37 0.21, 18.5 0 M0 18.5 C1.21 4.96, 7.51 -0.62, 18.5 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(226.8421875 310) rotate(0 78.64999999999999 27.5)&quot;&gt;&lt;text x=&quot;78.65&quot; y=&quot;19.381999999999998&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;22px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Model weights&lt;/text&gt;&lt;text x=&quot;78.65&quot; y=&quot;46.882&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;22px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;988 MB&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(305.4921875 394) rotate(0 0 60)&quot;&gt;&lt;path d=&quot;M-0.27 0.05 C-0.32 19.77, -0.97 99.01, -0.8 118.93 M1.79 -0.97 C2.17 18.81, 1.73 99.64, 1.49 119.91&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;4&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(305.4921875 394) rotate(0 0 60)&quot;&gt;&lt;path d=&quot;M-6.88 96.35 C-4.53 101.09, -4.24 109.67, 1.49 119.91 M-6.88 96.35 C-4.21 101.79, -1.78 108.62, 1.49 119.91&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;4&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(305.4921875 394) rotate(0 0 60)&quot;&gt;&lt;path d=&quot;M10.22 96.49 C8.18 101.15, 4.07 109.7, 1.49 119.91 M10.22 96.49 C8.36 102.02, 6.25 108.82, 1.49 119.91&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;4&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(332.55078125 416.73828125) rotate(0 66.55000000000001 13.75)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;19.381999999999998&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;22px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;every token&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(330.984375 451.14453125) rotate(0 135 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;200 GB/s (memory bandwidth)&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(105.4921875 514) rotate(0 200 98)&quot;&gt;&lt;path d=&quot;M32 0 C130.93 -1.92, 229.54 -0.42, 368 0 M32 0 C99.72 0.58, 167.53 0.56, 368 0 M368 0 C391.01 1.7, 401.67 10.05, 400 32 M368 0 C388.08 -1.6, 399.39 11.39, 400 32 M400 32 C400.13 74.96, 400.68 120.81, 400 164 M400 32 C399.65 77.23, 400.09 123.62, 400 164 M400 164 C398.57 184.92, 390.08 195.1, 368 196 M400 164 C401.35 185.68, 388.87 196.12, 368 196 M368 196 C236.27 193.44, 105.27 194.3, 32 196 M368 196 C234.17 194.33, 100.95 194.72, 32 196 M32 196 C11.88 194.6, 0.12 186.23, 0 164 M32 196 C12.11 195.81, -1.19 183.11, 0 164 M0 164 C0.55 133.14, -0.84 107.02, 0 32 M0 164 C0.75 124.47, 0.03 85.39, 0 32 M0 32 C-0.28 10.8, 10.83 0.53, 32 0 M0 32 C-2.2 10.68, 12.24 -1.1, 32 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(252.6921875 528) rotate(0 52.80000000000001 15)&quot;&gt;&lt;text x=&quot;52.800000000000004&quot; y=&quot;21.144&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;24px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;GPU chip&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(145.4921875 568) rotate(0 160 30)&quot;&gt;&lt;path d=&quot;M15 0 C99.62 -1.05, 182.47 -1.22, 305 0 M15 0 C103.22 -0.99, 192.05 -1.08, 305 0 M305 0 C314.37 -0.46, 321.69 3.91, 320 15 M305 0 C315.63 -0.35, 319.58 4.69, 320 15 M320 15 C320.83 25.44, 320.87 36.44, 320 45 M320 15 C320.78 26.27, 319.23 35.94, 320 45 M320 45 C321.13 56.13, 313.88 58.41, 305 60 M320 45 C320.24 55.61, 314.63 61.85, 305 60 M305 60 C242.15 58.48, 178.49 57.69, 15 60 M305 60 C242.28 59.95, 178.99 60.61, 15 60 M15 60 C4.09 59.59, -1.56 55.3, 0 45 M15 60 C5.33 61.29, -2.17 56.61, 0 45 M0 45 C-0.09 37.54, -1.86 31.06, 0 15 M0 45 C-0.63 37.06, -0.72 31.33, 0 15 M0 15 C0.46 6.88, 4.63 -0.58, 15 0 M0 15 C-1.42 5.57, 5.4 0.71, 15 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(228.4921875 573) rotate(0 77 25)&quot;&gt;&lt;text x=&quot;77&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;On-chip memory&lt;/text&gt;&lt;text x=&quot;77&quot; y=&quot;42.62&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;a few MB&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(145.4921875 642) rotate(0 160 24)&quot;&gt;&lt;path d=&quot;M12 0 C104.82 -5.14, 194.3 -6.16, 308 0 C316.29 -0.84, 322.1 5.06, 320 12 C318.82 14.72, 321.51 27.67, 320 36 C316.52 43.95, 314.25 45.13, 308 48 C241.32 45.38, 173.95 46.01, 12 48 C3.77 49.27, 2.48 45.98, 0 36 C1.73 31.9, 1.77 24.43, 0 12 C-1.55 3.74, 5.56 0.21, 12 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#a5d8ff&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M12 0 C70.11 0.75, 130.18 1.71, 308 0 M12 0 C123.71 -0.15, 233.67 -0.28, 308 0 M308 0 C316.15 0.03, 321.53 5.76, 320 12 M308 0 C316.06 0.68, 321.19 2.08, 320 12 M320 12 C318.7 20.33, 320.75 30.37, 320 36 M320 12 C321.09 20.4, 320.54 26.66, 320 36 M320 36 C320.85 45.37, 315.01 49.87, 308 48 M320 36 C318.37 45.62, 317.31 47, 308 48 M308 48 C235.84 46.46, 163.47 45.38, 12 48 M308 48 C216.19 45.83, 123.5 45.41, 12 48 M12 48 C3.49 48.89, -1.1 45.25, 0 36 M12 48 C2.18 45.74, -2.13 45.7, 0 36 M0 36 C-1.67 31.25, 0.88 21.54, 0 12 M0 36 C0.29 29.47, -0.1 22.3, 0 12 M0 12 C-1.88 4.88, 5.74 0.34, 12 0 M0 12 C-0.78 4.36, 3.74 -0.16, 12 0&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(226.8421875 653) rotate(0 78.64999999999999 13.75)&quot;&gt;&lt;text x=&quot;78.65&quot; y=&quot;19.381999999999998&quot; font-family=&quot;Excalifont-135a73x, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;22px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Compute units&lt;/text&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;On a MacBook, RAM is shared between the CPU and GPU. To generate output tokens, the weights are copied from disk to RAM once. Each time the system generates a token it streams all 988 MB from RAM to the GPU chip, which holds only a few MB at a time.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Memory bandwidth dictates the speed limit of this process:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mtext&gt;model size&lt;/mtext&gt;&lt;mtext&gt;memory bandwidth&lt;/mtext&gt;&lt;/mfrac&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mtext&gt;TPOT.&lt;/mtext&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt; \frac{\text{model size}}{\text{memory bandwidth}} \le \text{TPOT.}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.2519em;vertical-align:-0.8804em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.3714em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;memory bandwidth&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;model size&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8804em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;≤&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;TPOT.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;In other words, &lt;strong&gt;the lowest possible TPOT is given by the model size divided by the memory bandwidth of our system.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For example, the weights for Qwen2.5-0.5B take ~988 MB of storage. As a result, on my MacBook Pro with the M2 Pro chip (16 GB) and a peak memory bandwidth of 200 GB/s, I can expect the lowest possible TPOT for this model to be 4.94 ms.&lt;/p&gt;
&lt;p&gt;We can characterize the relationship between model size and TPOT using a linear model&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mtext&gt;fixed cost&lt;/mtext&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mfrac&gt;&lt;mtext&gt;model size&lt;/mtext&gt;&lt;mtext&gt;memory bandwidth&lt;/mtext&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;TPOT,&lt;/mtext&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt; \text{fixed cost} + \frac{\text{model size}}{\text{memory bandwidth}} = \text{TPOT,}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.7778em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;fixed cost&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.2519em;vertical-align:-0.8804em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.3714em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;memory bandwidth&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;model size&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8804em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.8778em;vertical-align:-0.1944em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;TPOT,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;in which model size will be our independent variable and TPOT will be the dependent variable.&lt;/p&gt;
&lt;figure class=&quot;figure figure--bleed-mobile&quot; style=&quot;margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;svg version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 623.3204067335557 418.20117995552215&quot; class=&quot;diagram&quot; role=&quot;img&quot; aria-label=&quot;Sketch of the linear model, with TPOT on the vertical axis and model size on the horizontal axis. A dashed best-case line starts at the origin, for the full 200 GB/s and no overhead.&quot;&gt;&lt;!-- svg-source:excalidraw --&gt;&lt;defs&gt;&lt;style class=&quot;style-fonts&quot;&gt;
      @font-face { font-family: Excalifont-172z9af; src: url(data:font/woff2;base64,d09GMgABAAAAABbYAA4AAAAAJ9QAABaCAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhwbiFgcghQGYACBJBEICro4qzMLTgABNgIkA4EYBCAFgxgHIBsQH1GUs1oZZF8kZHIXFtmIRHYrahMVvH+PhSP9wM/t/9xcw9bQayoGMnpRRI8okQpxjNbGKsIqrEZ85fP7MDGi0Fdh1ReEaC2zs/eqyBJYIygCyWhElWuFQ6FaK4hCdi8jSiQxjWDd4dqe+tIclSZuJ0gondJt21BOLekrPW9mZEjrgm05eJS0XQC244NXDl/7+Mvf0XdAYAgIDKHd2HG4FCjBSHPw/+/nKg/3pFLS0kIhpJXyNr97EwXe/4hpcm8eIodQ3KJLIq7RCAUakRTIlZWW8d/hsvpP7DHSkIUZ+elROWMd0olVBwQAEwAV0WkkEHKE3BcAXTANfN6UbAWXo057A7ic2SvrweW6tKURXOAAYPaoclVpbwRqLw0BEGjOAVIjKpYMGstazg+sQI+AAxGMOHr3cYDL0Zub4Jnoad2297/7q3jZOgdtl+zdQKcdCUA9/o8IHqccDJr1+ushdM3vxxhH/3aWbvQucY2echl95ueB3wPfLFzYQLPHg+ExCTeB4CDRMLE4cMTGxcMnJOLClSdxFFCMlAPCSChlEQuC1A2ZUEI4nAfBFtgSqLQljobDER0XAw+IwAI8m2ajA2pFcO9VEv0B1c4DAAhDETg3ETCHUCxh9zYYlQyvvgJyYOhe3C5/aO7xqsv3wuvzQOGANLO7IwGgZ8mggISkJM5NAkKckwiEasuiEAKmRoC4DIKQxv87p8Q3TIEqu6fhdW11KYjv7BWMYO5ngM+ohJ0SevffDlOxvphmgrgJ6wKMjFQCssWP0hy5MCWIIuwrpXL0HYsIPVsCPItDrImLkZMTzpEjwaYMk5LsTySJA4f4aTjQaQDYuGJPEKqd8bAi3UCgxgI0DwCSsrkguxDqE3AjNjRsmlNhCMQm5ravC4DYLou0iQE2AgAgT/2kiVEnjwHgjy5UHrhuEAuCq5P58BdKxyxFOqsqtSZo02Hi/yiLvG2CUFrcUjUa2L9Pz3133TbspGP+54j99tljl512hEAR8q/IHZVQcuNwQHsGYBx4lwRKRHigECWme0IbG0SompcIu1QENhTko5CKgvJ0Ql5mhbtPpCRNxVeKwmMcfPhZehc3RbLe2SNcU5g0qT+rj/eHgoYK8LNr2m+WonvolcykLPm1MmjoAoLLBcGTF7ZIVgVm3btuj/bgRmuCdGuY9VW1wbnBD5/nt+RiSl6KAKx4Fa+Rb8SxPnqZiEoioClmBPbVRts3eMfvGNzniHu+MdBtSKkDHTEhouMsQHCNTLH81v8/ypPq3Ochv1rIjGddAdi6qAgcFpzt9eLScJFt/orQ6uW2lBHJnGIhXbH0x4b9w0sBhYYeG65+J31385GG58nvCMsJSIJfXi5hhJnqqY1C4/8FNRAPssFJfsvg0uS+wYub0+0SdRUFqpmqWwlqwW7f6MvP7ENY57cHO4W1e2/iYBh0M86NOQgmjDAQHOQ237v9+KsLw56kKwv3baxMONsvupV1ARJnlJTChxlawUMNR7solVA8WsKLOEVR1EerZqzbn3yhtxcbaaoS3ChrGGPGGIoca16NpBMmZ0KUElgRVUJgcknK26IqOI4v4sOtfErReZrH8ySBFkiefMEOQ+NC9Q4XN9Mb55Tgz4R5qF0QfzqVw5R35HeSuW5+Fm5ktz8+3ZaDYbAXHJEnQOBK8slI3pQqzNLU7Xv5tknHYQipTiXkY/gwJYBsr4OfSYSizVXkpTmW5hrI05xtAbrzB9OR9AcBQuyWxUH0By8/+IjLLZn370xet2HYy1IKYc2CXBo7deYPDDuEkPYgTaBrZROkdyHt/h2niylLddJjWaJJ6aFAa0OZsKriD4LscTw2BofP70dkejN9c1fDb5OuEgR7mSNAHOJAetU97k32ageq5I7Mry1GqJ/3bvta+RLVQz3BcjPqUges39oTxeGN0yw5qpybWZ6lRJkYOPprLE0e8zg2eAOxfhSh3bSMIwJIExCR1e2lYI5J1EiNxlojx1BarxKwN/8owvJyJZmPY8MOQ9OWdSrPStQlkBAQ4SIeahoGRaI5kCQKTS4ppOKm8rUgnWLMK4/RMDry9lvcuK3bNRpSAENIazSBG9CaBjdYi2tCyFsUf7LNx6P7iUYHBaIYPFmf6c72lk4ppTRR9+V1HpTXpduXmiIjcHDzwrC/GCUT7na3Ikhm/HvlSMNfEwIJtSjMPjyz8BDvonS6We/cf++2XIj3e5Oum3EADhhqI7YLhi9mrKYL3S/CxKzTnmLdGtbwFENemlelwlGSNQr1Al/zO4YvVd+k+9C9WrWqjsgo2d+7Gwl0AdFw+WTxTlasT6l5rNuGZBJp6VzwqvxIshHDM5FaER8HGv6y7CgryWejr8z4szirHM0XRVNQyqFOaWjbenw7739ZxnvdSqbGHJeO0hCT15nwBiU+Mhbb9c5Pg7f42KiNIKUutFwHOGXWR9E8DtCuqIpUYD7/wxLtFomGU7Sr4aKSUZpJ1iLBEi5vprk0HzzpCrB+i2h4jqkNr8GNjlxNc2vp9euMc06pXuDdplZyRXKs25PHydUkahHs8FcSPUJRO+8fliinWArSd2gT/CmY6qMIw9Xy/gLNWi13BVpNR/GDjTBim21vVV2ThVV5zadLmLQorFFlWQjNTVqtLAWkNswxT1V93e7BrHOVLmeQOyyFs8pA5rfM0eQvNafVgm6GBCjKMa/NVlVPI12kA/A2UFsREESIqarGgACyNFg+StOsrZi+hp/+FSLjlN9VfWk0ckxtDKCddRWrqfCtLYMPBsbgJg83aDbRalZLcQEJaufvg83LddYwDuuHup18kJQFkGFwXrelTDDPY32U6hgEgHTBXoY4LYVuwPCvO2/d/uyi01Ms0NVwhLRhpbIOyDpYzlKXOMRT0k9yi21xt3nf5CGkVLFqde4bZwM6GoUXum1yH0UpSon1PdR+6zvJdmg7+fpaFXxKhkEAiAP3k8lEs6qnsru7tDvFfm3n04x/2UMBwEMUT8dmHeOt8WeGOljoZWkCS/FFvLAwmuiCYCgAngdv+jrWLA1NDjDaH9EvcZrNLoP17pPPyXAJRxFiHnO1RoA+Up+dIG/VkypXVXadDww+0i90K7CuLM/SkOr2tORCJ0hPpnMawt6EBZpWcqlVITXMUP/eXBr3PmznRzrmwOTH2QSzrtuD1OC3+RYf+IZf2JnmJaqHonzidZN3hScg5EYwrpHyZl7lb/GvuMwzr4/uhVU+zXWbUjhJk0mqf+wlDXgMT5PToyuWS0C3KIDAGi3RkEIXWEUWRDfZtBjWgu4RIC9AUItUxn3jUE5PD1RWJadDdFDwDc6pvjCWshBegs5M8ELYZG8PEEJ1e/zYlAs6CiGtEAE0ohUx0Kp4iP8deTmyBx7o+JXamB7EZjzWx3pjfrqmeTIMImZwIx7Hpl1KsGq1gNOtDst9w/+K6/YnOh3DDn80vYxzi0S7SCPD4V8t2tNt86vr4nUqqyk3nuv2hv5wQFtwowrWxbGFdVvmGr5a+AQUSJEd6rQ+J6hBmsFiZtuMF1JM7XBypRiUtdTyyS5fgnYSX4xNWeptz3+KMYiKgfa5BVcfvU8C4N0PZSUf1j30Beb/R2CwpQ7r7v1qWfEum57EmRN/NDN1pVah4IztisVi+ZYcQ0tJww9xxoU0HWucYdvbg+wTyRfZK07Pms+cARO+KG9JHhUxL0DkG+pGdrr2U3SctZ45TTkL/mf4ApzqJz84A3lbi0y4ryp2NzRIyp1X0PLcg/YOWKvB9PNOSsW8Y5yLomYmEyFuOIPnpfb0Zp1qjhVg3nwbm9e8GSmKqJmlAbaAl8FjTsV75+Ibx/agLgZYdCzfPZxjsb/q28/I+HB7viLXN6rVtKDhhLrganAoFcj2dykJj2R1xTHzGHRRoX/7pIhVOgZH02I1IgoqkalIHzVJ7kgy/GYeWKcqO2wt9ky6Cjt9+l2kQUFnVwwum65eQBlLsT74fBKcanh/IQZkyAPN8WKQftWLDYF+M5uprZu++zgjzj/rUFb5p4o+gh4e3Et5RMnA0xjY6YROHBZTV/ybK9g3xvQtuqx7yzxYtvCLdvGvL1z7t68/11skQ4SIAlGJM7WasB8VQaX26Os54sin3/xx+l1l/pTSmCO+/91/oVW3NxI9Kwo9eL86hjZ2430E+8ySXhO/nJWM2jNAN51sIClGQiNptSWf/ylGSFvUiZ1wGGJgi8FUNFZ4Cm7SMS71ac8pfQChQh9rk5D0UDurs4Bn9nzbYCUNMxOKtB5sJUfHPaK2rv3eZtAR6Sz22Bm93v/KQwVsfRW/s92MSWlrndlr1G78CmxS4vsZaF9m8KlM5/i5AlNrYdSn+BYaw4Leg+ze0uGXHPcUjc88t0JovR6TYixTj8+srm+rs+yPyZCTTYZf+fQtqSil5einub49O10MZcW4RZNl4IQvEDQmSHURJ6GkKCe08I3c46gNCZ43P5vSa8WsvQbxNDqnVpmQSPX+COct155fSEx/r97NK10dQMlpAUovNt0gMpY0NudJtWOCohCaY45reky/1ARHJOew26HjBDNWdPanJsmyqAJDkZ7Xd+w5g3LLPO8MOlmuiPsZs+/2zC1cSeYJbULAH3v62VuN39E0CAx0UeojNgpxwhwN0am4r4Sd4jXHz+puKevo8kWKhdP78oVupN4QqEECqctlzklSS1VxSkCmZ2Keg9J4uXSkQxRy2dVwzS83tbvTJdimrEe278o5mOIvWB+fogzaWt05feg1VNUVByA6ekGi65LkX9Es7x861epwF9Y32euyyXXkeF6ZfKmfaaxgMAlyFz88P+dUMUEnX6xySo4WgbiCjE/qQWFgHreZ81zz9mSbaDYwmNZjDnDkvUFbcO1IuOwpMFvLULorDDjrVZVIKsHDzwU6FAWnG9hpyNQmj9GuI3CMcf9U5rMyS1RE0RCVs7FNNjZho1uQkUTcjzRJPKnIarat38bcxuQRmuca4k9KLlyl0mlrNmNO06Q/sk/DCfK51km5uO5uWrbAF/E1/uBfIe2xOKZROLVr1BeLCpeIUkXSQqfFDmn4ZyD6y3aaTsrDdgm1Xvomq66YcweZXGnBOihhibiqL2/Qhte6AL2swuyTGEgrQrKmkFZXwb8sQd1Dzkq62wvCca6b4yIsfbr8dw/LGs/AdKnaNn9lclhIfg57eOLZYSvH/ObPDf7yNNpPpsfZFzgFUFQx5JLxqpz94BNpalzaJSzx+l0zLKm6Fq8YHsKuv428vvyPnpfeYT+iuuitr0M/pyCH6gl7GMWpoxi2c0PO3d//kcyIpdDSZk5MZY4kesdhFKyTQgYLK1K57LFozPcvMl1XhkF6J7Ne7pki15QG5bq5ahI9/gqn8Mf/69zY6GhOODkcttX2jUT0Ptc/drmgHzXTrJUq8No5Ja+qi8tIdnpVPtSUEa/EM/IxoUDV8Picw46y5Jm8GcMP9HaFlP9sTrowIw3X+RhkBX+2E81vtXR3NOtn1ZImAf9CaHU/DNB7Pszt8pko4OqnT9yRL5gm82R3Rw0i9ULnjAVOYYPNffZ0dNL2mbvfc4mGkIGn/5Wekm/KQtqCo8LtrFSq5fqZhMm89zdWxvnB6wZXvWCTUuTcRiWoardpDMrDFHjZQFXe9LOLHPW//+hTH5ZQw2LeZojmSQErWEGqYDSJixWiQocvPjxCresZdxCB0cvjMg1/HU8pNRXYMlVCocTC98zb6Jbni0RwjU2w9Pizj+5i+vauUh4nPXO5Zl7oRFknEsnJ5cx1urgVWB2ulHRKHsXF/Q/l6SuC8RNuBFClQQga5FMHM3ii4cfIFCcWhc4464IyLvt3Ct080WkEZqazAlg/UJYnIJhRfGALa+IwTLR1nHwD+jpKyOwpLAlf9/l5ns/3b/kRzbzYrJG7B5YlDlHzkdyf3Ncqme0nrKONexcETEbLaSFZhg7O+rT1quR/+tCH8bmMZjQExiypShbCIEp35lZVc7VFyv0WkZSkSrQR4Zm8wNSmfsNRzwTGcnciBaOx9S/EJ+v8HCIQ/aFbspwJsvsayZa67vpDHuZsbgZ2HHH5U1wDD3Hf2mMJn/HaOt/a/VrvVfGJ8+KHVaZ98eOnuL2ttAWLk7xzhAlqE0O7qRqRWgP9PU8f4Vh7DFQvPwZCH//iUD0slfU5GPjEgJQpUklZyJZNom2LCrtK+ATNZbGFz9xpnjKfzlMsAHsja1Fk9LaRa7EOWAImRA2gXeDdUqhD/3LfOqBhuJ3vC28L2JCiy3bRhpvKrruS438rkmcRqAR7GTJoaLMIMmcTGS8Ot36+c3P2+/8Gs1/mRejGPqNwmiRTpwJ/+Fqh4vq3CsYkJd+Soz9xl+Y+UPbgqT6Sl8ccUld/XyPcGuiF2aoUIfcHtNe27YEZpe9N7AFRsclRTU874PK1Miz4dfqgdpaxvXayGW/l7x+69ihDM25SREp6k3gq3GeSlCjt/N+nVTplonGTOj3FWw/NCVft7HFCjIxsFhn4jicNmWHGxIGoXBEVlHmlzPskzf1oYpVborf60j4Kho/b4iAJ8lOq8ViW+dUXbj48pnBO/nBOXeycfqBd5D4DpEkC8rW3EJr1zvVz536/4fXzL1Kn3E3rXW4X3vw1i1w1IMaHkh0pT9XaDoPVWLwo4pF2O6dCog/WkhSwTPSnranHa5O60qp06mX5LytznvYFCNtzqHhTH3venpisUg22qSVTF6lfvmKK4Babkyk2cscgGOpr2FZVGKsxCoQWNadTXHZT9cnf5eNQRteypfm6P9l0VHh6gT7b6UEQaJveqGhRAxET5sz7XLOdvrrKbcbl7BfWUvNFXfdxrHt3CauN8ebXPiTmXUCXUfv3qmLXr8YEDBF93sqze7xgK/OdosbeMtFOYWL0BibGfo6jvuaC/jXzg6I5rOmGQDHHR9quX9sleeTahYYs/InNC9fklXGe0Ch7WI0tmyNP5jKwclYb/T+6cNH9LZw1u91Fltci/nui+7fr4zZtLjuZP1I1vVc4zqv7rAHhk/46nmseWbaIA08OHy6PxBi5SbMpVxy5szhz5GCr+c8tX+Bj1i7t7+RMOS/EQUlzvfwT/akmAT3dUSBImLb3wRhGL/zvUWJSSdLee4zdlfQoYfPhHqQh5c12umOkmSJnpbov3+IDXaQRuu1nKtLJrP/UVwd4Gd1PB9+4fGV8uLCiFh/Najqaf6W3J0vN/LntF3a+Okk5pafStq7fTxYSdHfVyeXT1QsACkTeo+I7gyxxjHlLZWC/AwCMdPoaAACebf5r0uisPwn/E48EACrUQvSLtabEZez3S8u/EJsXZS4DY+oGQD7BP/WCd0JAGSGAxyqISXtAGEdBGB+BkZmjG1+BH6PVxhHQ5nxAQgPK8IPw9AHEUQw+4Qyu6eXox4iAcG+wjUBYrISYcIDIyIPgtA5CwhO8wweQkENIsIAVaeAYMSBJ28FNMitGv44rPMsQpTtASv99yAXQwG4UBTBIQQOABtd1OILvWDjKwcFwjK854Ti5snBCHDmQdFUARh3KlWpQq8oEjVoEyFSpWqsGpexydkl2zayQqoqFCBQsaDQzX9DJpqbARqSCUAXeCxQ6fgSf3jmIm/dW3iCLWWrRulVzap0FvaI2nQzUtiI1I4F3C/BRNFSwhLAdlLHdxba0gr2g7QJNVKtBdBXbk+ZHUKlQJbRRlQqBMOA1+hUBAA==); }&lt;/style&gt;&lt;/defs&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(120.6214131903343 360.6657025386599) rotate(0 215 0)&quot;&gt;&lt;path d=&quot;M0 0 C124.81 2.41, 252.18 1.38, 430 0 M0 0 C109.15 -0.1, 217.98 0.41, 430 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(120.6214131903343 360.6657025386599) rotate(0 215 0)&quot;&gt;&lt;path d=&quot;M406.52 8.57 C411.55 7.97, 421.47 3.47, 430 0 M406.52 8.57 C412.69 6.24, 418.28 5.07, 430 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(120.6214131903343 360.6657025386599) rotate(0 215 0)&quot;&gt;&lt;path d=&quot;M406.5 -8.53 C411.43 -4.12, 421.36 -3.62, 430 0 M406.5 -8.53 C412.74 -6.52, 418.33 -3.36, 430 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(120.6214131903343 360.6657025386599) rotate(0 0 -170)&quot;&gt;&lt;path d=&quot;M0 0 C-1.69 -70.67, -2.4 -143.36, 0 -340 M0 0 C-1.5 -125.8, -1.49 -252.55, 0 -340&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(120.6214131903343 360.6657025386599) rotate(0 0 -170)&quot;&gt;&lt;path d=&quot;M8.3 -316.42 C6.07 -320.36, 3.25 -327.26, 0 -340 M8.3 -316.42 C5.61 -324.3, 2.55 -333.61, 0 -340&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(120.6214131903343 360.6657025386599) rotate(0 0 -170)&quot;&gt;&lt;path d=&quot;M-8.8 -316.6 C-7.44 -320.35, -6.66 -327.21, 0 -340 M-8.8 -316.6 C-5.13 -324.39, -1.83 -333.62, 0 -340&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(10 118.9600509503876) rotate(0 44.550000000000004 11.25)&quot;&gt;&lt;text x=&quot;44.550000000000004&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;TPOT (ms)&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(258.80583538209527 385.70117995552215) rotate(0 74.25 11.25)&quot;&gt;&lt;text x=&quot;74.25&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Model size (GB)&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(121.22383938364072 360.6657025386599) rotate(0 195.70957401887847 -101.17786044525235)&quot;&gt;&lt;path d=&quot;M0 0 C109.73 -58.18, 218.56 -114.28, 391.42 -202.36&quot; stroke=&quot;#555&quot; stroke-width=&quot;2.5&quot; fill=&quot;none&quot; stroke-dasharray=&quot;8 10&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(524.2204067335558 139.80726725943367) rotate(0 44.55000000000001 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;best case&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(226.2214131903343 330.6657025386599) rotate(0 114.39999999999999 10)&quot;&gt;&lt;text x=&quot;114.4&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;full 200 GB/s, no overhead&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(119.99655623451116 250.41621708753445) rotate(0 153.56535338302197 -111.1462204155648)&quot;&gt;&lt;path d=&quot;M0 0 C67.18 -46.11, 134.18 -95.1, 307.13 -222.29 M0 0 C119.79 -86.28, 238.44 -172.47, 307.13 -222.29&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;4&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(436.01460062952896 10) rotate(0 39.60000000000002 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;measured&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(100.6214131903343 343.61832302594945) rotate(0 0 -40)&quot;&gt;&lt;path d=&quot;M0 0 C-0.68 -19.03, 1.02 -33.99, 0 -80 M0 0 C0.93 -20.74, 0.19 -41.51, 0 -80&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(100.6214131903343 343.61832302594945) rotate(0 0 -40)&quot;&gt;&lt;path d=&quot;M-8.39 -23.55 C-7.57 -20.11, -4.05 -12.53, 0 0 M-8.39 -23.55 C-5.45 -17.1, -4 -10.72, 0 0&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(100.6214131903343 343.61832302594945) rotate(0 0 -40)&quot;&gt;&lt;path d=&quot;M8.71 -23.43 C5.81 -19.87, 5.61 -12.32, 0 0 M8.71 -23.43 C7.16 -17.09, 4.13 -10.74, 0 0&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(100.6214131903343 343.61832302594945) rotate(0 0 -40)&quot;&gt;&lt;path d=&quot;M8.75 -56.58 C5.98 -63.22, 5.78 -65.86, 0 -80 M8.75 -56.58 C7.28 -62.52, 4.24 -68.46, 0 -80&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(100.6214131903343 343.61832302594945) rotate(0 0 -40)&quot;&gt;&lt;path d=&quot;M-8.35 -56.44 C-7.39 -63.25, -3.88 -65.93, 0 -80 M-8.35 -56.44 C-5.33 -62.34, -3.89 -68.32, 0 -80&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(27.105333794356 281.61832302594945) rotate(0 24.75 22.5)&quot;&gt;&lt;text x=&quot;24.750000000000004&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1971c2&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;fixed&lt;/text&gt;&lt;text x=&quot;24.750000000000004&quot; y=&quot;38.358000000000004&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1971c2&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;cost&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(300.6214131903343 127.63991307448256) rotate(0 40 -28.950858502137372)&quot;&gt;&lt;path d=&quot;M0 0 C22.29 -1.45, 45.51 -2.33, 80 0 M0 0 C19.6 -0.36, 40.38 0.52, 80 0 M80 0 C81.57 -17.27, 77.92 -37.57, 80 -57.9 M80 0 C79.15 -10.81, 79.7 -23.02, 80 -57.9&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(312.4340932824404 137.26728781290134) rotate(0 17.599999999999994 10)&quot;&gt;&lt;text x=&quot;17.6&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1 GB&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(393.3322101025264 82.20763002266088) rotate(0 44.464932492242355 20)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;time to&lt;/text&gt;&lt;text x=&quot;0&quot; y=&quot;34.096000000000004&quot; font-family=&quot;Excalifont-172z9af, Xiaolai, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;read 1 GB&lt;/text&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;Linear relationship between model size and time per output token (TPOT). Here, the fixed cost sets where the line starts, and the time to read each GB sets how steep it is.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The slope of this line captures the effective memory bandwidth. Although the peak bandwidth of my laptop is 200 GB/s, we can’t expect to use all of it. This is partly because memory bandwidth is shared by the CPU, GPU, and laptop display. Thus, LLM inference gets whatever bandwidth is left over, which we can define as the effective bandwidth.&lt;/p&gt;
&lt;p&gt;In addition, the vertical offset of this line measures system overhead, which is treated as a fixed cost regardless of model size. I’d guess this fixed cost might include things like the time it takes for the GPU to start doing work, among other things.&lt;/p&gt;
&lt;h2 id=&quot;tpot-vs-model-size&quot;&gt;TPOT vs Model Size&lt;a href=&quot;#tpot-vs-model-size&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Using models in the Qwen2.5, Qwen3, and Gemma 3 families, I measured TPOT vs model size on my MacBook Pro. I ran each of these in both &lt;code&gt;bfloat16&lt;/code&gt; and &lt;code&gt;float32&lt;/code&gt; precisions to have more data points in the plot.&lt;/p&gt;
&lt;figure class=&quot;scatter-fit-chart&quot; style=&quot;margin-block: var(--figure-vertical-spacing)&quot; data-astro-cid-qkkemhej&gt;&lt;div class=&quot;figure-card scatter-fit-chart__card&quot; data-astro-cid-qkkemhej&gt;&lt;p class=&quot;scatter-fit-chart__title&quot; id=&quot;scatter-fit-chart-8f63fc2f-title&quot; data-astro-cid-qkkemhej&gt;Time per Output Token vs Model Size&lt;/p&gt;&lt;ul class=&quot;scatter-fit-chart__legend&quot; aria-hidden=&quot;true&quot; data-astro-cid-qkkemhej&gt;&lt;li data-astro-cid-qkkemhej&gt;&lt;svg class=&quot;scatter-fit-chart__key&quot; viewBox=&quot;0 0 24 12&quot; width=&quot;24&quot; height=&quot;12&quot; data-astro-cid-qkkemhej&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M7.5,6 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;/svg&gt;Measured&lt;/li&gt;&lt;li data-astro-cid-qkkemhej&gt;&lt;svg class=&quot;scatter-fit-chart__key&quot; viewBox=&quot;0 0 24 12&quot; width=&quot;24&quot; height=&quot;12&quot; data-astro-cid-qkkemhej&gt;&lt;line class=&quot;scatter-fit-chart__fit&quot; x1=&quot;0&quot; x2=&quot;24&quot; y1=&quot;6&quot; y2=&quot;6&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;/svg&gt;Line fit&lt;/li&gt;&lt;li data-astro-cid-qkkemhej&gt;&lt;svg class=&quot;scatter-fit-chart__key&quot; viewBox=&quot;0 0 24 12&quot; width=&quot;24&quot; height=&quot;12&quot; data-astro-cid-qkkemhej&gt;&lt;line class=&quot;scatter-fit-chart__reference&quot; x1=&quot;0&quot; x2=&quot;24&quot; y1=&quot;6&quot; y2=&quot;6&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;/svg&gt;Ceiling at the 200 GB/s spec&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;scatter-fit-chart__plot&quot; data-astro-cid-qkkemhej&gt;&lt;svg class=&quot;scatter-fit-chart__svg&quot; viewBox=&quot;0 0 560 340&quot; role=&quot;img&quot; aria-labelledby=&quot;scatter-fit-chart-8f63fc2f-title scatter-fit-chart-8f63fc2f-desc&quot; data-astro-cid-qkkemhej&gt;&lt;desc id=&quot;scatter-fit-chart-8f63fc2f-desc&quot; data-astro-cid-qkkemhej&gt;Line fit: 163 GB/s. Ceiling at the 200 GB/s spec: 200 GB/s. Gemma 3 270M, bfloat16: 0.54 GB, 7.1 ms. Gemma 3 270M, float32: 1.07 GB, 9.2 ms. Qwen2.5 0.5B, bfloat16: 0.99 GB, 9.0 ms. Qwen2.5 0.5B, float32: 1.98 GB, 14.9 ms. Gemma 3 1B, bfloat16: 2.00 GB, 17.6 ms. Gemma 3 1B, float32: 4.00 GB, 28.8 ms. Qwen2.5 1.5B, bfloat16: 3.09 GB, 22.1 ms. Qwen2.5 1.5B, float32: 6.17 GB, 41.1 ms. Qwen3 0.6B, bfloat16: 1.19 GB, 12.9 ms. Qwen3 0.6B, float32: 2.38 GB, 19.8 ms. Qwen3 1.7B, bfloat16: 3.44 GB, 26.2 ms. Qwen3 1.7B, float32: 6.88 GB, 46.1 ms.&lt;/desc&gt;&lt;g data-astro-cid-qkkemhej&gt;&lt;line class=&quot;scatter-fit-chart__grid&quot; x1=&quot;66&quot; x2=&quot;456&quot; y1=&quot;284&quot; y2=&quot;284&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;56&quot; y=&quot;284&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-qkkemhej&gt;0&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-qkkemhej&gt;&lt;line class=&quot;scatter-fit-chart__grid&quot; x1=&quot;66&quot; x2=&quot;456&quot; y1=&quot;196&quot; y2=&quot;196&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;56&quot; y=&quot;196&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-qkkemhej&gt;20&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-qkkemhej&gt;&lt;line class=&quot;scatter-fit-chart__grid&quot; x1=&quot;66&quot; x2=&quot;456&quot; y1=&quot;108&quot; y2=&quot;108&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;56&quot; y=&quot;108&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-qkkemhej&gt;40&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-qkkemhej&gt;&lt;line class=&quot;scatter-fit-chart__grid&quot; x1=&quot;66&quot; x2=&quot;456&quot; y1=&quot;20&quot; y2=&quot;20&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;56&quot; y=&quot;20&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-qkkemhej&gt;60&lt;/text&gt;&lt;/g&gt;&lt;text class=&quot;scatter-fit-chart__axis-title&quot; transform=&quot;translate(16 152) rotate(-90)&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;TPOT (ms)&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;66&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;0&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;163.5&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;2&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;261&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;4&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;358.5&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;6&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__tick&quot; x=&quot;456&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;8&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__axis-title&quot; x=&quot;261&quot; y=&quot;332&quot; text-anchor=&quot;middle&quot; data-astro-cid-qkkemhej&gt;Model weights (GB)&lt;/text&gt;&lt;line class=&quot;scatter-fit-chart__fit&quot; x1=&quot;66&quot; y1=&quot;266.2555301263173&quot; x2=&quot;456&quot; y2=&quot;50.894106827871866&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;line class=&quot;scatter-fit-chart__reference&quot; x1=&quot;66&quot; y1=&quot;284&quot; x2=&quot;456&quot; y2=&quot;108&quot; data-astro-cid-qkkemhej&gt;&lt;/line&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M87.63,252.804 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M113.76,243.652 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M109.66499999999999,244.444 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M157.82999999999998,218.48399999999998 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M159,206.51600000000002 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M256.5,157.236 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M211.99125,186.672 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M362.53125,103.292 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M119.61,227.152 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M177.72,196.836 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M229.24875,168.72 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;path class=&quot;scatter-fit-chart__dot&quot; d=&quot;M396.9975,81.16 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-qkkemhej&gt;&lt;/path&gt;&lt;text class=&quot;scatter-fit-chart__label&quot; x=&quot;466&quot; y=&quot;50.894106827871866&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-qkkemhej&gt;163 GB/s&lt;/text&gt;&lt;text class=&quot;scatter-fit-chart__label&quot; x=&quot;466&quot; y=&quot;108&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-qkkemhej&gt;200 GB/s&lt;/text&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;92.13&quot; cy=&quot;252.804&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;92.13&quot; data-y=&quot;252.804&quot; data-title=&quot;Gemma 3 270M, bfloat16&quot; data-rows=&quot;[&amp;quot;0.54 GB&amp;quot;,&amp;quot;7.1 ms&amp;quot;]&quot; aria-label=&quot;Gemma 3 270M, bfloat16, 0.54 GB, 7.1 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;118.26&quot; cy=&quot;243.652&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;118.26&quot; data-y=&quot;243.652&quot; data-title=&quot;Gemma 3 270M, float32&quot; data-rows=&quot;[&amp;quot;1.07 GB&amp;quot;,&amp;quot;9.2 ms&amp;quot;]&quot; aria-label=&quot;Gemma 3 270M, float32, 1.07 GB, 9.2 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;114.16499999999999&quot; cy=&quot;244.444&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;114.16499999999999&quot; data-y=&quot;244.444&quot; data-title=&quot;Qwen2.5 0.5B, bfloat16&quot; data-rows=&quot;[&amp;quot;0.99 GB&amp;quot;,&amp;quot;9.0 ms&amp;quot;]&quot; aria-label=&quot;Qwen2.5 0.5B, bfloat16, 0.99 GB, 9.0 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;162.32999999999998&quot; cy=&quot;218.48399999999998&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;162.32999999999998&quot; data-y=&quot;218.48399999999998&quot; data-title=&quot;Qwen2.5 0.5B, float32&quot; data-rows=&quot;[&amp;quot;1.98 GB&amp;quot;,&amp;quot;14.9 ms&amp;quot;]&quot; aria-label=&quot;Qwen2.5 0.5B, float32, 1.98 GB, 14.9 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;163.5&quot; cy=&quot;206.51600000000002&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;163.5&quot; data-y=&quot;206.51600000000002&quot; data-title=&quot;Gemma 3 1B, bfloat16&quot; data-rows=&quot;[&amp;quot;2.00 GB&amp;quot;,&amp;quot;17.6 ms&amp;quot;]&quot; aria-label=&quot;Gemma 3 1B, bfloat16, 2.00 GB, 17.6 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;261&quot; cy=&quot;157.236&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;261&quot; data-y=&quot;157.236&quot; data-title=&quot;Gemma 3 1B, float32&quot; data-rows=&quot;[&amp;quot;4.00 GB&amp;quot;,&amp;quot;28.8 ms&amp;quot;]&quot; aria-label=&quot;Gemma 3 1B, float32, 4.00 GB, 28.8 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;216.49125&quot; cy=&quot;186.672&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;216.49125&quot; data-y=&quot;186.672&quot; data-title=&quot;Qwen2.5 1.5B, bfloat16&quot; data-rows=&quot;[&amp;quot;3.09 GB&amp;quot;,&amp;quot;22.1 ms&amp;quot;]&quot; aria-label=&quot;Qwen2.5 1.5B, bfloat16, 3.09 GB, 22.1 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;367.03125&quot; cy=&quot;103.292&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;367.03125&quot; data-y=&quot;103.292&quot; data-title=&quot;Qwen2.5 1.5B, float32&quot; data-rows=&quot;[&amp;quot;6.17 GB&amp;quot;,&amp;quot;41.1 ms&amp;quot;]&quot; aria-label=&quot;Qwen2.5 1.5B, float32, 6.17 GB, 41.1 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;124.11&quot; cy=&quot;227.152&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;124.11&quot; data-y=&quot;227.152&quot; data-title=&quot;Qwen3 0.6B, bfloat16&quot; data-rows=&quot;[&amp;quot;1.19 GB&amp;quot;,&amp;quot;12.9 ms&amp;quot;]&quot; aria-label=&quot;Qwen3 0.6B, bfloat16, 1.19 GB, 12.9 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;182.22&quot; cy=&quot;196.836&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;182.22&quot; data-y=&quot;196.836&quot; data-title=&quot;Qwen3 0.6B, float32&quot; data-rows=&quot;[&amp;quot;2.38 GB&amp;quot;,&amp;quot;19.8 ms&amp;quot;]&quot; aria-label=&quot;Qwen3 0.6B, float32, 2.38 GB, 19.8 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;233.74875&quot; cy=&quot;168.72&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;233.74875&quot; data-y=&quot;168.72&quot; data-title=&quot;Qwen3 1.7B, bfloat16&quot; data-rows=&quot;[&amp;quot;3.44 GB&amp;quot;,&amp;quot;26.2 ms&amp;quot;]&quot; aria-label=&quot;Qwen3 1.7B, bfloat16, 3.44 GB, 26.2 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;circle class=&quot;scatter-fit-chart__hit&quot; cx=&quot;401.4975&quot; cy=&quot;81.16&quot; r=&quot;12&quot; tabindex=&quot;0&quot; data-x=&quot;401.4975&quot; data-y=&quot;81.16&quot; data-title=&quot;Qwen3 1.7B, float32&quot; data-rows=&quot;[&amp;quot;6.88 GB&amp;quot;,&amp;quot;46.1 ms&amp;quot;]&quot; aria-label=&quot;Qwen3 1.7B, float32, 6.88 GB, 46.1 ms&quot; data-astro-cid-qkkemhej&gt;&lt;/circle&gt;&lt;/svg&gt;&lt;div class=&quot;scatter-fit-chart__tooltip&quot; hidden data-astro-cid-qkkemhej&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-qkkemhej&gt;I used six models in this measurement, which include: Gemma 3 (270M, 1B), Qwen2.5 (0.5B, 1.5B), and Qwen3 (0.6B, 1.7B), each in bfloat16 and float32 for 12 data points.&lt;/figcaption&gt;&lt;/figure&gt;&lt;script type=&quot;module&quot; src=&quot;https://bryananthonio.com/opt/buildhome/repo/src/components/ScatterFitChart.astro?astro&amp;type=script&amp;index=0&amp;lang.ts&quot;&gt;&lt;/script&gt;
&lt;p&gt;The dashed line here represents the best-case scenario where there’s no fixed cost and the system uses the 200 GB/s peak bandwidth.&lt;/p&gt;
&lt;p&gt;I was able to measure an effective bandwidth of 163 GB/s and a fixed cost of 4.03 ms. I redid this linear fit without the result from the largest LLM, Qwen3 1.7B in float32 (6.88 GB), to check how well it captures the trend. In this case, the line predicted the TPOT for it within 0.2% (46.2 ms vs. 46.1 ms measured).&lt;/p&gt;
&lt;p&gt;In general, all points are within 1.6 ms of the line. The models that deviated the most from the trend were Qwen3 0.6B (bfloat16) and Gemma 3 270M (float32), which exhibited errors of 12.3% and 15.6%, respectively. The model exhibiting the most agreement was Qwen3 1.7B (float32), which had an error of 0.1%.&lt;/p&gt;
&lt;p&gt;As you can see in the table below, the general trend is that as the model size increases, the TPOT also increases as you’d expect.&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Weights (GB)&lt;/th&gt;
&lt;th&gt;Measured TPOT (ms)&lt;/th&gt;
&lt;th&gt;Predicted TPOT (ms)&lt;/th&gt;
&lt;th&gt;Fraction of time spent reading weights&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Gemma 3 270M, bfloat16&lt;/td&gt;
&lt;td&gt;0.54&lt;/td&gt;
&lt;td&gt;7.09&lt;/td&gt;
&lt;td&gt;7.31&lt;/td&gt;
&lt;td&gt;45%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen2.5 0.5B, bfloat16&lt;/td&gt;
&lt;td&gt;0.99&lt;/td&gt;
&lt;td&gt;8.99&lt;/td&gt;
&lt;td&gt;10.08&lt;/td&gt;
&lt;td&gt;60%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gemma 3 270M, float32&lt;/td&gt;
&lt;td&gt;1.07&lt;/td&gt;
&lt;td&gt;9.17&lt;/td&gt;
&lt;td&gt;10.59&lt;/td&gt;
&lt;td&gt;62%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen3 0.6B, bfloat16&lt;/td&gt;
&lt;td&gt;1.19&lt;/td&gt;
&lt;td&gt;12.92&lt;/td&gt;
&lt;td&gt;11.33&lt;/td&gt;
&lt;td&gt;64%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen2.5 0.5B, float32&lt;/td&gt;
&lt;td&gt;1.98&lt;/td&gt;
&lt;td&gt;14.89&lt;/td&gt;
&lt;td&gt;16.12&lt;/td&gt;
&lt;td&gt;75%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gemma 3 1B, bfloat16&lt;/td&gt;
&lt;td&gt;2.00&lt;/td&gt;
&lt;td&gt;17.61&lt;/td&gt;
&lt;td&gt;16.27&lt;/td&gt;
&lt;td&gt;75%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen3 0.6B, float32&lt;/td&gt;
&lt;td&gt;2.38&lt;/td&gt;
&lt;td&gt;19.81&lt;/td&gt;
&lt;td&gt;18.62&lt;/td&gt;
&lt;td&gt;78%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen2.5 1.5B, bfloat16&lt;/td&gt;
&lt;td&gt;3.09&lt;/td&gt;
&lt;td&gt;22.12&lt;/td&gt;
&lt;td&gt;22.92&lt;/td&gt;
&lt;td&gt;82%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen3 1.7B, bfloat16&lt;/td&gt;
&lt;td&gt;3.44&lt;/td&gt;
&lt;td&gt;26.20&lt;/td&gt;
&lt;td&gt;25.09&lt;/td&gt;
&lt;td&gt;84%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gemma 3 1B, float32&lt;/td&gt;
&lt;td&gt;4.00&lt;/td&gt;
&lt;td&gt;28.81&lt;/td&gt;
&lt;td&gt;28.51&lt;/td&gt;
&lt;td&gt;86%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen2.5 1.5B, float32&lt;/td&gt;
&lt;td&gt;6.17&lt;/td&gt;
&lt;td&gt;41.07&lt;/td&gt;
&lt;td&gt;41.81&lt;/td&gt;
&lt;td&gt;90%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Qwen3 1.7B, float32&lt;/td&gt;
&lt;td&gt;6.88&lt;/td&gt;
&lt;td&gt;46.10&lt;/td&gt;
&lt;td&gt;46.14&lt;/td&gt;
&lt;td&gt;91%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;For each model, I calculated the fraction of time spent using two steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Estimate the time spent reading weights by taking the model size and dividing that by the effective bandwidth (163 GB/s).&lt;/li&gt;
&lt;li&gt;Divide this read time by the predicted TPOT.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can see that system overhead takes up a larger fraction of the time needed to generate a new token for smaller models. For the smallest model my laptop spends roughly 45% of time reading weights, whereas for the largest model it spends 91%.&lt;/p&gt;
&lt;h2 id=&quot;how-i-measured-this&quot;&gt;How I Measured This&lt;a href=&quot;#how-i-measured-this&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For each model, I measured TPOT using a similar &lt;a href=&quot;https://bryananthonio.com/blog/one-reason-input-tokens-are-cheaper-than-output-tokens/#measuring-the-prefill-and-decoding-phases&quot;&gt;&lt;code&gt;ttft_tpot&lt;/code&gt; function from my previous post&lt;/a&gt; with the &lt;a href=&quot;https://docs.pytorch.org/docs/stable/notes/mps.html&quot;&gt;MPS backend in PyTorch&lt;/a&gt;, which allows PyTorch to run code on my MacBook’s integrated GPU.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; statistics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; torch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(model, ids, n):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    torch.mps.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;synchronize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# wait for any earlier GPU work to finish&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    t0 &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;perf_counter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    model.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; min_new_tokens&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; max_new_tokens&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; do_sample&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    torch.mps.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;synchronize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# wait for this call&amp;#39;s GPU work to finish&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;perf_counter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; t0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ttft_tpot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(model, ids, num_samples&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# warm-up&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    ttft &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; statistics.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;median&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;num_samples&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; statistics.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;median&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 101&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;num_samples&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ttft, (total &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ttft) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then I looped over the models:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; gc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; transformers &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; AutoModelForCausalLM, AutoTokenizer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;models &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;google/gemma-3-270m-it&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.bfloat16),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;google/gemma-3-270m-it&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.float32),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen2.5-0.5B-Instruct&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.bfloat16),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen2.5-0.5B-Instruct&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.float32),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;google/gemma-3-1b-it&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.bfloat16),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;google/gemma-3-1b-it&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.float32),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen2.5-1.5B-Instruct&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.bfloat16),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen2.5-1.5B-Instruct&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.float32),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen3-0.6B&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.bfloat16),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen3-0.6B&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.float32),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen3-1.7B&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.bfloat16),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Qwen/Qwen3-1.7B&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, torch.float32),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;input.txt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;rows &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; []  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# (size in GB, TPOT in seconds)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; name, dtype &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; models:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    model &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; AutoModelForCausalLM.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_pretrained&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;mps&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;eval&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    tok &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; AutoTokenizer.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_pretrained&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    input_ids &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tok&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;20_000&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; return_tensors&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).input_ids.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;mps&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    size_gb &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;numel&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; p&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;element_size&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; p &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; model&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;parameters&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1e9&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    _, tpot &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ttft_tpot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; input_ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    rows.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;size_gb&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tpot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    del&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    gc.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;collect&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    torch.mps.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;empty_cache&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For each model, I used the first 128 tokens of the &lt;a href=&quot;https://github.com/karpathy/char-rnn/blob/master/data/tinyshakespeare/input.txt&quot;&gt;Tiny Shakespeare text&lt;/a&gt; as the input prompt.&lt;/p&gt;
&lt;p&gt;After gathering the data, doing the linear fit is as simple as:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;gb &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [r[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; r &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; rows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;ms &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [r[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; r &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; rows]  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# seconds -&amp;gt; ms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;slope, intercept &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;polyfit&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;gb&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ms&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# ms per GB, ms&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;caveats-of-this-measurement&quot;&gt;Caveats of This Measurement&lt;a href=&quot;#caveats-of-this-measurement&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This was just a quick experiment to get a feel for the numbers, and not a rigorous benchmark. With that being said, there are a few flaws to my approach I should call out.&lt;/p&gt;
&lt;p&gt;One peculiar aspect of the data is that a linear fit on just the Gemma 3 270M data points (bfloat16 and float32) implies a memory bandwidth of about 258 GB/s, which is far above the spec limit of 200 GB/s. I was able to reproduce similar results across multiple runs, which suggests to me that this isn’t due to noise.&lt;/p&gt;
&lt;p&gt;I believe the simple conclusion is that fixed cost isn’t always the same for each model. This is true even when considering the same model in either bfloat16 or float32. I don’t yet fully know what the underlying cause is but I suspect it may have something to do with the model architecture.&lt;/p&gt;
&lt;p&gt;Another caveat is that I didn’t run this using an optimized inference server, such as &lt;a href=&quot;https://vllm.ai&quot;&gt;vLLM&lt;/a&gt; or &lt;a href=&quot;https://github.com/ggml-org/llama.cpp&quot;&gt;llama.cpp&lt;/a&gt;. I’d expect a more optimized setup would lower the fixed cost measured here but might not improve the bandwidth utilization as much given that there’s probably not much headroom to do better. It might be interesting to try this experiment again with &lt;a href=&quot;https://github.com/ml-explore/mlx-lm&quot;&gt;&lt;code&gt;mlx-lm&lt;/code&gt;, which offers optimized kernels for Apple silicon&lt;/a&gt;, instead of PyTorch.&lt;/p&gt;
&lt;h2 id=&quot;what-i-took-away-from-this&quot;&gt;What I Took Away From This&lt;a href=&quot;#what-i-took-away-from-this&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Now, let’s get back to my original question: for Qwen2.5-0.5B why is TPOT ~9 ms? The linear fit predicts that for this model my laptop takes roughly 6.05 ms to read the weights plus a fixed cost of 4.03 ms, giving us a total of 10.08 ms. This is 12% higher than the 8.99 ms figure I measured but it does paint a picture of how time is spent generating each token.&lt;/p&gt;
&lt;p&gt;I’ll admit I was surprised to see how well the data fit the expected linear trend. But now that I understand the mechanism behind this it all makes sense.&lt;/p&gt;</content:encoded></item><item><title>Black October Made Me Switch From Ubuntu to Debian</title><link>https://bryananthonio.com/blog/black-october-made-me-switch-from-ubuntu-to-debian/</link><guid isPermaLink="true">https://bryananthonio.com/blog/black-october-made-me-switch-from-ubuntu-to-debian/</guid><description>Debian patched a critical kernel CVE in a day but Ubuntu still hasn&apos;t.</description><pubDate>Tue, 03 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://news.ycombinator.com/item?id=50337608&quot;&gt;Yesterday, I was alarmed to learn&lt;/a&gt; that LTS versions of Ubuntu are still lacking a critical security update to patch a &lt;a href=&quot;https://www.cve.org/CVERecord?id=CVE-2026-98432&quot;&gt;severe vulnerability that was first disclosed on October 15th.&lt;/a&gt; As of now, &lt;a href=&quot;https://ubuntu.com/security/CVE-2026-98432&quot;&gt;all LTS versions&lt;/a&gt; dating back to version 16.04 have this vulnerability.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--bordered&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/ubuntu-lts-vuln_1jtHv5.webp&quot; alt=&quot;Ubuntu&apos;s CVE status table for the linux package.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1882&quot; height=&quot;754&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;Ubuntu&amp;#39;s security tracker indicates every release from 16.04 LTS to 26.10. is still vulnerable, two weeks after the CVE was disclosed.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Until recently my Hetzner VPS ran Ubuntu. Even though it’s unlikely that I would fall victim to this vulnerability, I find it unacceptable that Canonical are taking this long to ship a security patch. To be fair, &lt;a href=&quot;https://ubuntu.com/security/CVE-2026-98432#mitigation&quot;&gt;Canonical did recommend a mitigation&lt;/a&gt; for this but it only reduces the risk.&lt;/p&gt;
&lt;p&gt;My bigger worry is that we’re starting to see more and more vulnerabilities being disclosed at a pace that’s hard to keep up with. Last month alone was bad enough that it’s now referred to as &lt;a href=&quot;https://en.wikipedia.org/wiki/Black_October_(information_security)&quot;&gt;Black October.&lt;/a&gt; Given that AI will only accelerate this trend, I’m finding that it’s now more critical than ever to keep systems, like my VPS, up-to-date on the latest security patches as quickly as possible.&lt;/p&gt;
&lt;p&gt;So I reconfigured my VPS to run Debian instead, &lt;a href=&quot;https://lists.debian.org/debian-security-announce/2026/msg00482.html&quot;&gt;which already received a security patch the day after the CVE was announced.&lt;/a&gt; While this incident alone may not be proof that Debian will always have faster updates, it’s been enough to tip me over the edge.&lt;/p&gt;</content:encoded></item><item><title>One Reason Input Tokens Are Cheaper than Output Tokens</title><link>https://bryananthonio.com/blog/one-reason-input-tokens-are-cheaper-than-output-tokens/</link><guid isPermaLink="true">https://bryananthonio.com/blog/one-reason-input-tokens-are-cheaper-than-output-tokens/</guid><description>I measured how fast a small model reads a prompt versus how fast it writes a response to understand how tokens are priced.</description><pubDate>Sun, 25 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;LLM APIs typically charge more for output tokens than input tokens. As of October 2026, I’ve found that output tokens for models by &lt;a href=&quot;https://platform.claude.com/docs/en/about-claude/pricing&quot;&gt;Anthropic&lt;/a&gt;, &lt;a href=&quot;https://developers.openai.com/api/docs/pricing&quot;&gt;OpenAI&lt;/a&gt;, and other providers are roughly 5× more expensive than input tokens. Why is that? I recently started learning about LLM inference and this seemed like an interesting question to dig into.&lt;/p&gt;
&lt;p&gt;I timed a small model on my laptop, measuring the time spent processing each input and output token. In this experiment, I found that one output token can take up to 29× more time than a single input token, which gave me insight into how tokens are priced.&lt;/p&gt;
&lt;h2 id=&quot;two-phases-for-generating-text-with-llms&quot;&gt;Two Phases for Generating Text with LLMs&lt;a href=&quot;#two-phases-for-generating-text-with-llms&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;LLM inference requires two stages: prefill and decode. In the prefill phase the user’s whole prompt is processed in one pass. This requires reading the model’s weights just once. If you’re familiar with how the basic &lt;a href=&quot;https://en.wikipedia.org/wiki/Attention_(machine_learning)&quot;&gt;attention mechanism&lt;/a&gt; works, this is the stage where the key and value vectors are generated for the input tokens and stored in the &lt;a href=&quot;https://huggingface.co/blog/not-lain/kv-caching&quot;&gt;KV cache&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;During the decode phase the LLM generates one new token at a time. Unlike the prefill phase, however, all the model’s weights have to be reread each time the model generates a new token.&lt;/p&gt;
&lt;p&gt;Two metrics characterize these phases, which include the time to first token (TTFT) and the time per output token (TPOT). From a user’s perspective, the TTFT measures how long it takes before a response starts streaming into the chat window, whereas the TPOT measures how fast the streamed text is generated.&lt;/p&gt;
&lt;figure class=&quot;figure&quot; style=&quot;margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;svg version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 827.316009477159 444.0893128169569&quot; class=&quot;diagram&quot; role=&quot;img&quot; aria-label=&quot;Diagram of LLM inference over time. The eight prompt tokens &apos;To be, or not to be,&apos; go through a single prefill pass, which produces the first output token &apos;that&apos;. Four decode steps follow, each producing one token: &apos;is&apos;, &apos;the&apos;, &apos;question&apos;, &apos;:&apos;. A bracket under the prefill pass marks the time to first token (TTFT), and a bracket under one decode step marks the time per output token (TPOT).&quot;&gt;&lt;!-- svg-source:excalidraw --&gt;&lt;defs&gt;&lt;style class=&quot;style-fonts&quot;&gt;
      @font-face { font-family: Excalifont-1j2olal; src: url(data:font/woff2;base64,d09GMgABAAAAABVAAA4AAAAAJIgAABToAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhwbhn4cgW4GYACBHBEICrV8qAALSgABNgIkA4EQBCAFgxgHIBtNHKOihrJassj+4oAnwxsdIFRViSTi8FqLvmpjqjH7JEgYCeG833O4w3mEJLPD83PrvZ/bX7BKckT0NlZE9xgWYKJiFGFUAXrqJdZxkdqYGFUQT3+xfe+O4NPhCd0EkgVJ0zRNNyUWemgJ1Y867e3Vus9PUsKFYI+ALPtgysdlbdZ0tR1qcmQqcHJAKFkfpt//AOHf99XZp++2dD3ijxPQRBPS0Tbaq7xKVqBlJ2n5A4PjXK2pzRXIAdqAj9AtS/RkTIB2LkQbKigm9SUQpjUmwpQQFKCrEjWyRtXKGqdr3sgWDertJPPy0a+VSXiOuJFFnoWzuQmAAGACAAR8NDYgBwReFQAUH2rYdkjIALJLm6UByG6Wynog20vHNQEZDACAPao8rpYmQJPSFAAI6VaEPIDNhuSJx8FFTuVh40KFsX9ccqv3Za0X1hMGITsKRGTRrJXjSmNrHbR9JT0Yvw8q613L2n4HQRRm5qnyUsgSj50KtCaIvWA1EBcNHYWBiYWNU4F32VRESkZOkbsQZed2xhuEAEqQA0UBwgqHCAdIQhQmjWjYmOgAuAtwgLxTxMmOY8gAMkJBh1JjYABYNwx0ouXMAAG5oFPUUNLzUAsNRh4T2w7IBdqDHW0QJgMgRFFpgCMpDVDh3c5kmAFhggVC9rKSl1Y/CdKytsh+ry4FgpelogmwAbpoURXTLohLrKpAWCkz5sE2AXzLKCxShyIoDzcdEKEqgGbiR3lOFJwWISjSRlQSO8RqII6ARJpkTNkOhJeNAQeRhkKsGemOpcjOb44yfuMenvn5BOmnA9CMuqMxCQyxHU7ohB/AS5VMGMCGxKFvOdm2aKmBAAgShZtAzFxqnc+AHdiPKw++QoTRMUiRoUqt8WMZI6GrixNJZpeqUf9duRGXnPOPv51y0g+O2muP3fsCggqmnx0AVYOYWEF+tK+FeuZTGVRwFohCeoIrLyjddUZOKNp8RHBVsavLqzfTDMD+zMl0eevg6fVQoBt3fF+G8/sTqRP5E/vjmZEfVv+0cc/l4+uyGsniGPnpcMV/Sv5Fw84szP8SkdcExMmCef8Z7MAGZ3PNJ80JewIPOykMGTiTaknW65TvfUqOpeGMKCIKjjxIq5qUQYAefhFibS70Cl9hWU+V3TblK+0rlNt1wq027SOFyW7ggKdsSs/usFYBAo4aqTIr/8uWXP46Zx4mx5RlkU+FehpI5ampeBsbMb1ZMhUofIqG65Lqh8QZ0Y1icZxvgRXseczD6kHMY5ospGg6Z/VJu/xVKMF0WLJGvptRjuiWM0lk9z5MvmfBxCVh2zgRuGzyJxolkYCvac3p6LLOYnv7vZeN7+XL3iCFgOOKCC/pSdrazkilcRywYf4NMbMqWs7BLbTPw/uKAZquET3bitRSTWb0FJ0mUTSbiJHCUx7O/2f5ynwAGyH7WbfvED5wvuHMAVOa/IiyOt+NHu5yhk84FLGc5TiMCZhkk0SrWp040D3+WG7+blJwC6mA5lEDMPo/h9OxpyFMKiKt8FkbPZabC6TySQeQg+E5uA1+AXjaa+bwN4gMZxbydDmTeRL5r4KJGWVR6Jbr1lC3glu0f+/Ts2QxNIq2PdJgB4aKcW/HXozX3sA0GD6YKHfZM7Uw6zLZNlLSS0aPknXVeUIJylnOqGk3YLDlCc892PQhh0flmrHGSrKsEry4nMliSuUFlKuJ0Ngnj6IC+BbEZT6Pl8cq+dFoSWRE5Y1qQhO3YcOCJxu+JXWyOFyYfCugyvOKSpokX5SpaWG0IK3loKf5ALstLcAoFBJpD0vZqzwz0Yh2WthDldblo+1MXyPFyLNhysPESOAhJqPZpANnR6ZbhuWGKn7f/T4rrxDDc+b99IrOym7lg+EMl91uy2Gu12niz1nhMcv6mz8jdfShSIXzWUzgzBE/LaQanEALoz1q48/HH7+DXu0mcftGZv3Z7tdyY3Znohw2YwGA65Aa0TfhwecVYryJw6OJKG7bE4rY8oDBiCZRlCxJyR6K+EzWzNZvtK/QWKpxke7gcHBjVA94zJs73JwROATUoPDKeJOzRz4YlQdI0cJEX3MHrNmS+wtzJsAfxWCTHzAaHNvbUJri8MPj+4LDWdx7sWE332QNZyaIbG9sDGWPkvGxvbXbYWmWOTGHxKnpcWSjqTz36VbXrJ/vb+dbdLOPyW5i0WxAUNCTJN0ALnnEdTs41Rf5r2UKc6hBRB5pkPNmvYqIC6v8nsAoSriCeR9yGGxBDVZpVbaqdVqX16NEOxre5dTVZtuoOXeuCzFQmBlS5ZdiME+JFF3gMvpvSdpNxvfyVLexT0w7aRx23JEe0VFEomS8keKi07yGxXhDIfA/l0W+Vw+Q0s3AdKcu/ACy3h/JGRUz3NU7m5yhJ4nUfOSxSu5BYpTldlvlrjpRi2SC89laIUQ8VDaJFfDT6rauq22ZbcmhGKmysPGmrTQ4V0LR6cQJkD1I6JZa+tnFBJ5rDL7P9hPP88lKry+T4+LD8l0WdDolF12SbtXrOtfVSMNQc2ByB9hcFhifaLXka7A6VpTlEzeku2Sb1OD80fFYUDittiWVCa3KKazioSLGFZ6bozw1Rfub2Zux47myKTpKCOiyTzfvzma2nSp9bj5HSuzN11gde+5LU0mZ1S1e3LPTAAxgCE9i2OgoNIOTi2g5jkOcIaOqLO/6b8ej725z9lev8EMPol3+KFcsC2Ody2p1YfReHniih1ufbkz+MKuiqpSZ0mf9gwYj34za0r3tBVLeG5xYVgjGWVvVr/Q0mjZ34F96LFdznc44Lc3qX1h45/L97a8Pf97aVQR0NPCJ3yvtnwachkacmlbDyu45DxNbXew1GRfZw2Qrgm3X2/TjTur3vVdIFblN0oik6CQtUtt+XZrKG8sqZ1ctwXOr57qADfxUlHNF6B+ibY8qj0b0YjfphuNMRAxAj1QKe9ZX6Ilbh2fUqY3dOAm8J3gVbDzeT3XA9DjAhsmSW0esJK/IABpfQ5xnOz53FQbh+yPYq4DvP31Eiay1+ljTpPOQRNdbUmW1pId5inIfBXSuyWmlsZI8G6nlhblyhWnOCrHt4W5KQGVUpKsl/CttJfA2fNHgqyqX94Nidgs9RpJ36m/Anus7lOmsD4pJ9DOyMdqBINQfFHo0Ps5IHSzTLUx9D3djwRb0bxLf6vUuC3sCqeLlYX75Q7TOmfITUjPo607q4KE+Oc39FppKJmS+Kfyhe5gwpoeoAneJvq5K+tgf+Q72BJ6yFQ9DA5Q4OqIffbv+3ChaUETl07uFtR7QqVraE/dhu6rQAPu4dWqtmG1kntpeuZkzBZ9x/ukCjfTUmAhe3SoW5e78hnO1GqQ5o/krScPay/dMy99lUvHcrugf05JWaBwduUWdkWikwJSlG1fccCxKv4CuZZXotr09yDmR8Bdn+elZ85gzQPMw+Zbg06BxPlStrxvdKd9EarlrbLJasub/pPsCuJwbx6SAaKqFzSP+hUpdg125dDk9R+m9tz+jGhi+vLOnoS4R0oKwmQm4rwJj8J1DbFxYp1ojhaiLwMzht26GBcE1s9SAAyRc/mOog0PWSJYzg3CvXqTzcp/ZShu/8cjHGVEe6QPp5Z8q+nAq0KeXvEamYskM9HRsGwZ0EqVOG5whRGLGJ2feBAZ+XMHWBflNxgrb8BlU/8WXW/jmTYr7CnTcVsf9eNRVp1xvHy0StkWoyfQTX/29U6NKLokl/+VN0hsEkNy028c+7X7+9KFFSILfxPl92PrnfK9874ekuaBCdv4Ut3czvIpUsarIeBe7+qXylT+BDlrnSmnhk+nhHNajctId6tsQ9mxrv4NuCXLrHVZOh+h76XCG8dJ0tQjE2r2kHeuqODeAHExpSRscJHDu+E951ZQBocS0E0b6KQgcEsQpzgOh1+SMLR9ibOc6seemZhrSDA7qY8WXjTfPXC45fUtjU8FdaKWaaHjasYGqivLjGTJJPNU3Lru8K79JQeqCfoSaKJiHafmxTXFO9PQfwXjO5gQBUcg8/jmO+GWbXdyENNL2zKCVZJu4Izv8wqmBozsyUrpY/xjKFyOdq/6KTqygl/BL6Eij0kFHMqRBUt8n90vYLSNddo5oCZe8m0kU/UnK9+0E4bCwr5iT6NzlnqE0lU3qdIOFoul9uSIFEaPzUkMv2jIHaby9qaow0TPNJi6H7aT/p3R0ktj3H7nuf/fspO42mY/ZqR5u35V1MNFDuC460cl7a3Xb9KHXoIpf6Am1VF6cfHHCPSTd5VhbSEigjHU4c20msZZo5JeplrgbioSD8SDb8OH275KKZq1qkb8kIVwMbFlEdHwPAvrn8lq5t9VvT04QzwYMYEiNOMBV9XpvwTSjgQ43AVNWhlBy0C+N8a+ESTgf+92LXeCTouMkw6kt1mOdDkKrCOWnMtcV6eICUo1XzkY3mjm4mTLB0TjMnTDY2dDgKo55k5m5jcnH1bfV+CMyG/xHo+irN6OSafY/cE6DE8RtjcRpUd2l5EyhG3TTH/OosO8xWSWT3NrVIX8V5C8WJ4nt8yWL2MnYZ4C7l+00nFQF7BJpnGNaMrSF3ItwcqUJnUQGxGH+fTmDZqxWBihGhdE1zoteANOnEBly4TOWsO4qdwWleIFbzVFYLURTpqseWJtW23il2IeY561ICPDNzeIMt/82nME1vnm03kOVTP/FcD3zT24eKGANyVJflXOufCIMTUs6RcXOD9TDdlX/RzsOD6Fn3oaeWfaw56VLwA+INnzra7/PiXCgHrcEkJJJhWA7z/f3kf0fidRIkp48sz2JORrnEoWSaBtJ+IgqknicIiTi6Is0+YoAkOLIrFfZJKrUpd7ZCrk6zvpxIClofCZtarIyxp4cDthqPmwnfp/tEblMuAkx0jMq/YFz1ilVVV1UaoLkVflQS2q0E5aai4qE/g3Xf2fvKEuYyZ8xfCXG4mgvuNWVIkpNxrSuOoe8RxPx1rcaSomk3/Ff3CIU/OlXvQn0Uz0f5nS6tgt5MdPbd+QKpznYcLrDBmG9SJo6XxIw2NpnSUE6ts/c/Z6HN/j233xeekq1MR1O8AkLtLCSaKYzv8ZO5r8/uyLKHbzG8hjhRiexdAINp4UopjHIq4ngJaY5nXO3iK1iHvzgWh8QW8NiXmCI59oDFGN5+/sg8Tw0HxGxv7jy8RBtT8lBCMb+wWXTsNfRZKkhz5zmLxLZmQQ2ORsUOW4wmKdvAUuO3/qotKW2d5byuSlpy9Rz/dod2mAoN5s7R/LXVsBSyskUMoeUKR86nf5X2Nh81pNm7w0Rb9c6sEN+uVy/UONzbzuTS1SjhOHDiXEpJ2eCJ8ORJXmxyhP7/p+qZ2tHkAnOpRX2bkchQiOQQt8LHvrhOd/QehcerY1UBqIN0EAt9vlOhSPJRK1jvgPcbAIz+OLh63CKhEVSjN9kCOMfjzaRwgaZhqNGiuXJOkYui4Wo3vbAFlb7MGgHW0tU65HXYSJmT35x4NrPt3Ncj74VBLfyI9NHLx1YGjdEy4XZvyjXODEnnsgYa5qe7zkZKaf7pusmcdclr/NPeNqHXI3OZrQiviAowb94ARhEKCmvqpqnKXDabxLbEzQ7TXBgGt8rqWWT7kebWMYyJZ6I0jkxL2xP1rmzg2HMwHmHrGaHEbXdlrru+gFrYyYvFT0OZY9sa8BVzK3259jPWG2dW+1+jcvK6Li50cP+hn3RjVMUbyvNPrbxLlmi2BADQ7OxGtpneHnYnP6em9Gjozm7MyDV+GKgHqy1mM0jWT9xnY3UDhqoksG0Arpu2ovf+XoW2Tfe2sf0lTn1WVOU/UhawDqMjnTZkXVhFb2rgpb/HCEXxpoMGfsd3zHirTZMYbeD1WUP4g/nM/NYrWjy6STxYcTF8gMsTZ9kg6eGtHPvI53+17MTSD6fYCBkM70LORQqjaKF3AF/Xh7N0n6FcgyL+IwYsGZ3hBkb1AeWOPSxdQK8354p9rdnwS0bxdsW5ncWC3C6bJFJwNxpnDKP4jvOBxYRa2Fo+LbR/yPZaCwqQnRAo+efdwzxe6zc2q9mKP7oC5zguT5RmynTBBrKzsiJxvsFqnQcsUNf+g7qJpiEabPx1BeHxn++eG72++eDmS9zgrVFt0hui93UqUAQ8n++45nvHBkdTgJTVsyJS3Rlf9mVmzGh/BzmUEj10RrRVi9n1Fzl6DvSr/l/2x4wo/S9gdMvLjRYhVDJB2TfKgN8XqcMambpJ9ZONmLjBfuH/r+Wqi7pCE5MabGdCkaYBBmmmfdgWqUkDYnqaLOx3TrQFei/s0cC9YxMFuH1jm/vO8OI2nohKscw77R/y1xO0pU/xlUp4lxC/t5HoljJFradt7tTCBbJMr76wssFMYfTPji2BrScFTp2udpPjFnTaXdN3on4LviFww9U55Rxb9DJPaymcZtDT2Yz0HLWBOo5JVo4soW7erdSbHotFrzHu++fKdm4uexk7mjV9F5RiXP3bzooIDy0fHkOUbaQC24cOlQeijKy42eT/1rxZnG7VMDMfa7IFboaNUs2CXnpWS9svePnOHvEedAMQirFSiiMnbb3ShCjF6QxiK+JC7YwZFWZ/jLG/HA/FEZD1Ca89ZoYZeLdqZYM/2flCvF3t7jS5XAXz20/1iVNw42nLFofvnWHLUu5Bfx0jcnUdjz5FX13JRUmaj3UAxs40t3pSaOtpIrFjdtfdKCT0INuy68VKUT685D/+vmp3TcH38gAlLC/JsuLgq5iq4i3NAb6AAAARtvcdJtJb21+3DE261sH9ggL/W8tJUj+empxnuP39Us/0UE4bdVURp9uAH54pJdLIHDKHqJOZhMa0ExMwOoyrh3D6/ffx5gLVs2p7gLzIdsWcq2UPC997GaB3uBUyNOogK4QUTYIbQ6fSgVXzjlr+daGS13BqvhmO8WiSU8LzvL/N+s4KhcRGuQ/tVkNDUl5UHCmj9AGZ3iQwM88BNtBHspNFw+jUsbDRVEBkU8LAL1JypVqUKtKsybjeEpTqdp4DUpZZFW0aHVFnaq2fHnxmWE4o5/fxqyGhUviD34GLkL8Bg7mKj0KW6a08jrpjJK4ZRKz07IjxBiatTlQG6I1msAlAK6GfnwaBFQs01YZVKxgYZzIa4UaDehsS6etJ1BppBIm5CoVvKCQ1dg3OAAAAA==); }&lt;/style&gt;&lt;/defs&gt;&lt;g transform=&quot;translate(13.149922573000367 10) rotate(0 84.15 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Prompt (8 tokens)&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(10 42.368367950655056) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C17.95 1.77, 24.06 0.63, 31.75 0 M9 0 C14.63 0.44, 17.45 0.67, 31.75 0 M31.75 0 C37.87 1.35, 42.32 4.23, 40.75 9 M31.75 0 C36.73 -2.2, 43.01 4.51, 40.75 9 M40.75 9 C39.7 12.71, 42.1 18.41, 40.75 27 M40.75 9 C40.75 13.72, 40.84 19.98, 40.75 27 M40.75 27 C40.61 31.97, 39.06 37.38, 31.75 36 M40.75 27 C40.59 31.95, 36.93 33.95, 31.75 36 M31.75 36 C21.6 37.41, 13.4 35.16, 9 36 M31.75 36 C24.21 36.62, 17.1 35.01, 9 36 M9 36 C4.26 37.55, -1.52 31.87, 0 27 M9 36 C1.34 33.88, -0.96 32.92, 0 27 M0 27 C-0.02 21.87, 1.34 17.13, 0 9 M0 27 C0.08 21.82, -0.67 15.7, 0 9 M0 9 C-1.38 4.64, 3.02 0.64, 9 0 M0 9 C1.45 4.32, 2.1 2.03, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(20.475 49.118367950655056) rotate(0 9.900000000000002 11.25)&quot;&gt;&lt;text x=&quot;9.9&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;To&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(54.82421984507397 42.20785222747651) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C14.12 -1.94, 21.16 0.51, 31.75 0 M9 0 C15.63 -0.31, 21.27 0.03, 31.75 0 M31.75 0 C35.78 1.85, 40.09 2.64, 40.75 9 M31.75 0 C38.73 1.81, 38.45 1.32, 40.75 9 M40.75 9 C41.21 15.26, 40.88 21.78, 40.75 27 M40.75 9 C41.36 15.36, 40.4 21.92, 40.75 27 M40.75 27 C40.73 33.06, 39.69 35.07, 31.75 36 M40.75 27 C39.51 33.57, 36.35 35.48, 31.75 36 M31.75 36 C24.15 37.55, 20.78 35.27, 9 36 M31.75 36 C26.29 35.83, 18.6 35.74, 9 36 M9 36 C2.26 37.7, 0.69 33.46, 0 27 M9 36 C0.84 33.71, -0.77 31.16, 0 27 M0 27 C-1.57 21.57, 1.5 15.72, 0 9 M0 27 C-0.23 20.38, 0.04 15.47, 0 9 M0 9 C-1.68 4.11, 1.12 0.84, 9 0 M0 9 C-0.54 2.26, 5.01 -2.27, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(65.29921984507396 48.95785222747651) rotate(0 9.899999999999999 11.25)&quot;&gt;&lt;text x=&quot;9.9&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;be&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(99.86781117849188 42.20785222747651) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C16.1 -0.99, 23.36 -0.56, 31.75 0 M9 0 C16.86 0.69, 26.78 0.5, 31.75 0 M31.75 0 C37.23 -0.8, 41.52 4.11, 40.75 9 M31.75 0 C37.94 0.31, 40.86 1.05, 40.75 9 M40.75 9 C39.6 12.86, 40.56 17.68, 40.75 27 M40.75 9 C40.09 12.92, 41.14 16.86, 40.75 27 M40.75 27 C40.22 33.92, 38.14 36.4, 31.75 36 M40.75 27 C39.5 34.85, 37.25 34.55, 31.75 36 M31.75 36 C26.56 37.22, 18.46 35.24, 9 36 M31.75 36 C26.37 36.89, 21.88 36.52, 9 36 M9 36 C3.33 34.13, -1.83 31.76, 0 27 M9 36 C2.3 37.78, 1.92 32.05, 0 27 M0 27 C-1.04 23.3, -1.7 19.28, 0 9 M0 27 C-0.76 21.9, 0.36 18.29, 0 9 M0 9 C-1.12 1.61, 1.43 -0.12, 9 0 M0 9 C1.26 1.82, 1.62 -1.13, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(115.29281117849189 48.95785222747651) rotate(0 4.950000000000003 11.25)&quot;&gt;&lt;text x=&quot;4.95&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;,&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(143.48994660776208 41.89930533736663) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C14.59 2.09, 20.01 -0.55, 31.75 0 M9 0 C13.41 -0.72, 20.48 0.89, 31.75 0 M31.75 0 C39.58 -1.58, 40.74 1.59, 40.75 9 M31.75 0 C39.97 -0.43, 39.16 4, 40.75 9 M40.75 9 C42.29 15.93, 40.99 17.63, 40.75 27 M40.75 9 C39.95 15.67, 40.29 23.72, 40.75 27 M40.75 27 C42.6 33.06, 38.95 35.62, 31.75 36 M40.75 27 C40.46 33.46, 35.52 35.84, 31.75 36 M31.75 36 C23.74 35.82, 16.96 35.76, 9 36 M31.75 36 C24.31 36.21, 18.11 36.15, 9 36 M9 36 C4.5 37.94, -1.35 32.88, 0 27 M9 36 C2.57 35.32, 0.11 31.44, 0 27 M0 27 C0.36 23.35, -1.65 16.66, 0 9 M0 27 C0.38 22.5, -0.52 17.25, 0 9 M0 9 C1.24 2.8, 3.54 1.08, 9 0 M0 9 C-2.29 2.88, 5.18 1.72, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(153.96494660776207 48.64930533736663) rotate(0 9.900000000000006 11.25)&quot;&gt;&lt;text x=&quot;9.9&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;or&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(188.09479496449194 42.23817186407689) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C16.04 -0.75, 24.93 -0.27, 31.75 0 M9 0 C13.41 -0.65, 18.92 -0.74, 31.75 0 M31.75 0 C39.3 -1.07, 41.57 3.44, 40.75 9 M31.75 0 C36.35 0.42, 41.65 5.14, 40.75 9 M40.75 9 C40.59 15.66, 42.39 20.29, 40.75 27 M40.75 9 C41.32 13.71, 40.22 19.52, 40.75 27 M40.75 27 C40.67 34.29, 38.97 36.49, 31.75 36 M40.75 27 C41.88 31.94, 39.94 34.51, 31.75 36 M31.75 36 C25.79 36.05, 19.12 35.71, 9 36 M31.75 36 C25.06 36.78, 19.19 35.16, 9 36 M9 36 C4.99 35.52, -0.97 31.71, 0 27 M9 36 C3.68 35.92, 2.08 30.89, 0 27 M0 27 C1.26 22.71, -1.79 15.51, 0 9 M0 27 C0.25 22.78, -0.09 19.55, 0 9 M0 9 C-0.61 3.59, 1.79 -1.54, 9 0 M0 9 C1.63 1.02, 0.71 -0.84, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(193.61979496449194 48.98817186407689) rotate(0 14.850000000000009 11.25)&quot;&gt;&lt;text x=&quot;14.850000000000001&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;not&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(234.1942230548177 40.896973821518316) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C16.99 1.63, 21.72 1.09, 31.75 0 M9 0 C16.95 0.6, 24.05 0.87, 31.75 0 M31.75 0 C39.32 -1.48, 40.71 2.37, 40.75 9 M31.75 0 C35.89 0.6, 42.34 1.62, 40.75 9 M40.75 9 C40.88 17.28, 41.96 23.02, 40.75 27 M40.75 9 C41.34 12.5, 41.37 18.04, 40.75 27 M40.75 27 C42.6 33.1, 39.39 37.49, 31.75 36 M40.75 27 C40.21 33.55, 38.96 37.13, 31.75 36 M31.75 36 C21.87 37.4, 16.3 37.77, 9 36 M31.75 36 C26.69 36.52, 21.62 35.13, 9 36 M9 36 C3.69 36.26, -1.46 33.07, 0 27 M9 36 C2.29 34.45, -0.76 32.28, 0 27 M0 27 C-1.52 20.34, -1.04 14.56, 0 9 M0 27 C-0.64 21.61, 0.15 14.15, 0 9 M0 9 C0.75 1.12, 4.74 1.36, 9 0 M0 9 C-0.77 3.6, 5.13 -1.35, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(244.66922305481773 47.646973821518316) rotate(0 9.900000000000006 11.25)&quot;&gt;&lt;text x=&quot;9.9&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;to&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(280.0047228434221 42.436141255997086) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C14.65 0.4, 18.79 0.97, 31.75 0 M9 0 C18.46 1.06, 27.31 -0.47, 31.75 0 M31.75 0 C39.18 0.17, 39.15 2.58, 40.75 9 M31.75 0 C39.22 0.25, 41.93 0.94, 40.75 9 M40.75 9 C39.24 15.52, 41.82 22.78, 40.75 27 M40.75 9 C40.46 15.77, 41.09 22.28, 40.75 27 M40.75 27 C41.87 34.79, 38.4 34.47, 31.75 36 M40.75 27 C42.81 31.99, 38.9 36.07, 31.75 36 M31.75 36 C24.87 34.93, 18.15 35.3, 9 36 M31.75 36 C25.44 35.18, 19.89 34.98, 9 36 M9 36 C4.92 37.49, 1.93 32.26, 0 27 M9 36 C2.14 36.36, -0.31 32.16, 0 27 M0 27 C0.4 20.28, 0.23 13.86, 0 9 M0 27 C-0.73 21.94, -0.58 17.4, 0 9 M0 9 C0.49 1.65, 3.06 0.34, 9 0 M0 9 C-0.85 3.64, 4.4 -1.89, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(290.4797228434221 49.186141255997086) rotate(0 9.900000000000006 11.25)&quot;&gt;&lt;text x=&quot;9.9&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;be&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(325.3675621151617 42.04376948822727) rotate(0 20.375 18)&quot;&gt;&lt;path d=&quot;M9 0 C16.12 0.75, 21.01 0.73, 31.75 0 M9 0 C17.74 0.16, 26.08 0.25, 31.75 0 M31.75 0 C36.24 -0.77, 40.81 3.52, 40.75 9 M31.75 0 C39.68 -1.87, 42.12 1.9, 40.75 9 M40.75 9 C40.78 13.51, 40.67 20.61, 40.75 27 M40.75 9 C40.94 12.87, 40.37 17.22, 40.75 27 M40.75 27 C42.23 33.51, 38.29 36.66, 31.75 36 M40.75 27 C41.65 32.59, 37.11 37.36, 31.75 36 M31.75 36 C23.7 35.9, 13.25 34.23, 9 36 M31.75 36 C25.69 36.4, 20.76 36.33, 9 36 M9 36 C2.49 35.43, -0.42 32.82, 0 27 M9 36 C1.71 36.83, 2.23 31.13, 0 27 M0 27 C1 22.24, -0.49 17, 0 9 M0 27 C0.71 22.96, 0.42 19.96, 0 9 M0 9 C1.54 2.67, 1.43 1.56, 9 0 M0 9 C-1.45 3.83, 3.3 -1.16, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(340.79256211516173 48.79376948822727) rotate(0 4.949999999999989 11.25)&quot;&gt;&lt;text x=&quot;4.95&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;,&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(186.30330209920737 86.58931281695692) rotate(0 0.1324463941830487 14.997007070317629)&quot;&gt;&lt;path d=&quot;M0.51 -0.34 C0.54 4.58, 0.16 24.79, 0.12 29.76 M0.11 0.68 C0.08 5.64, -0.25 25.48, -0.25 30.33&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(186.30330209920737 86.58931281695692) rotate(0 0.1324463941830487 14.997007070317629)&quot;&gt;&lt;path d=&quot;M-5.24 16.19 C-4.19 20.52, -2.59 24.94, -0.25 30.33 M-5.24 16.19 C-4.37 19.01, -2.78 22.25, -0.25 30.33&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(186.30330209920737 86.58931281695692) rotate(0 0.1324463941830487 14.997007070317629)&quot;&gt;&lt;path d=&quot;M5.02 16.29 C3.03 20.63, 1.6 25.03, -0.25 30.33 M5.02 16.29 C3.81 19.04, 3.3 22.26, -0.25 30.33&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(16.30330209920737 122.58931281695692) rotate(0 170 40)&quot;&gt;&lt;path d=&quot;M20 0 C129.38 1.73, 233.66 3.41, 320 0 C335.69 3.29, 342.21 4.67, 340 20 C341.53 26.29, 342.85 41.63, 340 60 C337.14 75.13, 333.9 77.19, 320 80 C246.41 75.22, 174.23 76.93, 20 80 C6.05 82.63, 2.25 75.21, 0 60 C-1.59 51.33, -0.96 43.33, 0 20 C3.58 3.87, 7.45 0.01, 20 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#a5d8ff&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M20 0 C98.9 -0.77, 179.65 -1.84, 320 0 M20 0 C131.41 0.34, 242.11 0.36, 320 0 M320 0 C333.29 -1.45, 341.57 5.15, 340 20 M320 0 C333.75 1.99, 339.87 5.87, 340 20 M340 20 C338.48 29.78, 339.36 43.13, 340 60 M340 20 C340.12 34.57, 340.48 50.03, 340 60 M340 60 C341.39 75.29, 332.78 78.37, 320 80 M340 60 C341.12 73.45, 331.72 80.29, 320 80 M320 80 C208.78 81.27, 98.97 82.38, 20 80 M320 80 C240.38 81.9, 161.88 82.72, 20 80 M20 80 C7.99 78.2, 0.74 71.87, 0 60 M20 80 C7.15 80.83, 1.14 72.26, 0 60 M0 60 C0.08 49.3, -1.56 36.91, 0 20 M0 60 C0.74 47.37, -0.11 33.25, 0 20 M0 20 C-0.54 6.46, 5.8 -0.82, 20 0 M0 20 C-2.25 8.78, 6.27 -1.8, 20 0&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(54.30330209920737 137.58931281695692) rotate(0 132 25)&quot;&gt;&lt;text x=&quot;132&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Prefill&lt;/text&gt;&lt;text x=&quot;132&quot; y=&quot;42.62&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;all 8 tokens in one pass&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(368.6236763907632 123.6597697752506) rotate(0 46 40)&quot;&gt;&lt;path d=&quot;M20 0 C41.37 0.41, 58.99 -0.68, 72 0 C82.55 2.48, 90.21 9.95, 92 20 C89.3 30.48, 91.2 38.14, 92 60 C88.77 75.36, 87.34 76.79, 72 80 C56.05 80.51, 37.24 82.58, 20 80 C6.15 80.55, 1.46 72.42, 0 60 C-2.6 48.98, -1.42 38.53, 0 20 C-1.28 8.05, 5.18 -0.21, 20 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#ffec99&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M20 0 C33.06 1.3, 48.7 -1.5, 72 0 M20 0 C31.49 0.02, 43.12 0.39, 72 0 M72 0 C86.29 1.27, 93.63 4.88, 92 20 M72 0 C86.09 -0.98, 93.93 6.9, 92 20 M92 20 C92.54 32.48, 89.89 45.08, 92 60 M92 20 C92.25 32.24, 92.92 42.65, 92 60 M92 60 C92.76 73.64, 84.76 78.4, 72 80 M92 60 C92.58 71.29, 87.57 77.78, 72 80 M72 80 C54.21 80.76, 36.87 78.41, 20 80 M72 80 C59.51 78.87, 47.96 79.35, 20 80 M20 80 C4.97 78.37, 0.71 71.75, 0 60 M20 80 C8.71 82.2, -0.08 71.17, 0 60 M0 60 C1.73 44.55, -0.25 34.17, 0 20 M0 60 C-0.09 48.23, 0.09 34.98, 0 20 M0 20 C-0.23 7.7, 6.64 -1.74, 20 0 M0 20 C1.52 6.15, 5.43 -0.03, 20 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(376.1236763907632 138.6597697752506) rotate(0 38.5 25)&quot;&gt;&lt;text x=&quot;38.5&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Decode&lt;/text&gt;&lt;text x=&quot;38.5&quot; y=&quot;42.62&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1 token&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(414.3033020992074 208.58931281695692) rotate(0 -0.10011787626322644 13.22047891541267)&quot;&gt;&lt;path d=&quot;M-0.48 0.38 C-0.37 4.69, 0.07 22.06, 0.18 26.35 M0.27 0.09 C0.37 4.49, -0.1 21.12, -0.16 25.56&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(414.3033020992074 208.58931281695692) rotate(0 -0.10011787626322644 13.22047891541267)&quot;&gt;&lt;path d=&quot;M-4.35 13.25 C-2.99 16, -2.17 18.51, -0.16 25.56 M-4.35 13.25 C-2.95 16.88, -1.74 21.16, -0.16 25.56&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(414.3033020992074 208.58931281695692) rotate(0 -0.10011787626322644 13.22047891541267)&quot;&gt;&lt;path d=&quot;M4.54 13.44 C4.08 16.11, 3.08 18.58, -0.16 25.56 M4.54 13.44 C3.13 16.97, 1.52 21.19, -0.16 25.56&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(374.3033020992074 240.58931281695692) rotate(0 40 18)&quot;&gt;&lt;path d=&quot;M9 0 C26.34 0.66, 43.16 0.46, 71 0 M9 0 C30.63 0.17, 53.77 -0.73, 71 0 M71 0 C75.16 -0.48, 80.35 4.77, 80 9 M71 0 C78.49 0.81, 79.09 3.06, 80 9 M80 9 C81.8 14.4, 80.46 18.23, 80 27 M80 9 C79.95 13.12, 79.44 17.55, 80 27 M80 27 C80.91 32.16, 75.32 36.61, 71 36 M80 27 C78.51 34.24, 75.86 35.31, 71 36 M71 36 C47.84 36.45, 28.34 35.51, 9 36 M71 36 C50.27 36.07, 29.93 36.09, 9 36 M9 36 C3.38 36.97, -0.65 34.21, 0 27 M9 36 C1.79 35.24, 2.22 33.18, 0 27 M0 27 C-1.42 19.51, 0.25 14.12, 0 9 M0 27 C-0.09 19.44, -0.58 12.9, 0 9 M0 9 C-0.78 3.2, 4.42 -1.17, 9 0 M0 9 C1.9 1.12, 2.59 0.56, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(404.4033020992074 247.33931281695692) rotate(0 9.900000000000006 11.25)&quot;&gt;&lt;text x=&quot;9.9&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;is&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(470.3033020992074 122.58931281695692) rotate(0 46 40)&quot;&gt;&lt;path d=&quot;M20 0 C35.27 -0.46, 49.38 -1.72, 72 0 C87.85 2.58, 95.11 5.2, 92 20 C95.11 30.76, 91.76 37.99, 92 60 C89.23 71.83, 87.23 77.51, 72 80 C61.25 77.54, 50.22 78.78, 20 80 C7.95 77.19, -0.76 70.16, 0 60 C0.96 50.94, 0.46 39.94, 0 20 C2.35 5.61, 3.5 -2.6, 20 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#ffec99&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M20 0 C36.87 -0.43, 53.72 0.23, 72 0 M20 0 C32.12 -0.66, 43.02 0.24, 72 0 M72 0 C85.31 -0.23, 90.87 5.11, 92 20 M72 0 C83.96 0.6, 91.76 6.41, 92 20 M92 20 C93.88 31.26, 91.4 41.16, 92 60 M92 20 C92.45 30.94, 92.42 42.42, 92 60 M92 60 C90.17 74.84, 86.42 80.03, 72 80 M92 60 C90.42 74.01, 84.5 79.31, 72 80 M72 80 C51.55 79.52, 32.3 79.84, 20 80 M72 80 C57.11 81.06, 43.09 79.87, 20 80 M20 80 C8.15 78.39, 1.59 74.21, 0 60 M20 80 C5.49 79.91, 0.2 75.2, 0 60 M0 60 C-0.66 46.12, -0.3 33.44, 0 20 M0 60 C-0.06 49.63, 0.71 37.3, 0 20 M0 20 C-0.26 7.63, 8.64 1.4, 20 0 M0 20 C0.47 7.42, 5.73 0.09, 20 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(477.8033020992074 137.58931281695692) rotate(0 38.5 25)&quot;&gt;&lt;text x=&quot;38.5&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Decode&lt;/text&gt;&lt;text x=&quot;38.5&quot; y=&quot;42.62&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1 token&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(516.3033020992074 208.58931281695692) rotate(0 0.12979560005476287 13.059344039955178)&quot;&gt;&lt;path d=&quot;M-0.48 0.15 C-0.47 4.41, -0.39 21.5, -0.35 25.79 M0.27 -0.26 C0.5 4.07, 0.87 21.93, 0.7 26.37&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(516.3033020992074 208.58931281695692) rotate(0 0.12979560005476287 13.059344039955178)&quot;&gt;&lt;path d=&quot;M-3.7 14.14 C-2.54 16.43, -2 19.2, 0.7 26.37 M-3.7 14.14 C-2.43 17.8, -1.03 21.14, 0.7 26.37&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(516.3033020992074 208.58931281695692) rotate(0 0.12979560005476287 13.059344039955178)&quot;&gt;&lt;path d=&quot;M5.19 14.18 C4.4 16.42, 2.99 19.18, 0.7 26.37 M5.19 14.18 C4.01 17.81, 2.95 21.14, 0.7 26.37&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(476.3033020992074 240.58931281695692) rotate(0 40 18)&quot;&gt;&lt;path d=&quot;M9 0 C29.34 -0.94, 51.99 0.54, 71 0 M9 0 C25.01 1.26, 39.52 1.42, 71 0 M71 0 C78.85 1.4, 80.99 2.37, 80 9 M71 0 C76.68 -0.81, 77.96 5.07, 80 9 M80 9 C78.67 13.61, 80.25 19.96, 80 27 M80 9 C79.32 14.57, 80.26 19.9, 80 27 M80 27 C79.86 33.64, 75.77 36.45, 71 36 M80 27 C80.95 34.4, 76.42 33.92, 71 36 M71 36 C55.62 34.38, 43.37 35.37, 9 36 M71 36 C55.49 36.58, 38.13 35.6, 9 36 M9 36 C1.86 36.17, -0.88 31.31, 0 27 M9 36 C0.8 36.42, -2.19 34.89, 0 27 M0 27 C1.48 20.24, -1.48 14.69, 0 9 M0 27 C-0.14 22.99, -0.11 18.91, 0 9 M0 9 C0.74 3.09, 1 1.62, 9 0 M0 9 C-1.43 2.76, 2.69 1.62, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(501.45330209920735 247.33931281695692) rotate(0 14.850000000000023 11.25)&quot;&gt;&lt;text x=&quot;14.850000000000001&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;the&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(571.5786459635453 122.34521811862868) rotate(0 46 40)&quot;&gt;&lt;path d=&quot;M20 0 C39.58 -1.93, 51.72 -0.19, 72 0 C88.02 1.16, 92.24 6.25, 92 20 C91.79 29.61, 93.7 49.61, 92 60 C89.89 74.06, 83.31 82.78, 72 80 C57.01 77.42, 50.06 81.45, 20 80 C5.6 80.32, -0.11 73.27, 0 60 C-0.87 54.62, -0.1 45.22, 0 20 C1.94 9.87, 5.75 0.39, 20 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#ffec99&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M20 0 C36.36 -1.39, 52.26 0.3, 72 0 M20 0 C31.14 0.93, 41.93 0.01, 72 0 M72 0 C84.94 0.28, 91.82 4.75, 92 20 M72 0 C84.84 -0.05, 90.59 5.79, 92 20 M92 20 C92.02 30.41, 92.17 38.88, 92 60 M92 20 C91.67 30.06, 92.07 38.66, 92 60 M92 60 C93.29 74.27, 83.55 79.52, 72 80 M92 60 C91.79 71.35, 87.29 78.08, 72 80 M72 80 C60.71 78.41, 45.55 78.26, 20 80 M72 80 C53.39 81.29, 33.37 79.54, 20 80 M20 80 C5.04 81.62, 1.34 71.47, 0 60 M20 80 C6.58 81.04, -0.88 71.15, 0 60 M0 60 C-1.56 51.55, -0.12 38.29, 0 20 M0 60 C-0.71 49.29, 0.11 36.31, 0 20 M0 20 C0.39 6.75, 7.07 -0.76, 20 0 M0 20 C-1.47 8.79, 8.73 0.31, 20 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(579.0786459635453 137.34521811862868) rotate(0 38.5 25)&quot;&gt;&lt;text x=&quot;38.5&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Decode&lt;/text&gt;&lt;text x=&quot;38.5&quot; y=&quot;42.62&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1 token&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(618.3033020992074 208.58931281695692) rotate(0 -0.2515410282588846 12.845403967859454)&quot;&gt;&lt;path d=&quot;M-0.02 -0.23 C0 4.17, 0.25 22.12, 0.18 26.51 M-0.69 -0.82 C-0.73 3.43, -0.38 21.21, -0.16 25.81&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(618.3033020992074 208.58931281695692) rotate(0 -0.2515410282588846 12.845403967859454)&quot;&gt;&lt;path d=&quot;M-4.99 13.74 C-3.76 17.65, -1.41 20.61, -0.16 25.81 M-4.99 13.74 C-3.62 17.55, -2.12 21.65, -0.16 25.81&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(618.3033020992074 208.58931281695692) rotate(0 -0.2515410282588846 12.845403967859454)&quot;&gt;&lt;path d=&quot;M3.9 13.46 C2.4 17.45, 2.03 20.5, -0.16 25.81 M3.9 13.46 C2.46 17.39, 1.16 21.57, -0.16 25.81&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(578.3033020992074 240.58931281695692) rotate(0 40 18)&quot;&gt;&lt;path d=&quot;M9 0 C31.83 2.22, 54.64 1.9, 71 0 M9 0 C24.65 -0.69, 41.11 -0.35, 71 0 M71 0 C75.61 0.27, 81.14 3.78, 80 9 M71 0 C78.58 0.31, 80.85 2.73, 80 9 M80 9 C81.49 14.42, 78.61 23.62, 80 27 M80 9 C80.65 16.55, 79.45 22.15, 80 27 M80 27 C78.09 33.55, 77.75 36.42, 71 36 M80 27 C78.23 34.1, 78.28 35.93, 71 36 M71 36 C52.36 35.9, 30.33 37.24, 9 36 M71 36 C55.62 36.37, 40.11 37.13, 9 36 M9 36 C3.41 37.86, -1.06 31.66, 0 27 M9 36 C2.34 36.73, 2.21 32.59, 0 27 M0 27 C0.25 21.03, -0.64 18.34, 0 9 M0 27 C0.41 22.7, 0.61 17.68, 0 9 M0 9 C-0.44 4.84, 4.8 -1.51, 9 0 M0 9 C-1.09 3.12, 4.37 -0.1, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(578.7033020992073 247.33931281695692) rotate(0 39.60000000000002 11.25)&quot;&gt;&lt;text x=&quot;39.6&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;question&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(674.1057765257597 121.41562503624579) rotate(0 46 40)&quot;&gt;&lt;path d=&quot;M20 0 C32.23 3.52, 44.07 0.19, 72 0 C83.73 3.12, 89.89 7.22, 92 20 C89.94 33.19, 89.18 48.37, 92 60 C90.55 75.76, 83.05 78.26, 72 80 C52.5 78.28, 32.97 78.53, 20 80 C8.49 77.25, 1.95 71.37, 0 60 C-2.34 47.08, 0.69 35.65, 0 20 C1.81 7.9, 9.66 -1.49, 20 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#ffec99&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M20 0 C40 -1.32, 59.26 0.84, 72 0 M20 0 C34.58 0.67, 48.25 0.24, 72 0 M72 0 C84.31 1.78, 90.56 5.45, 92 20 M72 0 C83.27 0.51, 89.94 8.9, 92 20 M92 20 C90.22 29.1, 93.21 37.25, 92 60 M92 20 C92.2 33.97, 91.78 47.56, 92 60 M92 60 C90.71 74.39, 85.7 78.16, 72 80 M92 60 C92.62 75.46, 87.27 79.52, 72 80 M72 80 C53.63 78.04, 38.55 79.46, 20 80 M72 80 C59.87 79.23, 47.97 79.94, 20 80 M20 80 C6.06 80.59, 0.17 74.83, 0 60 M20 80 C7.49 78, -0.19 72.7, 0 60 M0 60 C1.27 49.8, -1.06 37.97, 0 20 M0 60 C-0.85 47.92, 0.7 37.23, 0 20 M0 20 C-0.22 6.08, 8.58 -0.63, 20 0 M0 20 C2.21 5.21, 5.72 2.3, 20 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(681.6057765257597 136.41562503624579) rotate(0 38.5 25)&quot;&gt;&lt;text x=&quot;38.5&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Decode&lt;/text&gt;&lt;text x=&quot;38.5&quot; y=&quot;42.62&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1 token&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(720.3033020992074 208.58931281695692) rotate(0 -0.27259055725380676 12.859798789210615)&quot;&gt;&lt;path d=&quot;M0.12 -0.54 C0.13 3.86, -0.17 21.82, -0.13 26.26 M-0.49 0.37 C-0.53 4.63, -0.74 21.04, -0.63 25.43&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(720.3033020992074 208.58931281695692) rotate(0 -0.27259055725380676 12.859798789210615)&quot;&gt;&lt;path d=&quot;M-5.12 13.23 C-4.64 16.19, -2.54 19.85, -0.63 25.43 M-5.12 13.23 C-3.61 16.95, -2.49 20.81, -0.63 25.43&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(720.3033020992074 208.58931281695692) rotate(0 -0.27259055725380676 12.859798789210615)&quot;&gt;&lt;path d=&quot;M3.77 13.2 C1.98 16.18, 1.81 19.85, -0.63 25.43 M3.77 13.2 C2.48 16.97, 0.79 20.83, -0.63 25.43&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(680.3033020992074 240.58931281695692) rotate(0 40 18)&quot;&gt;&lt;path d=&quot;M9 0 C22.95 -1.55, 41.45 1.32, 71 0 M9 0 C25.99 0.52, 41.52 0.6, 71 0 M71 0 C75.02 -1.49, 79.04 1.88, 80 9 M71 0 C75.08 -0.31, 80.27 3.26, 80 9 M80 9 C80.98 14.24, 79.25 18.5, 80 27 M80 9 C80.59 14.96, 79.33 19.2, 80 27 M80 27 C78.08 33.56, 76.01 35.99, 71 36 M80 27 C81.12 30.85, 77 33.84, 71 36 M71 36 C48.33 36.75, 27.48 34.87, 9 36 M71 36 C49.7 36.52, 29.46 34.73, 9 36 M9 36 C4.82 37.28, 0.46 33.46, 0 27 M9 36 C1.24 37.05, 2.05 32.4, 0 27 M0 27 C-0.93 22.51, 1.38 15.03, 0 9 M0 27 C0.52 21.38, 0.33 14.09, 0 9 M0 9 C-0.1 1.66, 3.92 -1.94, 9 0 M0 9 C-0.08 3.98, 1.92 -0.75, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(715.3533020992073 247.33931281695692) rotate(0 4.949999999999989 11.25)&quot;&gt;&lt;text x=&quot;4.95&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;:&lt;/text&gt;&lt;/g&gt;&lt;g transform=&quot;translate(780.3033020992074 146.58931281695692) rotate(0 7.699999999999989 17.5)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;24.668&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;28px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;…&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(310.3033020992074 208.58931281695692) rotate(0 -0.18045083209011636 12.847660600445224)&quot;&gt;&lt;path d=&quot;M0.11 0.04 C0.2 4.34, -0.02 21.24, 0.02 25.61 M-0.49 -0.42 C-0.44 3.96, -0.52 21.82, -0.4 26.12&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(310.3033020992074 208.58931281695692) rotate(0 -0.18045083209011636 12.847660600445224)&quot;&gt;&lt;path d=&quot;M-4.96 13.94 C-3.96 16.56, -3.49 19.57, -0.4 26.12 M-4.96 13.94 C-3.37 18.44, -1.55 22.52, -0.4 26.12&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(310.3033020992074 208.58931281695692) rotate(0 -0.18045083209011636 12.847660600445224)&quot;&gt;&lt;path d=&quot;M3.93 13.86 C3.03 16.51, 1.6 19.53, -0.4 26.12 M3.93 13.86 C2.4 18.42, 1.1 22.53, -0.4 26.12&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(270.3033020992074 240.58931281695692) rotate(0 40 18)&quot;&gt;&lt;path d=&quot;M9 0 C28.39 1.55, 49.3 0.01, 71 0 M9 0 C25.11 -0.92, 43.67 -0.32, 71 0 M71 0 C77.49 -1.36, 81.16 1.83, 80 9 M71 0 C75.29 1.63, 78.89 4.64, 80 9 M80 9 C79.24 14.47, 78.65 20, 80 27 M80 9 C79.8 15.16, 80.33 22.12, 80 27 M80 27 C79.72 31.81, 77.61 37.8, 71 36 M80 27 C82.17 33.27, 75.01 37.09, 71 36 M71 36 C53.58 34.89, 37.43 35.04, 9 36 M71 36 C50.86 36.82, 31.03 35.7, 9 36 M9 36 C2.32 35.28, 1.95 34.22, 0 27 M9 36 C2.15 35.69, -0.19 35.16, 0 27 M0 27 C-1.66 22.5, 1.04 17.77, 0 9 M0 27 C-0.43 22.07, 0.82 15.08, 0 9 M0 9 C1.89 4.58, 4.49 0.46, 9 0 M0 9 C-1.7 4.49, 4.18 -0.3, 9 0&quot; stroke=&quot;#1e1e1e&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(290.50330209920736 247.33931281695692) rotate(0 19.80000000000001 11.25)&quot;&gt;&lt;text x=&quot;19.8&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;that&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(16.30330209920737 328.5893128169569) rotate(0 400.5891001188569 0.08183734031393897)&quot;&gt;&lt;path d=&quot;M1.07 -0.15 C134.11 -0.23, 665.89 -0.25, 798.89 -0.07 M0.17 -1.27 C133.51 -1.19, 667.47 1.37, 801.01 1.44&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(16.30330209920737 328.5893128169569) rotate(0 400.5891001188569 0.08183734031393897)&quot;&gt;&lt;path d=&quot;M777.5 9.92 C785.48 4.87, 793.89 2.17, 801.01 1.44 M777.5 9.92 C786.77 6.7, 796.24 3.2, 801.01 1.44&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(16.30330209920737 328.5893128169569) rotate(0 400.5891001188569 0.08183734031393897)&quot;&gt;&lt;path d=&quot;M777.55 -7.18 C785.57 -6.22, 793.96 -2.91, 801.01 1.44 M777.55 -7.18 C786.93 -3.91, 796.39 -0.93, 801.01 1.44&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(19.750228678095823 296.3478932501892) rotate(0 51.624000549316406 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#555&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;output time&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(16.30330209920737 350.5893128169569) rotate(0 170.08881926228486 4.407105683104049)&quot;&gt;&lt;path d=&quot;M0 0 C-0.42 1.8, -0.12 3.83, 0 8 M0 0 C0.31 2.21, -0.11 4.19, 0 8 M0 8 C123.18 9.14, 246.09 9.03, 340 8 M0 8 C131.56 7.42, 262.44 7.22, 340 8 M340 8 C340.36 6.73, 340.67 4.78, 340 0 M340 8 C339.77 5.31, 339.76 2.26, 340 0&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(82.35330209920737 366.5893128169569) rotate(0 103.94999999999999 22.5)&quot;&gt;&lt;text x=&quot;103.95&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1971c2&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;TTFT&lt;/text&gt;&lt;text x=&quot;103.95&quot; y=&quot;38.358000000000004&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1971c2&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;(time to first token)&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(368.3033020992074 350.5893128169569) rotate(0 45.970561271126996 4.749232837403127)&quot;&gt;&lt;path d=&quot;M0 0 C0.48 1.69, -0.27 4.25, 0 8 M0 0 C0.18 2.66, -0.19 5.6, 0 8 M0 8 C30.27 9.73, 57.51 10.25, 92 8 M0 8 C27.75 8.08, 53.54 9.72, 92 8 M92 8 C91.67 6.52, 91.53 3.96, 92 0 M92 8 C91.72 5.23, 91.95 2.25, 92 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(349.95330209920735 366.5893128169569) rotate(0 64.35000000000002 33.75)&quot;&gt;&lt;text x=&quot;64.35000000000001&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#f08c00&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;TPOT&lt;/text&gt;&lt;text x=&quot;64.35000000000001&quot; y=&quot;38.358000000000004&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#f08c00&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;(time per&lt;/text&gt;&lt;text x=&quot;64.35000000000001&quot; y=&quot;60.858000000000004&quot; font-family=&quot;Excalifont-1j2olal, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#f08c00&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;output token)&lt;/text&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;h2 id=&quot;initial-setup&quot;&gt;Initial Setup&lt;a href=&quot;#initial-setup&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For this exploration, I ran the &lt;a href=&quot;https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct&quot;&gt;Qwen2.5-0.5B-Instruct&lt;/a&gt; model (bfloat16) on my M2 MacBook Pro (16 GB) via PyTorch’s MPS backend. I used the &lt;a href=&quot;https://huggingface.co/docs/transformers/index&quot;&gt;&lt;code&gt;transformers&lt;/code&gt; library from Hugging Face&lt;/a&gt; to set this up:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; torch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; transformers &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; AutoModelForCausalLM, AutoTokenizer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;Qwen/Qwen2.5-0.5B-Instruct&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;tok &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; AutoTokenizer.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_pretrained&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;model &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; AutoModelForCausalLM.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_pretrained&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;bfloat16&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;mps&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;eval&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These few lines load the model and set up a tokenizer that we can use to prepare inputs for the model.&lt;/p&gt;
&lt;h2 id=&quot;measuring-the-prefill-and-decoding-phases&quot;&gt;Measuring the Prefill and Decoding Phases&lt;a href=&quot;#measuring-the-prefill-and-decoding-phases&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Models loaded this way offer a &lt;code&gt;generate&lt;/code&gt; method you can use to generate outputs. However, this method doesn’t report times for the prefill and decode phases so we have to compute those manually.&lt;/p&gt;
&lt;p&gt;We can start by writing a function to measure how long it takes to generate &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; tokens:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(ids, n):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    torch.mps.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;synchronize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# wait for any earlier GPU work to finish&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    t0 &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;perf_counter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    model.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; min_new_tokens&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; max_new_tokens&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; do_sample&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    torch.mps.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;synchronize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# wait for this call&amp;#39;s GPU work to finish&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;perf_counter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; t0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we set &lt;code&gt;min_new_tokens=n&lt;/code&gt; so the model can’t stop early and &lt;code&gt;do_sample=False&lt;/code&gt; for greedy decoding, meaning the model always selects the most probable token, so runs are repeatable.&lt;/p&gt;
&lt;p&gt;Measuring the time it takes to generate one new token roughly gives us the TTFT. However, getting the TPOT requires more effort. The time it takes to generate 101 new tokens roughly equals the TTFT + (100 tokens) × TPOT. From the total time, we can subtract the TTFT and divide by 100 tokens to obtain the TPOT.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; statistics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ttft_tpot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(ids, num_samples&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# warm-up&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    ttft &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; statistics.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;median&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;num_samples&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; statistics.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;median&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;timed_generate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 101&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;num_samples&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ttft, (total &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ttft) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this function, we make a warm-up call first, then compute the median of 5 runs.&lt;/p&gt;
&lt;p&gt;Now, we can load a text file and use that to see how TTFT and TPOT vary with length of the input prompt:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;input.txt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;all_ids &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tok&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;20_000&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; return_tensors&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).input_ids.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;mps&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; length &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;512&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2048&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    ttft, tpot &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ttft_tpot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;all_ids&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[:,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ttft&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tpot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; length &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ttft&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For this example, I used &lt;a href=&quot;https://github.com/karpathy/char-rnn/blob/master/data/tinyshakespeare/input.txt&quot;&gt;this text file&lt;/a&gt; containing the contents of some of Shakespeare’s plays. The inputs to the model are plain-text prompts cut from the start of this text file and include the first 16, 128, 512, and 2,048 tokens, respectively.&lt;/p&gt;
&lt;h2 id=&quot;results&quot;&gt;Results&lt;a href=&quot;#results&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Each value in the table below represents the median of 5 runs.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:right&quot;&gt;Prompt (tokens)&lt;/th&gt;
&lt;th style=&quot;text-align:right&quot;&gt;TTFT (ms)&lt;/th&gt;
&lt;th style=&quot;text-align:right&quot;&gt;TPOT (ms)&lt;/th&gt;
&lt;th style=&quot;text-align:right&quot;&gt;Prefill throughput (tokens/s)&lt;/th&gt;
&lt;th style=&quot;text-align:right&quot;&gt;Decode throughput (tokens/s)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:right&quot;&gt;16&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;29.6&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;8.79&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;540&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;114&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:right&quot;&gt;128&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;68.1&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;9.09&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;1,880&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;110&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:right&quot;&gt;512&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;188&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;9.12&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;2,720&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;110&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:right&quot;&gt;2,048&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;711&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;10.1&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;2,880&lt;/td&gt;
&lt;td style=&quot;text-align:right&quot;&gt;99&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The data show that TTFT grows with the prompt, starting from 29.6 ms at 16 tokens to 711 ms at 2,048 (about 24× bigger for a prompt 128× longer). Prefill throughput (prompt length / TTFT) climbs from 540 to about 2,900 tokens/s, then levels off. At 16 tokens, there’s probably some overhead in &lt;code&gt;generate()&lt;/code&gt; which dominates TTFT, so the number is low.&lt;/p&gt;
&lt;p&gt;I was surprised to learn that TPOT barely moves. It varies from 8.8 ms to 10.1 ms over the same range. I would have thought that for larger inputs, the time to generate output tokens would significantly increase but this wasn’t the case. Correspondingly, the decode throughput doesn’t change much from 114 to 99 tokens/s.&lt;/p&gt;
&lt;figure class=&quot;line-chart&quot; style=&quot;margin-block: var(--figure-vertical-spacing)&quot; data-astro-cid-dwn4dnrm&gt;&lt;div class=&quot;figure-card line-chart__card&quot; data-astro-cid-dwn4dnrm&gt;&lt;p class=&quot;line-chart__title&quot; id=&quot;line-chart-d9822ed0-title&quot; data-astro-cid-dwn4dnrm&gt;Token Throughput vs Prompt Length&lt;/p&gt;&lt;div class=&quot;line-chart__plot&quot; data-astro-cid-dwn4dnrm&gt;&lt;svg class=&quot;line-chart__svg&quot; viewBox=&quot;0 0 560 340&quot; role=&quot;img&quot; aria-labelledby=&quot;line-chart-d9822ed0-title line-chart-d9822ed0-desc&quot; data-astro-cid-dwn4dnrm&gt;&lt;desc id=&quot;line-chart-d9822ed0-desc&quot; data-astro-cid-dwn4dnrm&gt;16 tokens: Prefill 540 tokens/s, Decode 114 tokens/s. 128 tokens: Prefill 1,880 tokens/s, Decode 110 tokens/s. 512 tokens: Prefill 2,721 tokens/s, Decode 110 tokens/s. 2,048 tokens: Prefill 2,881 tokens/s, Decode 99 tokens/s.&lt;/desc&gt;&lt;g data-astro-cid-dwn4dnrm&gt;&lt;line class=&quot;line-chart__grid&quot; x1=&quot;94&quot; x2=&quot;472&quot; y1=&quot;284&quot; y2=&quot;284&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;84&quot; y=&quot;284&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;0&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-dwn4dnrm&gt;&lt;line class=&quot;line-chart__grid&quot; x1=&quot;94&quot; x2=&quot;472&quot; y1=&quot;218&quot; y2=&quot;218&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;84&quot; y=&quot;218&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;1,000&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-dwn4dnrm&gt;&lt;line class=&quot;line-chart__grid&quot; x1=&quot;94&quot; x2=&quot;472&quot; y1=&quot;152&quot; y2=&quot;152&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;84&quot; y=&quot;152&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;2,000&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-dwn4dnrm&gt;&lt;line class=&quot;line-chart__grid&quot; x1=&quot;94&quot; x2=&quot;472&quot; y1=&quot;86&quot; y2=&quot;86&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;84&quot; y=&quot;86&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;3,000&lt;/text&gt;&lt;/g&gt;&lt;g data-astro-cid-dwn4dnrm&gt;&lt;line class=&quot;line-chart__grid&quot; x1=&quot;94&quot; x2=&quot;472&quot; y1=&quot;20&quot; y2=&quot;20&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;84&quot; y=&quot;20&quot; text-anchor=&quot;end&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;4,000&lt;/text&gt;&lt;/g&gt;&lt;text class=&quot;line-chart__axis-title&quot; transform=&quot;translate(16 152) rotate(-90)&quot; text-anchor=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;tokens/s&lt;/text&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;113.38461538461539&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;16&lt;/text&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;258.7692307692307&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;128&lt;/text&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;355.6923076923077&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;512&lt;/text&gt;&lt;text class=&quot;line-chart__tick&quot; x=&quot;452.6153846153846&quot; y=&quot;306&quot; text-anchor=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;2,048&lt;/text&gt;&lt;text class=&quot;line-chart__axis-title&quot; x=&quot;283&quot; y=&quot;332&quot; text-anchor=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;Prompt length (tokens)&lt;/text&gt;&lt;line class=&quot;line-chart__crosshair&quot; x1=&quot;0&quot; x2=&quot;0&quot; y1=&quot;20&quot; y2=&quot;284&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;path class=&quot;line-chart__line line-chart__series-1&quot; d=&quot;M113.4,248.3 L258.8,159.9 L355.7,104.4 L452.6,93.8&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__line line-chart__series-2&quot; d=&quot;M113.4,276.5 L258.8,276.7 L355.7,276.8 L452.6,277.5&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-1&quot; d=&quot;M108.88461538461539,248.3336 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-1&quot; d=&quot;M254.26923076923072,159.91340000000002 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-1&quot; d=&quot;M351.1923076923077,104.4074 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-1&quot; d=&quot;M448.1153846153846,93.8408 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-2&quot; d=&quot;M109.38461538461539,272.4892 h8 v8 h-8 Z&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-2&quot; d=&quot;M254.76923076923072,272.74 h8 v8 h-8 Z&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-2&quot; d=&quot;M351.6923076923077,272.7664 h8 v8 h-8 Z&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;path class=&quot;line-chart__dot line-chart__series-2&quot; d=&quot;M448.6153846153846,273.4858 h8 v8 h-8 Z&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;text class=&quot;line-chart__value&quot; x=&quot;452.6153846153846&quot; y=&quot;79.8408&quot; text-anchor=&quot;end&quot; data-astro-cid-dwn4dnrm&gt;2,881 tokens/s&lt;/text&gt;&lt;text class=&quot;line-chart__value&quot; x=&quot;452.6153846153846&quot; y=&quot;263.4858&quot; text-anchor=&quot;end&quot; data-astro-cid-dwn4dnrm&gt;99 tokens/s&lt;/text&gt;&lt;text class=&quot;line-chart__label&quot; x=&quot;466.6153846153846&quot; y=&quot;93.8408&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;Prefill&lt;/text&gt;&lt;text class=&quot;line-chart__label&quot; x=&quot;466.6153846153846&quot; y=&quot;277.4858&quot; dominant-baseline=&quot;middle&quot; data-astro-cid-dwn4dnrm&gt;Decode&lt;/text&gt;&lt;rect class=&quot;line-chart__hit&quot; x=&quot;94&quot; y=&quot;20&quot; width=&quot;92.07692307692304&quot; height=&quot;264&quot; role=&quot;img&quot; tabindex=&quot;0&quot; data-x=&quot;113.38461538461539&quot; data-title=&quot;16 tokens&quot; data-rows=&quot;[{&amp;quot;slot&amp;quot;:1,&amp;quot;name&amp;quot;:&amp;quot;Prefill&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;540 tokens/s&amp;quot;},{&amp;quot;slot&amp;quot;:2,&amp;quot;name&amp;quot;:&amp;quot;Decode&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;114 tokens/s&amp;quot;}]&quot; aria-label=&quot;16 tokens: Prefill 540 tokens/s, Decode 114 tokens/s&quot; data-astro-cid-dwn4dnrm&gt;&lt;/rect&gt;&lt;rect class=&quot;line-chart__hit&quot; x=&quot;186.07692307692304&quot; y=&quot;20&quot; width=&quot;121.15384615384613&quot; height=&quot;264&quot; role=&quot;img&quot; tabindex=&quot;0&quot; data-x=&quot;258.7692307692307&quot; data-title=&quot;128 tokens&quot; data-rows=&quot;[{&amp;quot;slot&amp;quot;:1,&amp;quot;name&amp;quot;:&amp;quot;Prefill&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;1,880 tokens/s&amp;quot;},{&amp;quot;slot&amp;quot;:2,&amp;quot;name&amp;quot;:&amp;quot;Decode&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;110 tokens/s&amp;quot;}]&quot; aria-label=&quot;128 tokens: Prefill 1,880 tokens/s, Decode 110 tokens/s&quot; data-astro-cid-dwn4dnrm&gt;&lt;/rect&gt;&lt;rect class=&quot;line-chart__hit&quot; x=&quot;307.23076923076917&quot; y=&quot;20&quot; width=&quot;96.92307692307696&quot; height=&quot;264&quot; role=&quot;img&quot; tabindex=&quot;0&quot; data-x=&quot;355.6923076923077&quot; data-title=&quot;512 tokens&quot; data-rows=&quot;[{&amp;quot;slot&amp;quot;:1,&amp;quot;name&amp;quot;:&amp;quot;Prefill&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;2,721 tokens/s&amp;quot;},{&amp;quot;slot&amp;quot;:2,&amp;quot;name&amp;quot;:&amp;quot;Decode&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;110 tokens/s&amp;quot;}]&quot; aria-label=&quot;512 tokens: Prefill 2,721 tokens/s, Decode 110 tokens/s&quot; data-astro-cid-dwn4dnrm&gt;&lt;/rect&gt;&lt;rect class=&quot;line-chart__hit&quot; x=&quot;404.15384615384613&quot; y=&quot;20&quot; width=&quot;67.84615384615387&quot; height=&quot;264&quot; role=&quot;img&quot; tabindex=&quot;0&quot; data-x=&quot;452.6153846153846&quot; data-title=&quot;2,048 tokens&quot; data-rows=&quot;[{&amp;quot;slot&amp;quot;:1,&amp;quot;name&amp;quot;:&amp;quot;Prefill&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;2,881 tokens/s&amp;quot;},{&amp;quot;slot&amp;quot;:2,&amp;quot;name&amp;quot;:&amp;quot;Decode&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;99 tokens/s&amp;quot;}]&quot; aria-label=&quot;2,048 tokens: Prefill 2,881 tokens/s, Decode 99 tokens/s&quot; data-astro-cid-dwn4dnrm&gt;&lt;/rect&gt;&lt;/svg&gt;&lt;div class=&quot;line-chart__tooltip&quot; hidden data-astro-cid-dwn4dnrm&gt;&lt;/div&gt;&lt;template class=&quot;line-chart__keys&quot; data-astro-cid-dwn4dnrm&gt;&lt;svg class=&quot;line-chart__key line-chart__series-1&quot; data-slot=&quot;1&quot; viewBox=&quot;0 0 24 12&quot; width=&quot;24&quot; height=&quot;12&quot; aria-hidden=&quot;true&quot; data-astro-cid-dwn4dnrm&gt;&lt;line x1=&quot;0&quot; x2=&quot;24&quot; y1=&quot;6&quot; y2=&quot;6&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;path d=&quot;M7.5,6 a4.5,4.5 0 1,0 9,0 a4.5,4.5 0 1,0 -9,0&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;/svg&gt;&lt;svg class=&quot;line-chart__key line-chart__series-2&quot; data-slot=&quot;2&quot; viewBox=&quot;0 0 24 12&quot; width=&quot;24&quot; height=&quot;12&quot; aria-hidden=&quot;true&quot; data-astro-cid-dwn4dnrm&gt;&lt;line x1=&quot;0&quot; x2=&quot;24&quot; y1=&quot;6&quot; y2=&quot;6&quot; data-astro-cid-dwn4dnrm&gt;&lt;/line&gt;&lt;path d=&quot;M8,2 h8 v8 h-8 Z&quot; data-astro-cid-dwn4dnrm&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/template&gt;&lt;/div&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-dwn4dnrm&gt;Prefill throughput increases from 540 to 2,880 tokens/s as the prompt length increases. In contrast, decode throughput remains relatively flat at about 100 tokens/s throughout this range.&lt;/figcaption&gt;&lt;/figure&gt;&lt;script type=&quot;module&quot; src=&quot;https://bryananthonio.com/opt/buildhome/repo/src/components/LineChart.astro?astro&amp;type=script&amp;index=0&amp;lang.ts&quot;&gt;&lt;/script&gt;
&lt;p&gt;There are of course some caveats with this measurement:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;My batch size is just 1. In production settings, it’s possible to batch multiple requests concurrently to increase decode throughput so the gap between prefill and decode throughput observed here would be much smaller.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;transformers&lt;/code&gt; library isn’t exactly an optimized inference engine so these numbers have to be taken with a grain of salt.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-this-all-means&quot;&gt;What This All Means&lt;a href=&quot;#what-this-all-means&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let’s think about this in the scenario where a user submits a 128-token prompt and gets a 200-token answer. Output generation will start after about 68 ms and finish after about 1.9 s (68 + 199 × 9.09). Here, the decode phase takes roughly 96% of the wait. This means that for a typical chat like this the answer’s length sets the wait time, not how long the question is.&lt;/p&gt;
&lt;figure class=&quot;figure&quot; style=&quot;margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;svg version=&quot;1.1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 819.6 260.1311033604684&quot; class=&quot;diagram&quot; role=&quot;img&quot; aria-label=&quot;A horizontal bar drawn to scale for a 128-token prompt and a 200-token answer. Prefill is a thin 68 ms sliver at the left. Decode, 199 tokens at 9.09 ms each, about 1.81 s, fills the rest of the bar, which ends at 1.88 s.&quot;&gt;&lt;!-- svg-source:excalidraw --&gt;&lt;defs&gt;&lt;style class=&quot;style-fonts&quot;&gt;
      @font-face { font-family: Excalifont-1f4tm5g; src: url(data:font/woff2;base64,d09GMgABAAAAABUcAA4AAAAAJOgAABTGAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGiIbhlYcgg4GYACBHBEICrZcqCMLSgABNgIkA4EQBCAFgxgHIBtuHCMDwcYBBFB8Q7J/Jti2CtY3YRhjbONg8KJFKT71XwYI8zkDCyLD+yMkmR2eTv337pJccqGKuFzJxIWSFFyJVRiIj2pC25n/u5PE8/h77tz7vlIEU5VuQtEVMzN/E4VGiEn15muzPnh7g7e3qiLSMxlJQmfU+osIhJHT+uUKV7iWIJEWJNLpMZKM5/+xHvFfwMppef7f2xOCEY+SJKhTJ23aB93uX2u/yuISimizkMSmk/rN/kXe7FdHdEluidBMqlhIJk3sDrVG1xKgEYmJWqiU0FA35q/8BBZ/AackxZYq0TkiI499uzKAAKBBJYBAEXYR6ZxACgRvCgCcDy3C8ZSQCiTXjsYaILk3llYDyVrYXAckCAAAeUil562xDpCp6gMAIa1Daoj8iSBDsg0BQLYZRseC4J/lTWocN1MZux9XC7DbaV0kWx852fFEK6+fQ+bEajdMOBAMMaGI6BiY2Di4eATEJKScxA6GkCOqhYik0JWkjFBABKjEKWCO4xgjo6EoAWWIUFk8RLAdSKxEAJMjjJInZgpKTPAHVCSOlSDmw9VVQC7oFNUoxf2doZefR+xJUgDAsEiR6IhtEGKHEntQoh3awJWIYmo+KAhQTDSEG+W8YIaFOitFpTVANhoiuvrro7wQyF6NJYzzPx3g+RRmEbOWI+rWLoAGzXzsG3hiciYTyWYS/6wPZWMiAWJSwO2MfCQcUkpYUfEwbhEwGZsmoYY3JiMHxPCBuNkE4NEdANRUC4mTbRmhqZkfscmd2UwAxM8GXvj5AilRaxrPliRwGMhcRFv/NmKtlNY9AL97B/3QdJchh5uEZ3pEHoLpALAGwQwyXvwECWOUolCZSvWatbn4Uwiqe/IRKISFiv2rNX5VPo/b5bTrtthsow3WWysOhEhwSrmqQAw1DwIoBQB/s9vEMgp3MuTCsQwZyOBJBlltHmYUenmyhx0HohB6Z9Z5OgQLnXx4PJ7BKVTuzwuMYLjx7BKHBHWCQezkzgzU5iT7OeMCZ362RYFzDVN9JFq9QGH9oK+wgCHmB8pYdiAhLnT5El/C+OMD2DbS5kgJvmWy8znZhmQawWHJPYPjGZJwmuphM671lfHKdpl4BHpnBn9J23TrGkLrO3tFUS0MRgghsmDKz2Xn57eZxnn7eWFczz/YeibZJ/ARTCtcy8pX3/7FRjYVfdGqWf+riSF/Ug/vVS8rrFBZoDDGmOw0iCcImlbSvOVQD2cjZVR86dyGBluMr9c27p+zwzQclFyAMcY2ppiC8FZl68PF82+3ZadFyNCbdNeW3CtvBsubHGRwmjWipyWybKeSHc9Qnmv1O3pzhmbnqES3kH+94UACb05PRtvj/ftv5x+M4mZ4IjyRSnZztPeysddiTAmQVbUUke1nitOAmeycVuetqFsy6yH4LMwfI6FtuwBrS7gkYDg/vX3amd6dHJxkS1zjf8nzY1t5rSiMcbGfPW9snyXEFcKHaZ1rXLu8WJnLjj68e3V1ifO2k7CEjYNq0PNH8QzFfBkPBpsl8juc/MVuPs739guF3kN5299Qi2/Z3G0SwU4EnbslgSnR1SWyWecaIK2nmeBuDpZ5O/uQoQyiPh0h5R8D8EJ3hxdNQmCUQdlgrxW/ipEdS1TKNRzy3Pgv4a1qr6gYb87gGykrtJ2jx68OjYqVB4Uxz1ot4nUEsyyYrtCJQY91kRVK0A9qNI5TiWsY/NbKdDpC12rB7YYc7SYDgDuCObhIR8r6CpOdHbgDsBvz7kgPD/n7WTKX1QiqaiIjG9EtPxfFXrGTjhWE8nyrEsgOhBHM/q9nB7Nd2B1kbKuHAQ85P/HZZ0ZhsIXCxEovv/dB3pYI9LJSpmfQK5kA6LfEwugX1Y12jEZVfxH9WYPIkQyF68QjLtg8c4PX01MP6xhg1yutHiQYu+DOpaRQvTYvUZjlaw1jNMubdoyBxXN4WXakJN9Hqe/1LaVfLFaYshg3RyICYASJTjI4gRBjEJ6iC3kI+e3Q/S2ozz98ULP6KOT18MXmgcHB4Z/t1J6h/JMto//gw0VxMrk5bHleyQV2SFEP0RlI3xwwOx70vo4y1SBDwTyT6vY+ivy8KhaFPs3KSs2osY2grwSFGKjkJvR2a6bm8pJQPupNMugBLNnNe+3f6vyrh+3OXdlRCgPHUv4ZN9aLTwtDSeCjbPca/yyU7G+arrCWfTn9Vk2+TMrCnRN1foq3cyOSCYkcR04W1aBWtm8Mlks6dfzFo1ykKK/54wabKu2e0f91vMLmij6FhHjQ9FzgNukIxSfsEM24xncnyjJPW2RQx+VoOZpJdl0oCZ2sbOLwEo6fobhXDW43CCPgC8n7pAPOh/tGiOY58qvBSVI2u94aNDuugK3BpduFym6zJPtQTED3oCF4AQh+8VWmEzWBQ21wKm/TEbIkCfNSp55WtmosCETWE//Uy4sKayRHzSwre4JO9Oh3Xj1/XdwoauvFnkB2WiaBOhFWORe8rNstE4D1tEJ9UfzgMYRld/eD2rhyuxEdFMZF9bI6bV3V3W635uIQxb/q9Oi66Et4YDkAvAdiIwzCGFFRlCjgGv6LYSUo30N7ZBxJ9vuT4yW3+YEYFIpVoaI1hk7ZE8yOwC5fVth4rIxPs2hCypmgm13BAzjUH89ube00qKXcNm7LTvZxdoRrOA0fG05R1Kif8gXnkg1CgAfgRgm7XYFMYHQBY0opyttWbhUJRPnHt953d5n+VuvwL9L8A8nkAte90uCSc7hojx5/dkPzzt48GV26vrL4cvvsUDDBQLJjJKXLy5sAn+VWy8TDLpeR7HuVds+FVgOVRZAQwdQNFiiP3ifTabQtOyoLUJyjEFnGR72VHwvDiZygvrjusv0Qp2EIsAtvZq1MMLWH+bnZX2b76LVeNd8b7PRRCOwUJSrBtK+szL9UxPHJYZlksJFsJydPTpcGIEw5sE+AZFPLPEgilQGU9QMGDUbK5VWwOXjxFU51O7QpGh3z19crFcsXdxO1mBevNYSC4O0gMhtfVpTgOuV86RDPtgInZVJWxw+KKjvOPmQsUoZZvWAN35Betfm/Yr+Cb4Ankv1OtPaPEzWZy3PZasalYdk0jKnClF6ZqN48nWZ2gTvQSuDeZTIffzmXTDKUHfXbvfzSw/xYblivZWciP32fdOFEA5tcttRwiooV9qUvNAESlI9k8jeK2kcukiRsJQJ+80KAdURrvKiyhIVRRKi44P0BqoiMKfIjqJuy7CRjZZzMZYdAAWCAXxxIJYJgkqEzeJB4pJkHwEjGMpAzcHl4T01O5go1o9ZaPWxKucEHu7oDOlmyPVeL1vDKiS9sG8T1UKplcKXugzbX4O9v2+Vn+G3DRxWOP7B+D/O3Dwoy6hohzg2blED0dSBQuS4hzq70402RfNSVV8/iNvXC3OCKyVrAApXn2Cs3OApj3gp5H7dhdwq6e4uOZd0uG79QUOAy/7QRzFKP/61WIYiHHcUPNIuSqfn5xwBp6UNi7HVlVV7EdCouzPFs6wperqeytc2pJqgmE23qlJ9m+UW51WPS1l/8i7an5jnH/wfWufVIFN7ep5cOLR4fMhMbiaVe/XoMiNjcp9AIDzsh6S5Ukkf5HKOXx6Qmckv3rs8Tojztg/biLyWLiHigz0LsOmYlJFPRE7EdBDCHvPRFBn9zkHknsnh+33SweNY33ZwHr6Q9a349szBXCQVQDf1lNp02YJ/au7AxfDhdFnprx+MTH0qzxhRG7HV/eeWVLqStjrhgaY4T9wHTr24+YRGRdXLuQjOvmJ6ANFrBczl3TbHIuBPtvxBa0l3I/jnXPUf1nmU02O0O5HRl+4FXprhP7W1C8amzaRFM3WeCyB0pptISCQRk5MBFNm1ZbxIeo6+o/jhFbLPznroqdltjiS4sQimdeNnl93/C6llxFHFR84r4Qfm1ool2Pii6752Z5mxzBOF8PZxs/PiRN+OAgzVnvCnKt8yUPmGZr0t4rFRJIW7a6lzqm12jTefb3x3xoap0NmVzxAvHc27m1ji/LHhp8b7nxqdWt5YT2/jB1u0WqtX/HFAKwF7ZSWAUORr1wal8xNCSnHYLmLlxuf2zcuosJbLwCfjAxdd93Poeh4cOaHO/egsx6pomy9tHj4T18XVpfsJrZybqVMkFsdi/nHaTmQexng0+CtvDnPGH5yAJfm0zFxF+e8n1yvF+jNXnlkjOH2cv7IXXkDJ6GRbvKq9eLF1+APzkFnTBTsdbFKivDgB7VpcEfunZtj1XysSb8H9mZsex0irwvYrZcRiZ9O8spAaSBB8I7Dhz8YXWnbna/W++OsLKChnAbFe/JKiKdOVmgC7yxOXivGfjw1n0J8WYBzR1IIwpTn7b3BOkTmuZmu2UTRQ4wXJpvFYAYuWvyXumlpwbRLalNNiGhkhEdsuX7HLcjOBC8lEL5TgEygRhistg6HUpte+TQTZdw5huTTPbzErtnvzLllvDlwtO3NE5l7BnM1Vt5uddv+NlUX4ccxpGtPrGZRRPzalzwIxB+6EuCmYT9NzYujgNxb4flHHzRkA9nh0nnZvwALG77ukICQmU0Hek/ZJG+oVUyy1SzfMwj+QPxYMM86e7Z0Qlo/SqOf6ihHAhkNFJ0fELEDAwndPEvqt9f6xVOAVQBbM1YitbtdC7j6C7Hai8BWiVIgSXgqOkuzqRZk7VpeQ0vjt0N+3xLFEsiGEmY+zKlSF/5ebMFSYJFTmiOYxkwldA3F+0znxMFbBeoHMxNKTq89gX4ejSGLQdC4gj+C/KHKonVEpALv2wxPqmmHX1C8lcN2+iIN/lkfaUvOz/aPWpw+jw+9DhJY8XvHYN2Ifow/vf+n1NhIPVxMYATNSeB9ZwfM9c2fKZZI3EKMmTOpNot+Nco1AM7cBIPoKSJA5rJBKx+5VNuiwApKhp1SrnRJW20DvDQaqNc3oaiPFqX4jr6piW2GOnAvrrd8iFHzM8I5fwexALJbXUH7ikH1eVVUVZE0Rvig83WKM1BGsWKuD719w4w1hblDCJO+HUVUOjWsG7MzVFYE0m6N2MyuwnbcSm9zrcEbHf85/bwOf96VfeAwbwBZ+mTXTr5HMM4zvXZvHHKZ1Z88OGYLVAbJ0pChhqWtSYgnStmbThI4dY4ztw62XhcVW3Hbb6hAU20pPIMcMnY0dzP55dFuUB3kKpgd+tEYpbyURyiMM4KnYtEcw7cuezowxfM7GQy06xLdFO9+tUdsBQdgZ7muivfkDfIcVSsExM4vhYc+Jffu2osyPICm+IeLtVgbXSy8Wm2TqfB2tobFI5SjJ/OtqccmwSeHYqsiA71vHo5v/Hmhj6K0irS2GJwn03RMgkJM/3gqfp1LQfaLUrh9yBOQaiNdCMz/XZqSIiyaRKdY4S9saACVzhqRtwjIiO4dTTEoT6j2eHwMEZGUdELTh9BH0PtiQWoibZ1j565ynQCfoLVL8hb8MEtAU5+YG/fL2b6bb7PS+4iRtpv31p6+K4w+QsmHHIcZWG1nY09WfdppkjRiPFFF+7sZ39a/Kv/gnPFyHXojOoTYgvCErwz58FhhBczCkr5+hyNVtihAoSWa4LDrRxvZIaeoz7nWOpSxyJiSiFZXglO1blwQiGhsHzyvRRyitaeV/V/OpBJ0sax4oegZInsgpwjeBeeTD2K6Gyyr1yi851eXTc9OhT/ubN0bVjHN6X1vvI4l3TBbEhZqquuxwqUr08nU/sZacuMJJdPKgQr301WA1+aayvv5J+gO1iwdeSQZkE2nIpxnGvznBNdGxRi5NPzHfa2Bd1UYortoBfCRRkqhyrCitZuCJo6cEIKT82xpy6Rf2BGs/8fQyjE8xTLmIYecQBBU3or6DDvm7h6tk5E/N5RIpkTgyPts4yZgbOVc8EjQL67NDw1bf/j2SgsagAMQKdiXteHeL31LF/QEt1+GNRYOuI3xL1aRJdoLloWEqqfZirshMROfrad8jYGsO3TSFaX21v+Xrx3JSPL4fSXmcG60fewdgN8rFjAS/k/xz18E41tUvDi0k3HL1EcRwounrLEMrNpB0OKd9dIej3ckHry9S+VwZ0/6/eCCYUfjSzBoR5ZmYInrxV8qM0wOdtypBusqmtcrSF0MLbcvj/61ZtQVdwYkqDbCw4yUBl1LP62xXLpozyD4e/ICQNne2hYGeMx1rZqXmOY8zEJe6GjHhdY9kdDRprkMeTPDiOJ7xKf/G1D4beg7+nvS7Uzt9iaQiIU+ijzP/c5rdJbWEVwLDD9kndFNBwlq+e6qZoM6yaKL8unYj4zjrE4gZqM4vYNynYRnpdc2/osQwqWkxvxV/igtlX+iovoxLnPxwefycgj+Sp50ozSUWz2eDm9u3FoSg1I34K9i+TM5k9VQXq2S8dsvhuFt28Hi3POP2VzDt+motnnCfZzMdTmHx+7LhNV4OoC8HiyoEr2TmKx356MsqS+Sig/24mlEm0tMr1H4z5+8gIipoCsv62pf1lxcJP3yVp0qx2iGCY24RJ3Vt+iNgPasQqPhtcvCvPuoRgaDW4Pr1s0LVAg2D4H2/AgesZiVSeTb3HNL8UDxM0bV8Aa7Ko6U60327CVPSs6dM3Z+tEkgnMbzxZkkKyvwz5b4BrnX9r6J2k8nZ7AbnNbtoVOJL178IF9hDavdb7rKyQeM2YBaX1v/R4KH29Ly0/tmR8yGY2iOH8Bvd8O73ymRE/sow+AgC43eFuBACAO71/18/Jq4vwZAkFABmiQDpG5RsOcJ7fegGBr2oUMWU+AAM0WYUbDn2Xi8hGgu7folaRdTznjt5cC++QgW7QAL176aqlySd0HnncKibNa5+9wyrfCLdBQJeJKAOENpNPWis4v/CtM9e6gd2B87KGw6BBNQxZOn+YKbJdoMpFQMrs1i4CQAv1MdlQcKG0gBrDIRDPwRAEw7YQlLupIQQqRSFEUVRAEr4AmLQrVqhGpTKj1Gk2gk2pci0TVqhR+svQqAniSWUZX158doZwFsw61Ktgbor8wS/AVZFf38HcyOOQtScra2RnkcSdK7WfdM4EQxjqdQCoXDJVaLYLK+ZGpuBzQcANRbCrTBVKllmhNl4CuqF7DZk6bXqB0kCl0CrWEl4d82P9XIgAAA==); }&lt;/style&gt;&lt;/defs&gt;&lt;g transform=&quot;translate(23.200000000000003 10) rotate(0 199.7270050048828 13.75)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;19.381999999999998&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;22px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;128-token prompt, 200-token answer&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(23.200000000000003 70.13110336046839) rotate(0 13.782918395942438 30)&quot;&gt;&lt;path d=&quot;M6.89 0 C12.07 0.45, 12.34 0.06, 20.67 0 C24.77 2.53, 30.08 1.53, 27.57 6.89 C29.93 14.58, 23.58 29.79, 27.57 53.11 C28.75 55.91, 23.41 63.37, 20.67 60 C15.51 61.24, 13.87 61.88, 6.89 60 C-0.6 59.77, -2.75 59.06, 0 53.11 C1.72 45.45, 0.18 34.48, 0 6.89 C3.44 5.38, -0.79 -2.57, 6.89 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#a5d8ff&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M6.89 0 C11.57 -1.43, 18.35 1.06, 20.67 0 M6.89 0 C10.16 0.36, 14.94 -0.09, 20.67 0 M20.67 0 C24.3 1.14, 26.09 1.41, 27.57 6.89 M20.67 0 C26.9 -1.03, 25.48 3.47, 27.57 6.89 M27.57 6.89 C27.53 21.05, 26.23 38, 27.57 53.11 M27.57 6.89 C28.16 16.86, 28.04 27.43, 27.57 53.11 M27.57 53.11 C26.97 56.86, 27.25 60.35, 20.67 60 M27.57 53.11 C27.31 59.85, 26.45 62.16, 20.67 60 M20.67 60 C16.95 58.85, 13.35 60.3, 6.89 60 M20.67 60 C17.02 60.59, 14.13 59.56, 6.89 60 M6.89 60 C2.86 58.65, 0.5 57.88, 0 53.11 M6.89 60 C0.84 61.24, 1.4 59.73, 0 53.11 M0 53.11 C1.61 41.74, -0.34 29.91, 0 6.89 M0 53.11 C0.06 41.21, 0.17 30.6, 0 6.89 M0 6.89 C-1.73 3.29, 3.88 0.2, 6.89 0 M0 6.89 C1.75 0.66, 4.34 -1.37, 6.89 0&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot; transform=&quot;translate(55.40183847385151 70.19991064941718) rotate(0 366.21708160405757 30)&quot;&gt;&lt;path d=&quot;M15 0 C228.7 3.76, 445.79 3.48, 717.43 0 C724.51 -3.58, 732.88 6.12, 732.43 15 C729.03 29.75, 731.78 35.84, 732.43 45 C730.81 56.05, 727.17 60.88, 717.43 60 C512.43 57.46, 306.77 59.73, 15 60 C7.08 56.75, 0.35 51.71, 0 45 C0.84 38.7, 3.74 26.14, 0 15 C-0.99 4.95, 8.59 0.51, 15 0&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; fill=&quot;#ffec99&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M15 0 C221.57 2.94, 428.74 2.15, 717.43 0 M15 0 C165.46 2.6, 315.45 2.5, 717.43 0 M717.43 0 C729.06 0.11, 733.54 5.28, 732.43 15 M717.43 0 C725.46 -1.2, 731.49 3.58, 732.43 15 M732.43 15 C732.98 19.57, 732.65 28.86, 732.43 45 M732.43 15 C732.87 24.4, 733.12 34.92, 732.43 45 M732.43 45 C733.92 54.55, 726.67 61.06, 717.43 60 M732.43 45 C730.74 55.73, 725.63 60.38, 717.43 60 M717.43 60 C472.02 61.46, 226.85 60.39, 15 60 M717.43 60 C514.42 59.72, 312.15 59.25, 15 60 M15 60 C5.25 59.2, -1.07 54.79, 0 45 M15 60 C6.67 59.7, -2.18 56.54, 0 45 M0 45 C1.58 38.59, -0.67 29.24, 0 15 M0 45 C-0.46 34.15, 1.27 21.36, 0 15 M0 15 C-1.96 4.27, 6.4 -1.65, 15 0 M0 15 C-0.72 3.57, 6.94 2.09, 15 0&quot; stroke=&quot;#f08c00&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(218.11892007790902 87.69991064941718) rotate(0 203.50000000000003 12.5)&quot;&gt;&lt;text x=&quot;203.50000000000003&quot; y=&quot;17.619999999999997&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;20px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Decode: 199 tokens × 9.09 ms ≈ 1.81 s&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(36.98291839594244 168.1311033604684) rotate(0 -0.039368847648166394 -15.058895695209515)&quot;&gt;&lt;path d=&quot;M0.24 0.36 C0.24 -4.78, 0.2 -25.47, 0.14 -30.48 M-0.3 0.07 C-0.37 -5.05, -0.18 -24.92, -0.23 -30.03&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(36.98291839594244 168.1311033604684) rotate(0 -0.039368847648166394 -15.058895695209515)&quot;&gt;&lt;path d=&quot;M4.89 -15.93 C3.7 -20.23, 0.92 -25.12, -0.23 -30.03 M4.89 -15.93 C3.03 -20.65, 1.44 -25.69, -0.23 -30.03&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;g transform=&quot;translate(36.98291839594244 168.1311033604684) rotate(0 -0.039368847648166394 -15.058895695209515)&quot;&gt;&lt;path d=&quot;M-5.37 -15.94 C-3.28 -20.26, -2.79 -25.14, -0.23 -30.03 M-5.37 -15.94 C-3.54 -20.7, -1.46 -25.73, -0.23 -30.03&quot; stroke=&quot;#1971c2&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(23.200000000000003 172.1311033604684) rotate(0 69.30000000000001 11.25)&quot;&gt;&lt;text x=&quot;0&quot; y=&quot;15.858&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;18px&quot; fill=&quot;#1971c2&quot; text-anchor=&quot;start&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;Prefill: 68 ms&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(23.200000000000003 214.1311033604684) rotate(0 0.16808226351338718 5)&quot;&gt;&lt;path d=&quot;M0 0 C0.46 2.74, 0.45 6.88, 0 10 M0 0 C0.02 3.4, -0.01 6.48, 0 10&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(10 230.1311033604684) rotate(0 13.200000000000003 10)&quot;&gt;&lt;text x=&quot;13.200000000000001&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;0 s&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(225.6517978252415 214.1311033604684) rotate(0 -0.05214235116974919 5)&quot;&gt;&lt;path d=&quot;M0 0 C-0.02 2.82, -0.22 5.75, 0 10 M0 0 C-0.14 3.77, -0.07 7.66, 0 10&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(203.6517978252415 230.1311033604684) rotate(0 21.999999999999986 10)&quot;&gt;&lt;text x=&quot;22&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;0.5 s&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(428.10359565048293 214.1311033604684) rotate(0 0.0660434348513661 5)&quot;&gt;&lt;path d=&quot;M0 0 C0.42 3.7, 0.04 7.49, 0 10 M0 0 C-0.23 3.75, 0.26 8, 0 10&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(414.90359565048294 230.1311033604684) rotate(0 13.199999999999989 10)&quot;&gt;&lt;text x=&quot;13.200000000000001&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1 s&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(630.5553934757245 214.1311033604684) rotate(0 0.030862571878230938 5)&quot;&gt;&lt;path d=&quot;M0 0 C0.06 1.68, 0.09 3.88, 0 10 M0 0 C-0.09 3.16, 0.21 6.36, 0 10&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(608.5553934757245 230.1311033604684) rotate(0 22 10)&quot;&gt;&lt;text x=&quot;22&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#555&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1.5 s&lt;/text&gt;&lt;/g&gt;&lt;g stroke-linecap=&quot;round&quot;&gt;&lt;g transform=&quot;translate(23.200000000000003 214.1311033604684) rotate(0 380 0.41065527710809135)&quot;&gt;&lt;path d=&quot;M0 0 C203.54 0.71, 406.15 -0.44, 760 0 M0 0 C229.47 1.18, 457.79 1.28, 760 0&quot; stroke=&quot;#555&quot; stroke-width=&quot;2&quot; fill=&quot;none&quot;&gt;&lt;/path&gt;&lt;/g&gt;&lt;/g&gt;&lt;mask&gt;&lt;/mask&gt;&lt;g transform=&quot;translate(756.8000000000001 230.1311033604684) rotate(0 26.399999999999977 10)&quot;&gt;&lt;text x=&quot;26.400000000000002&quot; y=&quot;14.096&quot; font-family=&quot;Excalifont-1f4tm5g, Xiaolai, sans-serif, Segoe UI Emoji&quot; font-size=&quot;16px&quot; fill=&quot;#1e1e1e&quot; text-anchor=&quot;middle&quot; style=&quot;white-space: pre;&quot; direction=&quot;ltr&quot; dominant-baseline=&quot;alphabetic&quot;&gt;1.88 s&lt;/text&gt;&lt;/g&gt;&lt;/svg&gt;&lt;/div&gt;&lt;/figure&gt;
&lt;p&gt;I now have a clearer perspective on why LLM APIs charge more for output tokens. There’s a cost to operating a GPU every second, which includes things like hardware costs and the price of electricity.&lt;/p&gt;
&lt;p&gt;LLM APIs bill per token, so what ultimately matters is the time spent per token. The prefill phase takes much less time per token since all of the input tokens are processed at once, unlike the decode process which generates one token at a time and requires reading all of the model weights each time. On my laptop, I measured that for an input prompt of 2,048 tokens, an output token uses 29× more time than an input token.&lt;/p&gt;
&lt;p&gt;So, charging more per output token makes sense from that perspective. The ~5:1 ratio between the pricing of output and input tokens by model providers leads me to believe that in production settings this 29× gap I observed would be smaller. But the point still stands.&lt;/p&gt;
&lt;h2 id=&quot;one-question-to-resolve-next-time&quot;&gt;One Question To Resolve Next Time&lt;a href=&quot;#one-question-to-resolve-next-time&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One question I’d like to resolve next is: why is the TPOT roughly 10 ms per output token? All I know right now is that it has something to do with the fact that the process of generating one token is largely bound by memory bandwidth.&lt;/p&gt;</content:encoded></item><item><title>Building a Better NAS for My Homelab</title><link>https://bryananthonio.com/blog/building-a-better-nas-for-my-homelab/</link><guid isPermaLink="true">https://bryananthonio.com/blog/building-a-better-nas-for-my-homelab/</guid><description>How I replaced my old home server with a compact, power-efficient NAS to handle my future storage needs.</description><pubDate>Sun, 04 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;After setting up my first home server &lt;a href=&quot;https://bryananthonio.com/blog/my-homelab-setup/&quot;&gt;using an old gaming PC&lt;/a&gt; earlier this year, I eventually realized that this machine wasn’t ideal for my needs. Its mid-tower case occupied too much space and only had room for two 3.5-inch hard drives. I wanted a more compact setup that would allow me to add more storage in the future.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&quot;https://bryananthonio.com/blog/sagittarius-8-bay-nas-first-impressions/&quot;&gt;my last post&lt;/a&gt;, I identified the case I chose for my new NAS build. I’ve since finished the build and the result is an 8-bay NAS with 16 GB of ECC memory that idles at 30–35 W for about $1,100 in new parts, including drives. Here’s how I put it together and some of the surprises I encountered along the way.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#parts-selection&quot;&gt;Parts Selection&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#watch-out-for-vendor-locked-ryzen-pro-cpus&quot;&gt;Watch Out for Vendor-Locked Ryzen PRO CPUs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#why-not-buy-a-prebuilt-nas&quot;&gt;Why Not Buy a Prebuilt NAS?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#building-my-nas&quot;&gt;Building my NAS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#what-im-running-on-my-nas&quot;&gt;What I’m Running on My NAS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#noise-and-thermals&quot;&gt;Noise and Thermals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#power-consumption&quot;&gt;Power Consumption&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#final-thoughts&quot;&gt;Final Thoughts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;parts-selection&quot;&gt;Parts Selection&lt;a href=&quot;#parts-selection&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;RAM&lt;/td&gt;
&lt;td&gt;2x &lt;a href=&quot;https://www.amazon.com/dp/B08J9PYP39&quot;&gt;Kingston KSM26ES8/8HD (8 GB each)&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;$208.98&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Motherboard&lt;/td&gt;
&lt;td&gt;ASRock B550M Pro4&lt;/td&gt;
&lt;td&gt;$87.99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;Ryzen 5 PRO 5650GE&lt;/td&gt;
&lt;td&gt;$137.50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CPU Cooler&lt;/td&gt;
&lt;td&gt;Noctua NH-L9a-AM4&lt;/td&gt;
&lt;td&gt;$49.95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case&lt;/td&gt;
&lt;td&gt;Sagittarius 8 Bay NAS Case&lt;/td&gt;
&lt;td&gt;$210.37&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage (HDD)&lt;/td&gt;
&lt;td&gt;2x &lt;a href=&quot;https://www.westerndigital.com/products/internal-drives/wd-red-plus-sata-3-5-hdd?sku=WD80EFPX&quot;&gt;Western Digital Red Plus 8 TB 3.5″&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;$343.98&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case Fan&lt;/td&gt;
&lt;td&gt;2x Arctic P12 PWM PST&lt;/td&gt;
&lt;td&gt;$21.76&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Drive Adapters&lt;/td&gt;
&lt;td&gt;2x &lt;a href=&quot;https://www.amazon.com/dp/B01LZWX6PD&quot;&gt;ORICO 2.5″ to 3.5″ SSD Adapter&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;$15.97&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SATA Adapters&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.amazon.com/dp/B09Y2SYDDF&quot;&gt;Right-angle SATA adapters&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;$13.98&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case Fan&lt;/td&gt;
&lt;td&gt;Noctua AF 120mm&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PSU&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://seasonic.com/prime-gold/&quot;&gt;SeaSonic PRIME Gold 650 W 80+ Gold&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage (SSD)&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.samsung.com/us/computing/memory-storage/solid-state-drives/ssd-850-evo-2-5-sata-iii-500gb-mz-75e500b-am/&quot;&gt;Samsung 850 Evo 500 GB 2.5″&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage (NVMe)&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.sandisk.com/en-gb/products/ssd/internal-ssd/wd-blue-sn550-nvme-ssd?sku=WDS500G2B0C-00PXH0&quot;&gt;Western Digital Blue SN550 500 GB M.2-2280&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Operating System&lt;/td&gt;
&lt;td&gt;TrueNAS Community Edition&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The prices listed here include shipping fees and taxes. In total, I paid $1,090.48, of which $746.50 was for components apart from the hard drives.&lt;/p&gt;
&lt;p&gt;I purchased the hard drives in December 2025 and other new components around April 2026. A few components, including the PSU and both SSDs, were reused from &lt;a href=&quot;https://bryananthonio.com/blog/my-homelab-setup/&quot;&gt;my previous build&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A few notes on my choices:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Motherboard:&lt;/strong&gt; I wanted &lt;a href=&quot;https://www.truenas.com/docs/scale/25.10/gettingstarted/scalehardwareguide/index.html#error-correcting-code-memory&quot;&gt;ECC support&lt;/a&gt; and at least two PCIe slots so I could add an HBA and 10GbE networking in the future. I also chose a DDR4-based motherboard since DDR5 memory is significantly more expensive in today’s market. I would have also preferred a motherboard with 8 SATA ports but the options I considered were all too expensive to justify.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RAM:&lt;/strong&gt; I started with 16 GB of ECC memory to have some headroom over &lt;a href=&quot;https://www.truenas.com/docs/scale/25.10/gettingstarted/scalehardwareguide/index.html#memory-sizing&quot;&gt;TrueNAS’s minimum recommendation of 8 GB&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CPU:&lt;/strong&gt; The Ryzen 5 PRO 5650GE supports ECC memory and has integrated graphics, so a dedicated GPU isn’t necessary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Case:&lt;/strong&gt; I chose the Sagittarius as it offers eight drive bays and ATX PSU support all in a compact 20-liter footprint. &lt;a href=&quot;https://bryananthonio.com/blog/sagittarius-8-bay-nas-first-impressions/&quot;&gt;I wrote more about the case in my review here.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CPU Cooler:&lt;/strong&gt; The case allows only 55 mm of clearance with an ATX PSU, so I needed the low-profile Noctua NH-L9a-AM4.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operating System:&lt;/strong&gt; I’ve stayed with TrueNAS Community Edition since it has been reliable and doesn’t require a paid license.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hard Drives:&lt;/strong&gt; In my storage layout two hard drives are mirrored, so only 8 TB are usable across these two.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;NVMe drive:&lt;/strong&gt; This is used as the boot drive.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SSD:&lt;/strong&gt; One app I’m self-hosting (Immich) has its Postgres DB stored on this drive to access data more quickly.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;watch-out-for-vendor-locked-ryzen-pro-cpus&quot;&gt;Watch Out for Vendor-Locked Ryzen PRO CPUs&lt;a href=&quot;#watch-out-for-vendor-locked-ryzen-pro-cpus&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Some Ryzen PRO CPUs are locked to certain motherboards. &lt;a href=&quot;https://www.servethehome.com/lenovo-vendor-locking-ryzen-cpus-with-amd-psb-the-video/&quot;&gt;CPUs pulled from Lenovo systems are one such example.&lt;/a&gt; This locking is done using what’s known as Platform Secure Boot (PSB), which blows a hardware fuse in the CPU to activate the lock. If, for instance, you try to run a locked Lenovo CPU on a motherboard by a different PC maker, it just won’t boot.&lt;/p&gt;
&lt;p&gt;When researching this, I found &lt;a href=&quot;https://www.servethehome.com/hp-amd-ryzen-pro-psb-status-tested-via-a-hp-elitedesk-805-g6-mini-asus-gigabyte/&quot;&gt;this blog post by ServeTheHome&lt;/a&gt; which noted that a chip pulled from an HP EliteDesk 805 G6 Mini wasn’t locked. This gave me the confidence to purchase a chip on eBay, which the seller claimed was pulled from an HP system. Fortunately, it worked on my ASRock motherboard.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/cpu_edited_1i8JP.jpg&quot; alt=&quot;Close-up of an AMD Ryzen 5 PRO 5650GE installed in the AM4 socket of an ASRock B550M Pro4 motherboard.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2880&quot; height=&quot;1920&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The Ryzen 5 PRO 5650GE pulled from an HP system, working just fine in my ASRock motherboard.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;why-not-buy-a-prebuilt-nas&quot;&gt;Why Not Buy a Prebuilt NAS?&lt;a href=&quot;#why-not-buy-a-prebuilt-nas&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before setting off to build my own NAS, I did consider just buying one off-the-shelf from either UGREEN or Synology. For example, &lt;a href=&quot;https://www.newegg.com/ugreen-dxp8800-plus-intel-core-i5-1235u-processor-diskless-system/p/N82E16822995005&quot;&gt;UGREEN offers this 8-bay NAS which currently retails for $1,369.99&lt;/a&gt;. It comes with 2 10GbE ports, but it lacks ECC RAM. &lt;a href=&quot;https://www.microcenter.com/product/714193/DiskStation_DS1825_8-Bay_Diskless_NAS&quot;&gt;Synology offers this 8-bay NAS for $1,549.99&lt;/a&gt;, which is more expensive than my build and only has half as much ECC RAM.&lt;/p&gt;
&lt;p&gt;For my build to more closely match the feature set of these two options, I’d need to purchase a few more components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;a href=&quot;https://www.amazon.com/dp/B000067SLY&quot;&gt;Molex splitter&lt;/a&gt; to power two additional drive bays (Currently $6.99). At the moment, my PSU is only powering 6 of the 8 drive bays.&lt;/li&gt;
&lt;li&gt;An &lt;a href=&quot;https://www.amazon.com/dp/B0FYWZMVL1&quot;&gt;HBA card&lt;/a&gt; to connect two more drives to the motherboard (Currently $71.88).&lt;/li&gt;
&lt;li&gt;A &lt;a href=&quot;https://www.amazon.com/dp/B08D71PVXG&quot;&gt;10GbE network card&lt;/a&gt; (Typically under $80).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This would add roughly $160 to the total build cost, before considering any taxes or shipping fees, which would be about $906, excluding hard drives.&lt;/p&gt;
&lt;p&gt;Of course, buying a prebuilt NAS does have its advantages. You’ll get a single warranty for the entire unit instead of separate warranties for each individual component. In addition, a prebuilt NAS would be fully functioning out of the box with a user-friendly OS that most people would likely find more intuitive than TrueNAS. You’d also likely get access to customer support if you ever run into technical issues.&lt;/p&gt;
&lt;p&gt;Despite these conveniences, the cost of buying one and my comfort with building computers were the driving factors in my decision to not buy a prebuilt NAS.&lt;/p&gt;
&lt;h2 id=&quot;building-my-nas&quot;&gt;Building my NAS&lt;a href=&quot;#building-my-nas&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First, I have to admit that the case was difficult to build in given how compact it is. Also, many of the screws included with the case were mixed together in one plastic bag. I spent the first several minutes sorting them.&lt;/p&gt;
&lt;p&gt;I first started installing the cables for the drive bays and the fans behind them. This involved connecting the SATA cables and some Molex connectors. The cable provided with my PSU only supports up to 3 Molex ports, so that means I’ll only be able to power 6 of the drive bays, which is fine for now.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sata_backplane_AqDMa.jpg&quot; alt=&quot;Interior of the NAS case showing labeled blue SATA cables and black power cables connected behind the eight drive bays, with two case fans below.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;5712&quot; height=&quot;4284&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The SATA and power cables connected behind the drive bays.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;From there, I switched over to the motherboard chamber. I found it tricky to slowly place the motherboard on the screw mounts since they’re not easy to see from above.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--portrait&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/mobo_chamber_1_ZgegA0.jpg&quot; alt=&quot;Top-down view of the ASRock B550M Pro4 motherboard installed in the NAS case with RAM sticks, a low-profile Noctua CPU cooler, and a case fan.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;4254&quot; height=&quot;5399&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The motherboard installed inside the case. You can see here that I forgot to install the I/O shield.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In such a tight space, I found it difficult to connect the SATA cables to the motherboard. I wish I had done this before putting in the motherboard.&lt;/p&gt;
&lt;p&gt;It was also here that I discovered there wouldn’t be enough room to place two fans in the motherboard chamber because of the SATA ports, which was an unpleasant surprise. However, this hasn’t been a huge issue since I’ve found that one intake fan provides enough cooling for the CPU and motherboard. I’ve since noticed that &lt;a href=&quot;https://youtu.be/Jr5MjhgPz_c?t=760&quot;&gt;Wolfgang was able to fit a second fan&lt;/a&gt; by pushing the SATA cables a bit further down.&lt;/p&gt;
&lt;p&gt;Shortly thereafter, I realized I forgot to put in the I/O shield, so I had to redo this part before mounting the PSU.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/psu_view_ZlmBBY.jpg&quot; alt=&quot;Interior of the NAS case showing the large SeaSonic power supply and its bundled cables above the motherboard.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;5481&quot; height=&quot;4284&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The power supply mounted above the motherboard.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;One concern I had was that the cables were having to bend almost 90 degrees to fit. I purchased SATA adapters to prevent this.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/mobo_chamber_2_sata_connectors_Z17LvXk.jpg&quot; alt=&quot;Close-up of four right-angle SATA adapters connected along the edge of the motherboard, with labeled blue SATA cables running across it.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;4032&quot; height=&quot;3024&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The right-angle SATA adapters connected to the motherboard.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Once all that was done, the next step was adding the storage drives. The Sagittarius case came with screws, rubber grommets, and mounting tabs to install on each hard drive. I assume these grommets are meant to dampen vibrations from the drives themselves. All in all, these were fairly easy to install.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/hdd_with_mount_tab_Z1SETvM.jpg&quot; alt=&quot;A 3.5-inch hard drive lying upside down on a wooden desk, with a perforated metal mounting tab screwed to one end and rubber-grommeted mounting screws along its side.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A hard drive fitted with a mounting tab and rubber-grommeted screws for the drive bays.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;I also wanted to mount a 2.5″ SSD, so I purchased a 2.5″ to 3.5″ drive adapter from ORICO to fit it in a 3.5″ bay. It didn’t make much sense to install the mounting tabs on the adapter, so I skipped that step.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/ssd_with_screw_mounts_sfQvf.jpg&quot; alt=&quot;A Samsung 2.5-inch SSD seated in a black 3.5-inch adapter bracket with a rubber-grommeted mounting screw on its side, resting on a wooden desk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A Samsung SSD in a 2.5-to-3.5-inch adapter bracket so it fits in the drive bays.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;With the drives all prepped, I slid them into their respective bays behind the removable front panel. I also left some space between the drives to maximize the airflow around them.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--portrait&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/mounted_drives_VG7z5.jpg&quot; alt=&quot;The NAS case with its perforated front panel removed and leaning against the side, revealing two hard drives with silver mounting tabs and the SSD adapter bracket installed in the drive bays, with the lower bays left empty.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1200&quot; height=&quot;1800&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The hard drives and SSD installed in the drive bays.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;what-im-running-on-my-nas&quot;&gt;What I’m Running on My NAS&lt;a href=&quot;#what-im-running-on-my-nas&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My previous home server hosted several self-hosted apps running within TrueNAS. I’ve scaled down on this and am now using this new machine primarily as a dedicated NAS.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--styled&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/truenas_dashboard_1rreX6.webp&quot; alt=&quot;TrueNAS Community Edition dashboard showing system information, CPU usage and temperature, and 15 GiB of ECC memory.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3024&quot; height=&quot;1808&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;The TrueNAS dashboard for my new NAS.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Right now, I only run these apps on it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://github.com/garethgeorge/backrest&quot;&gt;Backrest&lt;/a&gt;:&lt;/strong&gt; Backs up my data to a Backblaze B2 bucket every day that the NAS is turned on.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://github.com/analogj/scrutiny&quot;&gt;Scrutiny&lt;/a&gt;:&lt;/strong&gt; Monitors the health of my hard disks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://immich.app&quot;&gt;Immich&lt;/a&gt;:&lt;/strong&gt; Backs up photos from my iPhone.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I’ve since started hosting my other apps, such as &lt;a href=&quot;https://readeck.org/en/&quot;&gt;Readeck&lt;/a&gt; and &lt;a href=&quot;https://mealie.io&quot;&gt;Mealie&lt;/a&gt;, on an M1 Mac mini. I mainly did this to cut my power consumption as I don’t always have my NAS turned on, whereas the Mac mini runs 24/7, idling at about 5 W (more on that below).&lt;/p&gt;
&lt;h2 id=&quot;noise-and-thermals&quot;&gt;Noise and Thermals&lt;a href=&quot;#noise-and-thermals&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Thermals have been pretty good. I’ve found that the CPU temperature ranges from 32–40°C, averaging 34°C. The hard drive temperatures are between 25–34°C, averaging 31°C.&lt;/p&gt;
&lt;p&gt;As for noise, I mainly hear the sound of airflow, and the occasional clicking sound from the hard drives. The NAS sits in my home office and I haven’t found the noise to be very distracting while working.&lt;/p&gt;
&lt;h2 id=&quot;power-consumption&quot;&gt;Power Consumption&lt;a href=&quot;#power-consumption&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I bought a smart plug (Tapo P125M) to check the power consumption. Using this, I found that my previous home server was idling at 55–60 W, of which 15 W was due to my Nvidia GPU. My new build idles at 30–35 W. This is after enabling Global C-States in BIOS to allow the CPU to access lower power states.&lt;/p&gt;
&lt;p&gt;One thing to note is that my PSU isn’t efficient for such low power loads. &lt;a href=&quot;https://www.youtube.com/watch?v=TPSuCbS-4P0&quot;&gt;I went down a rabbit hole on this&lt;/a&gt; only to learn that it’s not worth purchasing a new PSU for this reason alone.&lt;/p&gt;
&lt;p&gt;I also explored the possibility of having the hard drives spin down to save power only to find that it wasn’t worth going down this path. This is because TrueNAS does SMART checks on the drives, which will regularly wake them up, defeating the purpose of spinning them down. The &lt;a href=&quot;https://www.truenas.com/docs/scale/25.10/gettingstarted/versionnotes/#drive-health-management-in-2510-and-beyond&quot;&gt;official documentation&lt;/a&gt; states that these checks occur every 90 minutes. As far as I can tell, TrueNAS doesn’t allow you to configure when these SMART checks occur.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;a href=&quot;#final-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This build, which I originally finished in April, wasn’t the most straightforward. One thing I’ve learned is that a single intake fan in the motherboard chamber is indeed enough for my NAS. If you’re building a gaming PC in this case or a compute-heavy home server you’d probably want to find a way to install a second fan. Adding an 8-port HBA card should help here by allowing you to connect eight drives to the card, freeing up the SATA ports on the motherboard to make room for the second fan.&lt;/p&gt;
&lt;p&gt;I also would have preferred getting a motherboard with 8 SATA ports and ECC support, if they were cheaper in today’s market, to more easily support all 8 drive bays in the case without needing another peripheral component.&lt;/p&gt;
&lt;p&gt;Even so, the end result has been well worth the effort for my needs compared to buying a prebuilt unit. Fortunately, I haven’t stumbled upon any deal-breaking technical issues with this, which I’m grateful for.&lt;/p&gt;</content:encoded></item><item><title>Jev by Example</title><link>https://bryananthonio.com/blog/jev-by-example/</link><guid isPermaLink="true">https://bryananthonio.com/blog/jev-by-example/</guid><description>Exploring Jev, TypeSafe AI’s new AI model that returns probabilities instead of prose.</description><pubDate>Sat, 26 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Recently, &lt;a href=&quot;https://typesafe.ai/blog/introducing-system-one-models-and-jev&quot;&gt;TypeSafe AI introduced a new frontier AI model called Jev&lt;/a&gt;. What’s unique about this model is that it specializes as a zero-shot classifier.&lt;/p&gt;
&lt;p&gt;In other words, you can give Jev a question and a fixed set of answer choices and it will tell you which answer choices it thinks are most likely to be correct. You get all of this out of the box without having to fine-tune or train the model on a representative set of examples.&lt;/p&gt;
&lt;p&gt;Jev is not designed to be a generative model, so it doesn’t output one text token at a time in formulating a response. This means that Jev is no good for tasks like summarizing an email or writing documentation. In contrast, it’s well-suited for tasks like triaging a support ticket, scoring the severity of a bug, or deciding whether an incoming request should be escalated to a human operator.&lt;/p&gt;
&lt;p&gt;While it’s true that other LLMs can perform these tasks, Jev is built to perform them more efficiently at a lower cost. TypeSafe currently charges &lt;a href=&quot;https://typesafe.ai/blog/introducing-system-one-models-and-jev&quot;&gt;$0.042 per million input tokens and nothing for output tokens&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To explore how Jev works, I vibecoded an app called &lt;a href=&quot;https://github.com/photon-collider/jevs-kitchen&quot;&gt;Jev’s Kitchen&lt;/a&gt; that answers questions about food. The examples shared in this post come from this app.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#how-jev-differs-from-a-typical-llm&quot;&gt;How Jev Differs From a Typical LLM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#examples&quot;&gt;Examples&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#choosing-a-dominant-taste&quot;&gt;Choosing a Dominant Taste&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#evaluating-ingredient-compatibility&quot;&gt;Evaluating Ingredient Compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#scoring-expected-heat&quot;&gt;Scoring Expected Heat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#try-it-yourself&quot;&gt;Try It Yourself&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#final-thoughts&quot;&gt;Final Thoughts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-jev-differs-from-a-typical-llm&quot;&gt;How Jev Differs From a Typical LLM&lt;a href=&quot;#how-jev-differs-from-a-typical-llm&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;TypeSafe AI refers to Jev as a &lt;a href=&quot;https://docs.typesafe.ai/introduction&quot;&gt;System One model&lt;/a&gt;. This name is a callback to Daniel Kahneman’s book &lt;a href=&quot;https://en.wikipedia.org/wiki/Thinking,_Fast_and_Slow&quot;&gt;&lt;em&gt;Thinking, Fast and Slow&lt;/em&gt;&lt;/a&gt;, which refers to System One thinking as the way the human brain performs fast, intuitive judgments as opposed to the slower, more deliberate System Two thinking. In this framing, Jev is positioned as a model for making snap decisions in contrast to other frontier models that are capable of performing multi-step reasoning.&lt;/p&gt;
&lt;p&gt;A Jev request has two main parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;state&lt;/code&gt;: contextual information the model should consider in outputting decisions&lt;/li&gt;
&lt;li&gt;&lt;code&gt;questions&lt;/code&gt;: a set of typed judgments the model should make conditioned upon the provided &lt;code&gt;state&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Requests support three kinds of &lt;code&gt;questions&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;choice&lt;/code&gt; selects the most plausible option from a fixed set of answer choices.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;noul&lt;/code&gt; returns the probability that a yes-or-no statement is true.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;score&lt;/code&gt; selects a rating on an ordered scale.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each response is constrained by the inputs we provide. For instance, a response to a &lt;code&gt;choice&lt;/code&gt; question will only include options provided in the initial input. This guarantees that the output shape is predictable, and the returned probabilities give our code a way to account for uncertainty.&lt;/p&gt;
&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;a href=&quot;#examples&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Jev’s Kitchen asks three questions about food, with each one corresponding to a different question type:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Question Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dominant Taste&lt;/td&gt;
&lt;td&gt;Which of the five basic tastes is most prominent?&lt;/td&gt;
&lt;td&gt;&lt;code&gt;choice&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ingredient Compatibility&lt;/td&gt;
&lt;td&gt;Is an ingredient typical in a conventional version of a dish?&lt;/td&gt;
&lt;td&gt;&lt;code&gt;noul&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spice Meter&lt;/td&gt;
&lt;td&gt;How spicy would a dish taste to a typical diner?&lt;/td&gt;
&lt;td&gt;&lt;code&gt;score&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;You can follow along by installing TypeSafe AI’s JavaScript SDK:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;sh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;npm&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; @typesafe-ai/sdk&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, set &lt;code&gt;TYPESAFE_API_KEY&lt;/code&gt; in your environment and create a client. The examples below will all use this &lt;code&gt;client&lt;/code&gt; and the helper functions &lt;code&gt;choice&lt;/code&gt;, &lt;code&gt;noul&lt;/code&gt;, and &lt;code&gt;score&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;TypeSafeClient&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;choice&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;noul&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;score&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; }&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; from&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;@typesafe-ai/sdk&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; client &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; TypeSafeClient&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;choosing-a-dominant-taste&quot;&gt;Choosing a Dominant Taste&lt;a href=&quot;#choosing-a-dominant-taste&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The first question asks Jev to decide on the dominant taste of a food or dish &lt;a href=&quot;https://en.wikipedia.org/wiki/Taste&quot;&gt;given the five basic tastes&lt;/a&gt;, which are sweet, salty, sour, bitter, and umami.&lt;/p&gt;
&lt;p&gt;Each answer choice comes with a criterion to provide Jev more context on which answer to select:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; tasteCriteria &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  Sweet&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;Sweet taste produced primarily by sugars and other compounds that activate sweet taste receptors (T1R2/T1R3), as in sugar, honey, and ripe fruit&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  Sour&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Sour taste produced by acids, especially hydrogen ions (H+), as in citrus, vinegar, fermented foods, and cultured dairy&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  Salty&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;Salty taste produced primarily by dissolved sodium ions and other salts, as in table salt, brines, and cured foods&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  Bitter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;Bitter taste produced by diverse compounds that activate bitter taste receptors (T2Rs), as in coffee, cocoa, bitter greens, and citrus peel&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  Umami&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;Umami taste produced primarily by glutamate and enhanced by nucleotides such as inosinate and guanylate, as in meat, mushrooms, tomatoes, aged cheese, and fermented foods&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using these criteria, we can ask Jev, for instance, about the dominant taste of soy sauce:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; client&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;systemOne&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;food_or_dish&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;soy sauce&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  questions&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    dominant_taste&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;choice&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;      &amp;quot;Which of the five basic tastes is most dominant in a typical version of `food_or_dish`? Judge perceived taste, not aroma, richness, temperature, or capsaicin heat. Choose the taste that leads even when several are present.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;      tasteCriteria&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, &lt;code&gt;choice&lt;/code&gt; restricts the answer to one of the five labels in &lt;code&gt;tasteCriteria&lt;/code&gt;. Jev also returns the probability assigned to each option, which Jev’s Kitchen displays as a ranked list.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; answer &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; response&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;answers&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;dominant_taste;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;answer&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;choice); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// &amp;quot;Salty&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;answer&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;confidence); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.58&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;answer&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;probabilities);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// { Salty: 0.67, Umami: 0.33, Sweet: 0, Sour: 0, Bitter: 0 }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Soy sauce is indeed known to have both saltiness and umami tastes. Since we’ve forced Jev to pick only one answer, it has decided to choose &lt;code&gt;Salty&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.typesafe.ai/confidence&quot;&gt;According to the official documentation&lt;/a&gt;, &lt;code&gt;confidence&lt;/code&gt; is a number between 0 and 1 that is computed based on the probability distribution of the answer choices. It’s a measure of how much the probability is concentrated on a single answer. Jev’s confidence is 0.58, which means it’s showing uncertainty regarding its answer choice as it has assigned a 33% probability to &lt;code&gt;Umami&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;evaluating-ingredient-compatibility&quot;&gt;Evaluating Ingredient Compatibility&lt;a href=&quot;#evaluating-ingredient-compatibility&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The second question asks Jev to judge whether an ingredient conventionally belongs in a dish:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; isTypical&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dish, ingredient) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; client&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;systemOne&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    state&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;dish&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;ingredient&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    questions&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;      is_typical&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;noul&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;Is `ingredient` typical or appropriate in a conventional version of `dish`? Judge common convention, not whether the ingredient is objectively authentic or whether a creative variation could taste good.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;          true&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;The ingredient is commonly expected, conventional, or an appropriate standard variation for the dish&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;          false&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;The ingredient is unusual for a conventional version of the dish&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;      ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; response&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;answers&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;is_typical&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;noul;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we use &lt;code&gt;noul&lt;/code&gt; to have Jev provide the probability that an ingredient is conventionally used to prepare &lt;a href=&quot;https://en.wikipedia.org/wiki/Carbonara&quot;&gt;carbonara, an Italian dish&lt;/a&gt;. Let’s try three ingredients:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; isTypical&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;carbonara&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;eggs&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.98&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; isTypical&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;carbonara&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pineapple&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.02&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; isTypical&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;carbonara&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;heavy cream&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.35&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you’d expect, Jev assigns a 98% probability to eggs and just 2% to pineapple. What’s interesting here is that heavy cream lands in between these extremes at 35%. My hunch is that this reflects the fact that some modern recipes recommend adding heavy cream while older recipes typically leave it out.&lt;/p&gt;
&lt;h3 id=&quot;scoring-expected-heat&quot;&gt;Scoring Expected Heat&lt;a href=&quot;#scoring-expected-heat&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In the final question, we ask Jev to rate how spicy a dish is given its ingredients. Here, the criteria define five levels ranging from no perceptible heat to extremely hot:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; spiceCriteria &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;No perceptible heat for a typical diner; no spicy ingredients&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;Mild; gentle warmth from black pepper or a little chili that most diners find easy&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;Medium; clearly spicy with sustained heat that a typical diner notices but finds comfortable&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;Hot; intense chili heat that many typical diners find challenging&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;Extremely hot; dominant, punishing heat from very hot chilies or extracts&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; rateSpiciness&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dish, ingredients) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; client&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;systemOne&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    state&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;dish&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;ingredients&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    questions&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;      spiciness&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;score&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;How much capsaicin-style heat would a typical diner expect from `dish` made with `ingredients`? Judge heat, not aromatic spice, seasoning, temperature, or flavor intensity.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;        spiceCriteria&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;      ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; response&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;answers&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;spiciness;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s compare two dishes we expect to be at opposite ends of the scale. Gingerbread is full of aromatic spices but has no spicy peppers, while &lt;a href=&quot;https://en.wikipedia.org/wiki/Phat_kaphrao&quot;&gt;pad kra pao&lt;/a&gt; is a Thai stir-fry dish made with chili peppers:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; gingerbread &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; rateSpiciness&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;gingerbread&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;Flour, molasses, brown sugar, butter, egg, ground ginger, cinnamon, cloves, nutmeg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;gingerbread&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;score); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.03&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;gingerbread&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;confidence); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.97&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;gingerbread&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;probabilities);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// { 0: 0.97, 1: 0.03, 2: 0, 3: 0, 4: 0 }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; padKraPao &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; rateSpiciness&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;pad kra pao&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;  &amp;quot;Ground pork, holy basil, garlic, 5 Thai bird&amp;#39;s eye chilies, fish sauce, oyster sauce, soy sauce, sugar, fried egg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;padKraPao&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;score); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 2.88&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;padKraPao&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;confidence); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// 0.83&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;padKraPao&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;probabilities);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// { 0: 0, 1: 0, 2: 0.16, 3: 0.8, 4: 0.04 }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the output &lt;code&gt;probabilities&lt;/code&gt;, the levels are numbered by their position in &lt;code&gt;spiceCriteria&lt;/code&gt;, from &lt;code&gt;0&lt;/code&gt; through &lt;code&gt;4&lt;/code&gt;, with &lt;code&gt;0&lt;/code&gt; corresponding to no spiciness and &lt;code&gt;4&lt;/code&gt; corresponding to extreme heat. Also, the score that Jev provides is the expected value based on these probabilities.&lt;/p&gt;
&lt;p&gt;This means that with a score of 2.88, Jev thinks that pad kra pao’s spiciness is closer to Hot than it is to Medium. In contrast, gingerbread has a score of 0.03 at a confidence of 0.97, which means that Jev is confident that it isn’t going to be spicy at all.&lt;/p&gt;
&lt;h2 id=&quot;try-it-yourself&quot;&gt;Try It Yourself&lt;a href=&quot;#try-it-yourself&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’ve shared the code for &lt;a href=&quot;https://github.com/photon-collider/jevs-kitchen&quot;&gt;Jev’s Kitchen&lt;/a&gt; on GitHub. You can set it up and run these examples yourself.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;sh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# Clone the repo and install dependencies&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; clone&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; https://github.com/photon-collider/jevs-kitchen.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; jevs-kitchen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pnpm&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; install&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# Create a .env file (optional: add your TYPESAFE_API_KEY here)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;cp&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; .env.example&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; .env&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# Start the dev server, then open the local URL Vite prints&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pnpm&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; dev&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you don’t have an API key to access Jev, the app will provide mock responses to each request. To get live answers from Jev, set up an account first at &lt;a href=&quot;https://console.typesafe.ai&quot;&gt;console.typesafe.ai&lt;/a&gt; to get a key, then add it to &lt;code&gt;.env&lt;/code&gt; as &lt;code&gt;TYPESAFE_API_KEY&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;a href=&quot;#final-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While it’s amazing that Jev can handle these queries without additional training or fine-tuning, it is still a black box. It gives us probabilities for the choices it makes, but it can’t explain them.&lt;/p&gt;
&lt;p&gt;For example, there’s no way of knowing why it gave heavy cream a 35% chance of belonging in a conventional carbonara recipe. Why wouldn’t this probability be higher or lower?&lt;/p&gt;
&lt;p&gt;Ultimately, the burden remains upon us to define the criteria Jev uses, evaluate its behavior in a representative set of cases, and decide what level of authority we’re comfortable giving it.&lt;/p&gt;</content:encoded></item><item><title>My First Impressions of the Sagittarius NAS Case</title><link>https://bryananthonio.com/blog/sagittarius-8-bay-nas-first-impressions/</link><guid isPermaLink="true">https://bryananthonio.com/blog/sagittarius-8-bay-nas-first-impressions/</guid><description>How I chose a compact NAS case for my home server after researching every option I could find, and my first take after unboxing it.</description><pubDate>Mon, 18 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;After setting up my &lt;a href=&quot;https://bryananthonio.com/blog/my-homelab-setup/&quot;&gt;homelab PC&lt;/a&gt;, one thing about it irked me in the weeks that followed: it was just too big. It was housed inside the Fractal Design Meshify C, a mid-tower case designed to maximize airflow for gaming rigs.&lt;/p&gt;
&lt;p&gt;I recently decided to rebuild it in the Sagittarius 8-bay NAS case. It’s nearly 40% smaller and packs plenty more room for storage drives. I want to share what led me to this choice, how it stacks up against other alternatives, and my first impressions after unboxing it.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#what-i-was-looking-for-in-a-case&quot;&gt;What I was looking for in a case&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#how-i-landed-on-the-sagittarius&quot;&gt;How I landed on the Sagittarius&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#unboxing-and-first-impressions&quot;&gt;Unboxing and first impressions&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#front-io&quot;&gt;Front I/O&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#pcie-expansion-slots&quot;&gt;PCIe Expansion Slots&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#sata-backplane&quot;&gt;SATA Backplane&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#included-accessories&quot;&gt;Included Accessories&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#final-thoughts-and-whats-next&quot;&gt;Final thoughts and what’s next&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-i-was-looking-for-in-a-case&quot;&gt;What I was looking for in a case&lt;a href=&quot;#what-i-was-looking-for-in-a-case&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My original goal was to find a compact case significantly smaller than my current Fractal Design Meshify C case (36.5L in volume) that could hold at least six 3.5” drives.&lt;/p&gt;
&lt;p&gt;As I researched different builds, it occurred to me that I’d need to decide between an mATX or mITX motherboard, which would determine which cases I could consider. Given that most consumer motherboards typically have between four to six SATA ports, I’d potentially have to reserve one PCIe slot for a SATA expansion card to support more drives.&lt;/p&gt;
&lt;p&gt;This tradeoff was harder for me to accept on an mITX motherboard as these motherboards only have one PCIe slot. To leave this slot available for future upgrades like a 10GBe ethernet card, I’d need a motherboard with enough native SATA ports to support all of the drive bays in my selected case. I also wanted a motherboard that supports ECC RAM to protect against silent data corruption. However, finding an mITX board with enough native SATA ports &lt;em&gt;and&lt;/em&gt; ECC support is nearly impossible outside of specialty server hardware.&lt;/p&gt;
&lt;p&gt;For instance, the &lt;a href=&quot;https://www.supermicro.com/en/products/motherboard/a2sdi-8c-hln4f&quot;&gt;Supermicro A2SDi-8C-HLN4F&lt;/a&gt; checks all my boxes, but I’ve only been able to find it for over $600 on eBay or Amazon. Meanwhile, mATX boards like the &lt;a href=&quot;https://www.asrock.com/mb/AMD/B550M%20Pro4/&quot;&gt;ASRock B550M Pro4&lt;/a&gt; support ECC and cost around $89 and the SATA port gap is easily covered by an expansion card.&lt;/p&gt;
&lt;h2 id=&quot;how-i-landed-on-the-sagittarius&quot;&gt;How I landed on the Sagittarius&lt;a href=&quot;#how-i-landed-on-the-sagittarius&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before settling on the Sagittarius case, I went down a deep rabbit hole looking at several alternatives. These are the other options that didn’t make the cut.&lt;/p&gt;
&lt;div style=&quot;overflow-x: auto;&quot;&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Case&lt;/th&gt;
&lt;th&gt;Volume&lt;/th&gt;
&lt;th&gt;3.5” Bays&lt;/th&gt;
&lt;th&gt;2.5” Bays&lt;/th&gt;
&lt;th&gt;Mobo Support&lt;/th&gt;
&lt;th&gt;PSU Type&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Sagittarius&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;21.9L&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mITX, mATX&lt;/td&gt;
&lt;td&gt;ATX&lt;/td&gt;
&lt;td&gt;$210&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jonsbo N6&lt;/td&gt;
&lt;td&gt;34.2L&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;mITX, mATX&lt;/td&gt;
&lt;td&gt;ATX&lt;/td&gt;
&lt;td&gt;$186&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jonsbo N4&lt;/td&gt;
&lt;td&gt;19.6L&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mITX, mATX&lt;/td&gt;
&lt;td&gt;SFX ≤ 125mm&lt;/td&gt;
&lt;td&gt;$133&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jonsbo N3&lt;/td&gt;
&lt;td&gt;18.2L&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;mITX&lt;/td&gt;
&lt;td&gt;SFX ≤ 105mm&lt;/td&gt;
&lt;td&gt;$159&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jonsbo N2&lt;/td&gt;
&lt;td&gt;11.1L&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;mITX&lt;/td&gt;
&lt;td&gt;SFX ≤ 150mm&lt;/td&gt;
&lt;td&gt;$145&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fractal Node 304&lt;/td&gt;
&lt;td&gt;19.2L&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;mITX&lt;/td&gt;
&lt;td&gt;ATX&lt;/td&gt;
&lt;td&gt;$109&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fractal Node 804&lt;/td&gt;
&lt;td&gt;40.3L&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mATX&lt;/td&gt;
&lt;td&gt;ATX&lt;/td&gt;
&lt;td&gt;$134&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SilverStone CS351&lt;/td&gt;
&lt;td&gt;25.1L&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;mITX, mATX&lt;/td&gt;
&lt;td&gt;ATX&lt;/td&gt;
&lt;td&gt;$229&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Most of these fell short on one or two requirements. The &lt;a href=&quot;https://www.jonsbo.com/en/products/N2Black.html&quot;&gt;Jonsbo N2&lt;/a&gt;, &lt;a href=&quot;https://www.jonsbo.com/en/products/N3.html&quot;&gt;N3&lt;/a&gt;, and &lt;a href=&quot;https://www.fractal-design.com/products/cases/node/node-304/black/&quot;&gt;Fractal Node 304&lt;/a&gt; are all mITX-only. The &lt;a href=&quot;https://www.jonsbo.com/en/products/N4Black.html&quot;&gt;Jonsbo N4&lt;/a&gt; supports mATX but has airflow concerns as it supports just one slim 120mm fan for the drive bays. Both the &lt;a href=&quot;https://www.jonsbo.com/en/products/N6Black.html&quot;&gt;Jonsbo N6&lt;/a&gt; and the &lt;a href=&quot;https://www.fractal-design.com/products/cases/node/node-804/black/&quot;&gt;Fractal Node 804&lt;/a&gt; have the right feature set but they’re each too big. And the &lt;a href=&quot;https://www.silverstonetek.com/en/product/info/server-nas/CS351/&quot;&gt;SilverStone CS351&lt;/a&gt; just doesn’t look good to me.&lt;/p&gt;
&lt;p&gt;In choosing the Sagittarius, what sealed the deal was how much it packs into its relatively small footprint. This case also offers support for four 120mm fans (two in front of the motherboard, and two behind the drive bays) and four full-height PCIe slots. The vendor’s listing claims that it supports a max CPU cooler height of 55mm and a max PCIe device length of 220mm.&lt;/p&gt;
&lt;p&gt;At the time of writing, I haven’t been able to find a comparable case that offers all of these features at this size. I honestly thought it was just too good to be true when I first stumbled upon it on an &lt;a href=&quot;https://www.reddit.com/r/DataHoarder/comments/1d0z2l3/sagittarius_nas_case_review_and_build_tips/&quot;&gt;old Reddit post&lt;/a&gt;. However, &lt;a href=&quot;https://www.youtube.com/watch?v=Jr5MjhgPz_c&quot;&gt;Wolfgang’s video&lt;/a&gt; featuring this as part of his home server confirmed that it’s real deal.&lt;/p&gt;
&lt;h2 id=&quot;unboxing-and-first-impressions&quot;&gt;Unboxing and first impressions&lt;a href=&quot;#unboxing-and-first-impressions&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I bought this case bundled with eight SATA cables on &lt;a href=&quot;https://www.aliexpress.us/item/3256810162369673.html&quot;&gt;AliExpress&lt;/a&gt; and it was shipped to me from Shenzhen, China for $210, including shipping. I received it 10 days after placing my order earlier this month.&lt;/p&gt;
&lt;p&gt;When I unboxed it, I found it sandwiched between two large pieces of styrofoam. I didn’t notice any dents or other physical damage.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/unboxing_IHo1V.jpg&quot; alt=&quot;Top-down view into the shipping box showing the Sagittarius NAS case wrapped in plastic and sandwiched between two styrofoam pieces&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The case in its shipping box, sandwiched between styrofoam&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;On the outside, the case looks just as I expected and I do like the white color. Unfortunately, the build quality isn’t amazing and if you’re a stickler about this sort of thing, then this case might be hard to justify at $210.&lt;/p&gt;
&lt;p&gt;The case itself is made of sheet metal that, according to the vendor listing, is 0.8mm thick. It appears to be quite sturdy and doesn’t flex much if you press on it.&lt;/p&gt;
&lt;p&gt;The right side of the case has a magnetic front panel that you can pull off with a pull tab. Looking at the interior, you can see imperfections on the surfaces. These, for me, were an unpleasant surprise. However, once you have all the panels installed, you’ll rarely see the inside, so it’s probably not a big deal. There also aren’t any hooks or other tie-down points to secure cables.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sagittarius-1_ZKTNuD.jpg&quot; alt=&quot;Front-right angle of the Sagittarius NAS case with the side panel removed, showing the eight 3.5-inch drive bay slots&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The eight drive bay slots visible with the side panel removed&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sagittarius-2_1QfQL7.jpg&quot; alt=&quot;Close-up of the drive bay rails inside the Sagittarius NAS case, showing surface imperfections on the sheet metal&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A closer look at the drive bay rails&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sagittarius-3_ZAKAL4.jpg&quot; alt=&quot;Front-left angle of the Sagittarius NAS case with both side panels removed, showing the drive bay cage and perforated ventilation panels&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Both side panels removed, revealing the drive bay cage&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sagittarius-4_21p4uG.jpg&quot; alt=&quot;Interior of the Sagittarius NAS case viewed from the right side, showing the bare motherboard area and front I/O cables with no cable management tie-down points&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The motherboard area and front I/O cables&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;front-io&quot;&gt;Front I/O&lt;a href=&quot;#front-io&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;As for the I/O, there’s just a power button, a USB-A 3.0 port, and a USB-C 3.0 (5 Gbps) port on the front. I do wish the front I/O was nicer. There are no HDD activity indicators or other features you might expect on a NAS case. If you look closely at the USB-A port in the photo below, you’ll notice that the metal cutout around it is slightly bent at the top, another blemish on the build quality.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/front-io_1knryG.jpg&quot; alt=&quot;Close-up of the front I/O on the Sagittarius NAS case showing the USB-C port, USB-A 3.0 port with blue connector, and circular power button&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The front I/O: USB-C, USB-A 3.0, and power button&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/front-io-2_Z2nkro0.jpg&quot; alt=&quot;Internal view of the front panel showing USB 3.0 header cables and the power button wire connected behind the perforated front panel&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The internal wiring behind the front I/O panel&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;pcie-expansion-slots&quot;&gt;PCIe Expansion Slots&lt;a href=&quot;#pcie-expansion-slots&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The back of the case has four full-height PCIe expansion slots. These slot covers can be pulled out but cannot be reattached.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/pcie-slots_Z1SXxOH.jpg&quot; alt=&quot;Rear of the Sagittarius NAS case showing four full-height PCIe expansion slot covers with front I/O cables visible inside&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Four full-height PCIe expansion slot covers on the rear&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;sata-backplane&quot;&gt;SATA Backplane&lt;a href=&quot;#sata-backplane&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The case has a SATA/SAS backplane to support hard drive hot swapping. This requires four MOLEX connectors to power all eight drive bays. My PSU has a MOLEX cable that only connects to three, so I’ll need a MOLEX splitter to power the fourth. I do wish the backplane used a more modern power connector, but this isn’t a dealbreaker for me.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--portrait&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sata-backplane_1LSmHp.jpg&quot; alt=&quot;Side view of the SATA/SAS backplane inside the Sagittarius NAS case, showing eight drive connectors and MOLEX power connectors&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1200&quot; height=&quot;1800&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The SATA/SAS backplane with eight drive connectors and MOLEX power inputs&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;included-accessories&quot;&gt;Included Accessories&lt;a href=&quot;#included-accessories&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Some people have commented that they didn’t receive a manual. However, mine surprisingly came with one. The case also includes some dust filters that you can install.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/manual-and-filters_11PX8D.jpg&quot; alt=&quot;The Sagittarius NAS case user manual, a black booklet with Chinese and English text, resting on top of the included white mesh dust filters&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The user manual sitting on top of the included mesh dust filters&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The package I received also included a box containing screws and other accessories for mounting hard disks.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/accessory-box_AocRS.jpg&quot; alt=&quot;Open white accessory box containing metal hard drive mounting brackets, bags of screws, and cable ties&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The included accessory box with mounting brackets, screws, and cable ties&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;As I mentioned earlier, my purchase included eight SATA cables bundled together in a mesh sleeve. The cables have a thin profile and seem to be of decent quality. Each has a label on both ends, allowing you to quickly identify which hard drive bay is connected to which SATA port on your motherboard.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/sata-cables_1RaMEX.jpg&quot; alt=&quot;Eight thin blue SATA cables bundled in a dark mesh sleeve, fanning out at both ends with white identification labels on each connector&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1800&quot; height=&quot;1200&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The bundled SATA cables with white labels on both ends for easy identification&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;final-thoughts-and-whats-next&quot;&gt;Final thoughts and what’s next&lt;a href=&quot;#final-thoughts-and-whats-next&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Despite the rough edges, I’m happy with this purchase. The reality is that there just aren’t many good options for a compact NAS case with mATX support, eight drive bays, and room for expansion cards.&lt;/p&gt;
&lt;p&gt;For that unique combination, I think the price is well worth it. If Fractal Design were to refresh their Node series I’d give that a hard look, but until then, the Sagittarius stands in a class of its own.&lt;/p&gt;
&lt;p&gt;Next up is the build itself. I’ll share my experience putting it all together in a &lt;a href=&quot;https://bryananthonio.com/blog/building-a-better-nas-for-my-homelab/&quot;&gt;follow-up post&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title>Why Your Home VPN Needs Dynamic DNS</title><link>https://bryananthonio.com/blog/home-vpn-dynamic-dns/</link><guid isPermaLink="true">https://bryananthonio.com/blog/home-vpn-dynamic-dns/</guid><description>Here’s what I learned about Dynamic DNS while setting up a WireGuard VPN on my home network.</description><pubDate>Sun, 26 Apr 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When I was learning how to set up a WireGuard VPN for remote access to my home network, the tutorial I was following recommended that I configure something called Dynamic DNS (DDNS).&lt;/p&gt;
&lt;p&gt;I had no idea what this was or why it was necessary here. But, it didn’t take long for my curiosity to send me down a rabbit hole regarding this, after which I learned that DDNS does solve an important problem. Namely, that without it your VPN might work just fine one day and mysteriously refuse to work the next.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;a href=&quot;#the-problem&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Your home router joins two networks: a local area network (LAN) for your devices, and your internet service provider’s (ISP) upstream network, known as the wide area network (WAN), which connects you to the internet.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--card&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/router_lan_wan_Z11TkN6.webp&quot; alt=&quot;Diagram of a home router sitting between the LAN — containing a laptop, smartphone, and homelab PC — and the WAN, which connects through the ISP to the wider internet.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3457&quot; height=&quot;1665&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;Your router lives on two networks, which includes the LAN your devices share, and the WAN that reaches the rest of the internet.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;When your router connects online, your ISP assigns it a WAN IP address. This particular IP address is different from the one you use to log into your router, known as the LAN IP address (which is often either &lt;code&gt;192.168.0.1&lt;/code&gt; or &lt;code&gt;192.168.1.1&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;If, for instance, you’re at a coffee shop and want to use WireGuard to connect to your home network, your computer needs to know your router’s current WAN IP address to make that connection. Unfortunately, this IP address can’t be hardcoded because it’s dynamically assigned by your ISP, meaning that it may change over time. Events such as power outages, modem reboots, or even scheduled ISP maintenance can cause it to shift. In practice it may actually stay the same for months, but you can’t always count on that.&lt;/p&gt;
&lt;p&gt;Dynamic DNS resolves this issue by helping you define a fixed endpoint that your computer can use to look up your router’s WAN IP address even when it changes.&lt;/p&gt;
&lt;h2 id=&quot;dynamic-dns-as-a-solution&quot;&gt;Dynamic DNS as a Solution&lt;a href=&quot;#dynamic-dns-as-a-solution&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before getting into what DDNS is, let’s briefly cover &lt;a href=&quot;https://en.wikipedia.org/wiki/Domain_Name_System&quot;&gt;DNS, the domain name system.&lt;/a&gt; A helpful analogy is that DNS functions as the phonebook of the internet. It maps domain names to IP addresses. For instance, when you type &lt;code&gt;example.com&lt;/code&gt; into your browser, your computer sends a request to a DNS server to identify the IP address of the server hosting that website. Afterwards, your computer sends a request to that IP address to load the page.&lt;/p&gt;
&lt;p&gt;Similarly, to get around the issue of your home network’s WAN IP address shifting, you can register a domain name (or grab a free subdomain from a DDNS provider) and point it to that IP address. You can then use a DDNS service to automatically update this mapping so that it always points to your current WAN IP address.&lt;/p&gt;
&lt;p&gt;Now, when you attempt to connect to your home VPN you can first have your computer look up that domain to find your WAN IP address and connect to it from there.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--card&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/dns_lookup_2rAhif.webp&quot; alt=&quot;Two-step diagram: a laptop at a coffee shop queries DNS for the home network’s WAN IP, then opens a VPN connection to the home router, which sits in front of a homelab PC on the LAN.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2612&quot; height=&quot;1394&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;The remote laptop first asks DNS for the home network’s current WAN IP, then uses it to open the VPN tunnel.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;There are many DDNS services out there including &lt;a href=&quot;https://www.duckdns.org&quot;&gt;DuckDNS&lt;/a&gt;, &lt;a href=&quot;https://www.noip.com&quot;&gt;NoIP&lt;/a&gt;, and &lt;a href=&quot;https://www.cloudflare.com/application-services/products/dns/&quot;&gt;CloudFlare&lt;/a&gt;. You can even self-host this as well, which is what I do.&lt;/p&gt;
&lt;h2 id=&quot;how-i-handle-dynamic-dns&quot;&gt;How I Handle Dynamic DNS&lt;a href=&quot;#how-i-handle-dynamic-dns&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’ve been self-hosting my own DDNS service using a tool known as &lt;a href=&quot;https://github.com/qdm12/ddns-updater&quot;&gt;&lt;code&gt;ddns-updater&lt;/code&gt;&lt;/a&gt;. For this, I purchased a domain on &lt;a href=&quot;https://porkbun.com&quot;&gt;Porkbun&lt;/a&gt; and ran the &lt;code&gt;ddns-updater&lt;/code&gt; client in a docker container to update the DNS records of that domain.&lt;/p&gt;
&lt;p&gt;I went with this approach as it was &lt;a href=&quot;https://kb.porkbun.com/article/271-how-to-set-up-dynamic-dns-ddns-on-your-domain&quot;&gt;recommended by the Porkbun documentation.&lt;/a&gt; If you opt to use a different DNS provider, &lt;code&gt;ddns-updater&lt;/code&gt; also works with many others including DigitalOcean, Cloudflare, Namecheap, and more.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;a href=&quot;#final-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;DDNS turned out to be a lot less complicated to understand and set up than I expected. And now I can rest knowing that my VPN will continue to work regardless of my ISP’s shenanigans.&lt;/p&gt;</content:encoded></item><item><title>My Homelab Setup</title><link>https://bryananthonio.com/blog/my-homelab-setup/</link><guid isPermaLink="true">https://bryananthonio.com/blog/my-homelab-setup/</guid><description>How I repurposed my old gaming PC to set up a home server for data storage, backups, and self-hosted apps.</description><pubDate>Sat, 07 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For the longest time, I’ve procrastinated on finding a good backup and storage solution for my Fujifilm RAW files. My solution up until recently involved manually copying my photos across two external SSD drives. This was quite a hassle and I hadn’t yet figured out a good off-site backup strategy.&lt;/p&gt;
&lt;p&gt;After hearing constant news updates of how &lt;a href=&quot;https://www.pcmag.com/news/memory-shortage-now-wreaking-havoc-on-hard-drive-prices-too&quot;&gt;hard drive prices have been surging due to AI data center buildouts&lt;/a&gt;, I finally decided to purchase some hard drives and set up a homelab to meet my storage and backup needs. I also used this opportunity to explore self-hosting some apps I’ve been eager to check out.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#hardware&quot;&gt;Hardware&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#truenas-operating-system&quot;&gt;TrueNAS Operating System&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#apps-im-currently-self-hosting&quot;&gt;Apps I’m Currently Self-hosting&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#scrutiny&quot;&gt;Scrutiny&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#backrest&quot;&gt;Backrest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#immich&quot;&gt;Immich&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#mealie&quot;&gt;Mealie&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ollama&quot;&gt;Ollama&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#remote-access&quot;&gt;Remote Access&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#next-steps&quot;&gt;Next Steps&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;hardware&quot;&gt;Hardware&lt;a href=&quot;#hardware&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I repurposed my old gaming PC I built back in 2018 for this use case. This machine has the following specs:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.techpowerup.com/cpu-specs/ryzen-5-2600x.c2014&quot;&gt;AMD Ryzen 5 2600X 3.6 GHz 6-Core Processor&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Motherboard&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.asrock.com/mb/amd/b450%20pro4/index.asp&quot;&gt;ASRock B450 Pro4 ATX AM4&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RAM&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.gskill.com/product/165/170/1535961634/F4-3200C14D-16GFX-EOL&quot;&gt;G.Skill Flare X 16 GB (2 x 8 GB) DDR4-3200 CL14&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPU&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.evga.com/products/specs/gpu.aspx?pn=649ba262-3f05-4ff8-9756-9cdc206e7ce6&quot;&gt;EVGA FTW2 GAMING iCX GeForce GTX 1070 Ti 8 GB&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.fractal-design.com/products/cases/meshify/meshify-c/&quot;&gt;Fractal Design Meshify C ATX Mid Tower&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PSU&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://seasonic.com/prime-gold/&quot;&gt;SeaSonic PRIME Gold 650 W 80+ Gold&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage (HDD)&lt;/td&gt;
&lt;td&gt;2x &lt;a href=&quot;https://www.westerndigital.com/products/internal-drives/wd-red-plus-sata-3-5-hdd?sku=WD80EFPX&quot;&gt;Western Digital Red Plus 8 TB 3.5”&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage (SSD)&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.samsung.com/us/computing/memory-storage/solid-state-drives/ssd-850-evo-2-5-sata-iii-500gb-mz-75e500b-am/&quot;&gt;Samsung 850 Evo 500 GB 2.5”&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage (NVMe)&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://www.sandisk.com/en-gb/products/ssd/internal-ssd/wd-blue-sn550-nvme-ssd?sku=WDS500G2B0C-00PXH0&quot;&gt;Western Digital Blue SN550 500 GB M.2-2280&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I purchased the Western Digital hard drives over the winter holiday break. The other components were already installed on the machine when I originally built it.&lt;/p&gt;
&lt;h2 id=&quot;truenas-operating-system&quot;&gt;TrueNAS Operating System&lt;a href=&quot;#truenas-operating-system&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On this machine I installed &lt;a href=&quot;https://www.truenas.com/truenas-community-edition/&quot;&gt;TrueNAS Community Edition&lt;/a&gt; on my NVMe drive. It’s a Linux-based operating system that is well-tailored for network-attached storage (NAS), file storage that is accessible to any device on your network.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--styled&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/truenas-dashboard_Z1VTB5r.webp&quot; alt=&quot;The TrueNAS Community Edition dashboard showing system information, CPU usage, and memory stats&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3014&quot; height=&quot;1680&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;My TrueNAS dashboard running version 25.10.1 (Goldeye)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;For instance, TrueNAS allows you to create snapshots of your data. This is great for preventing data loss. If, for example, you accidentally deleted a file, you could recover it from a previous snapshot containing that file. In other words, a file is only truly deleted if and only if the system has no snapshots containing that file.&lt;/p&gt;
&lt;p&gt;I’ve set up my machine to take hourly, daily, and even weekly snapshots. I’ve also configured it to delete old snapshots after a given period of time to save storage space.&lt;/p&gt;
&lt;p&gt;Most of my data is mirrored across the two 8 TB hard disks in a RAID 1 setup. This means that if one drive fails, the other drive will still have all of my data intact. The SSD is used to store data from services that I self-host that benefit from having fast read and write speeds.&lt;/p&gt;
&lt;h2 id=&quot;apps-im-currently-self-hosting&quot;&gt;Apps I’m Currently Self-hosting&lt;a href=&quot;#apps-im-currently-self-hosting&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Not only is TrueNAS good for file storage, you can also host apps on it!
&lt;a href=&quot;https://apps.truenas.com/catalog/&quot;&gt;TrueNAS offers a catalog of apps&lt;/a&gt;, supported by the community, that you can install on your machine.&lt;/p&gt;
&lt;h3 id=&quot;scrutiny&quot;&gt;Scrutiny&lt;a href=&quot;#scrutiny&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/Starosdev/scrutiny&quot;&gt;Scrutiny&lt;/a&gt; is a web dashboard for monitoring the health of your storage drives. Hard drives and SSDs have built-in firmware called S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology) that continuously tracks health metrics like temperature, power-on hours, and read errors.&lt;/p&gt;
&lt;p&gt;Scrutiny reads this data and presents it in a dashboard showing historical trends, making it easy to spot warning signs that a drive may fail soon.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--styled&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/scrutiny-dashboard_Z22oaU0.webp&quot; alt=&quot;Scrutiny drive health dashboard showing four drives — two 7.3 TiB HDDs, one SSD, and one NVMe — all with a passed status&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2378&quot; height=&quot;1708&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;Scrutiny monitoring all four of my drives&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;backrest&quot;&gt;Backrest&lt;a href=&quot;#backrest&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://garethgeorge.github.io/backrest/&quot;&gt;Backrest&lt;/a&gt; is a web frontend for &lt;a href=&quot;https://restic.net&quot;&gt;restic&lt;/a&gt;, a command-line tool used for creating file backups. I’ve set this up to save daily backups of my data to an object storage bucket on &lt;a href=&quot;https://www.backblaze.com/cloud-storage&quot;&gt;Backblaze B2&lt;/a&gt;.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--styled&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/backrest-dashboard_Z1aA5Yd.webp&quot; alt=&quot;The Backrest dashboard summary showing backup stats for a media-backup repository and plan&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3014&quot; height=&quot;1680&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;My Backrest configuration&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;immich&quot;&gt;Immich&lt;a href=&quot;#immich&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://immich.app&quot;&gt;Immich&lt;/a&gt; is one of the most popular open-source self-hosted apps for managing photos and videos. I love that it also offers &lt;a href=&quot;https://apps.apple.com/us/app/immich/id1613945652&quot;&gt;iOS&lt;/a&gt; and &lt;a href=&quot;https://play.google.com/store/apps/details?id=app.alextran.immich&amp;amp;hl=en_US&quot;&gt;Android&lt;/a&gt; apps that allow you to back up photos and videos from your mobile devices. This is great if you want to rely less on services like Google Photos or iCloud. I’m currently using this to back up photos and videos from my phone.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--styled&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/immich-dashboard_Z1sgb03.webp&quot; alt=&quot;Immich photo library showing a grid of bird photos.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3014&quot; height=&quot;1710&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;A sample of my Immich photo library&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;mealie&quot;&gt;Mealie&lt;a href=&quot;#mealie&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://mealie.io&quot;&gt;Mealie&lt;/a&gt; is a recipe management tool that has made my meal prepping experience so much better! I’ve found it great for saving recipes I find on sites like &lt;a href=&quot;https://cooking.nytimes.com&quot;&gt;NYT Cooking&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When importing recipes, you can provide the URL of the recipe and Mealie will scrape the ingredients and instructions from the page and save it in your recipe library. This makes it easier to keep track of recipes you find online and want to try out later.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--styled&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/mealie-dashboard_1APIQo.webp&quot; alt=&quot;Mealie’s recipe library showing six saved recipes in a grid layout&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3014&quot; height=&quot;1696&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;A few of my saved recipes in Mealie&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;ollama&quot;&gt;Ollama&lt;a href=&quot;#ollama&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://ollama.com&quot;&gt;Ollama&lt;/a&gt; is a backend for running various AI models. I installed it to try running large language models like &lt;a href=&quot;https://ollama.com/library/qwen3.5:4b&quot;&gt;&lt;code&gt;qwen3.5:4b&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://ollama.com/library/gemma3:4b&quot;&gt;&lt;code&gt;gemma3:4b&lt;/code&gt;&lt;/a&gt; out of curiosity. I’ve also recently been exploring the world of vector embeddings such as &lt;a href=&quot;https://ollama.com/library/qwen3-embedding:4b&quot;&gt;&lt;code&gt;qwen3-embedding:4b&lt;/code&gt;&lt;/a&gt;. All of these models are small enough to fit in the 8GB of VRAM my GPU provides. I like being able to offload the work of running models on my homelab instead of my laptop.&lt;/p&gt;
&lt;h2 id=&quot;remote-access&quot;&gt;Remote Access&lt;a href=&quot;#remote-access&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When I’m not at home, I use &lt;a href=&quot;https://tailscale.com&quot;&gt;Tailscale&lt;/a&gt;, a plug-and-play VPN service, to access my data and self-hosted apps remotely from any device. Tailscale builds on top of another tool called &lt;a href=&quot;https://www.wireguard.com&quot;&gt;WireGuard&lt;/a&gt; to provide a secure tunnel into my home network.&lt;/p&gt;
&lt;p&gt;The advantage here is that my homelab PC doesn’t need to be exposed to the public internet for this to work. Any device I want to use to access my homelab remotely needs to install the Tailscale app and be authenticated to my Tailscale network.&lt;/p&gt;
&lt;h2 id=&quot;next-steps&quot;&gt;Next Steps&lt;a href=&quot;#next-steps&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Right now, accessing my apps requires typing in the IP address of my machine (or Tailscale address) together with the app’s port number. Because all of my services share the same IP address, my password manager has trouble distinguishing which login to use for each one.&lt;/p&gt;
&lt;p&gt;In the future I’ll look into figuring out how to assign custom domain names to all of my services.&lt;/p&gt;</content:encoded></item><item><title>Redesign: Version 5.0</title><link>https://bryananthonio.com/blog/redesign-5/</link><guid isPermaLink="true">https://bryananthonio.com/blog/redesign-5/</guid><description>A review of the latest design changes I’ve made to my website.</description><pubDate>Fri, 27 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It’s been almost a year since my &lt;a href=&quot;https://bryananthonio.com/blog/redesign-4/&quot;&gt;last redesign.&lt;/a&gt; With this redesign I wanted to trying something new with the typography and the page layouts.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#new-font-stack&quot;&gt;New Font Stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#moving-away-from-tailwindcss&quot;&gt;Moving Away from TailwindCSS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#screenshots&quot;&gt;Screenshots&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#about&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#blog&quot;&gt;Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#blog-index&quot;&gt;Blog Index&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#blog-post&quot;&gt;Blog Post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#links&quot;&gt;Links&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#reading&quot;&gt;Reading&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;new-font-stack&quot;&gt;New Font Stack&lt;a href=&quot;#new-font-stack&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’m using these fonts now:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://fonts.google.com/specimen/Space+Grotesk&quot;&gt;&lt;strong&gt;Space Grotesk&lt;/strong&gt;&lt;/a&gt;. I love the techie and slightly quirky vibe of this font. Mainly using this in headers and some navigational elements.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://fonts.floriankarsten.com/fk-roman-standard&quot;&gt;&lt;strong&gt;FK Roman Standard&lt;/strong&gt;&lt;/a&gt;. Having previously used &lt;a href=&quot;https://fonts.google.com/specimen/Source+Serif+4&quot;&gt;Source Serif 4&lt;/a&gt; for body text in the past I wanted to try something new and fresh. I discovered this font from reading articles on &lt;a href=&quot;https://www.theverge.com/&quot;&gt;&lt;em&gt;The Verge&lt;/em&gt;&lt;/a&gt; and it just so happens to be designed by Florian Karsten, the same designer of Space Grotesk. I like how legible and slightly elegant this font is.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://fonts.google.com/specimen/IBM+Plex+Mono&quot;&gt;&lt;strong&gt;IBM Plex Mono&lt;/strong&gt;&lt;/a&gt;. This has been my go-to monospace font for sometime now. I mainly use this for code snippets, tags, and the byline in blog posts.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;moving-away-from-tailwindcss&quot;&gt;Moving Away from TailwindCSS&lt;a href=&quot;#moving-away-from-tailwindcss&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://bryananthonio.com/blog/simplifying-my-website-css-using-tailwind/&quot;&gt;While I’ve good experiences working with TailwindCSS&lt;/a&gt;, I no longer see it as a necessity for this project. Over the past year, I’ve grown more comfortable writing my own CSS especially since working through some exercises in &lt;a href=&quot;https://every-layout.dev&quot;&gt;this short course on CSS layouts.&lt;/a&gt; Removing TailwindCSS was relatively easy to do given that Astro components already support locally-scoped CSS styles in components.&lt;/p&gt;
&lt;p&gt;I now have my CSS in a few different files:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;base.css:&lt;/code&gt; Base styles for the entire site, such as anti-aliasing and some CSS resets.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;prose.css:&lt;/code&gt; Styles for prose content in blog posts, focusing mostly on typography.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fonts.css:&lt;/code&gt; Font-face declarations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tokens.css:&lt;/code&gt; CSS variables for colors, spacing, and typography scales (generated using &lt;a href=&quot;https://utopia.fyi/&quot;&gt;utopia.fyi&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cube.css:&lt;/code&gt; Styles based on the &lt;a href=&quot;https://cube.fyi/&quot;&gt;CUBE CSS approach.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;utilities.css:&lt;/code&gt; Miscellaneous reusable helper classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I first encountered CUBE CSS in &lt;a href=&quot;https://piccalil.li/complete-css/&quot;&gt;this CSS course by Andy Bell&lt;/a&gt;. I’m still learning the ins and outs of how to best use this in my current project.&lt;/p&gt;
&lt;h2 id=&quot;screenshots&quot;&gt;Screenshots&lt;a href=&quot;#screenshots&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;home&quot;&gt;Home&lt;a href=&quot;#home&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I spent a lot of time in Figma trying to come up with a new layout for the home page. Here’s where I landed after many days of experimentation.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/homepage_Z1jzYGo.png&quot; alt=&quot;New design of the home page layout&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;p&gt;One thing to note here is that the footer now contains a dropdown menu to toggle between light and dark mode.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/homepage-2_UlEhq.png&quot; alt=&quot;New design of the home page layout. Part 2.&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;h3 id=&quot;about&quot;&gt;About&lt;a href=&quot;#about&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Some modest changes here to the typography. Also added my portrait to the side.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/about_Z1NnqmL.png&quot; alt=&quot;New design of the about page layout&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;h3 id=&quot;blog&quot;&gt;Blog&lt;a href=&quot;#blog&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’ve updated the sidebar here to include a list of blog post tags and a link to the new blog index page.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-posts_2rxRoC.png&quot; alt=&quot;New design of the blog page layout&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;h3 id=&quot;blog-index&quot;&gt;Blog Index&lt;a href=&quot;#blog-index&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A new page where you can see all blog post tags and their associated posts in one place.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-index_Z2oKjqD.png&quot; alt=&quot;The blog index page.&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;h3 id=&quot;blog-post&quot;&gt;Blog Post&lt;a href=&quot;#blog-post&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The most notable change here is that I added some share buttons next to the intro paragraphs in the tablet/desktop layout. On mobile these just appear at the bottom of the article.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-post-1_Z1YB6Iy.png&quot; alt=&quot;New design of the blog page layout&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;h3 id=&quot;links&quot;&gt;Links&lt;a href=&quot;#links&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This is a new section I added to share links that I find interesting and helpful.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/links_XqYAn.png&quot; alt=&quot;Design of the links page layout.&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;
&lt;h3 id=&quot;reading&quot;&gt;Reading&lt;a href=&quot;#reading&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I switched to a staggered two-column layout for my reading list.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/reading_21E0UY.png&quot; alt=&quot;New design of the Reading page layout.&quot; sizes=&quot;auto&quot; style=&quot;margin: 1em 0;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2122&quot;&gt;</content:encoded></item><item><title>Self-Hosting Umami Analytics on a VPS</title><link>https://bryananthonio.com/blog/self-hosting-umami-analytics/</link><guid isPermaLink="true">https://bryananthonio.com/blog/self-hosting-umami-analytics/</guid><description>Documenting my experience switching from Plausible to self-hosted Umami Analytics.</description><pubDate>Sun, 28 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Over the winter holiday break this year, I decided to self-host my web analytics platform for this website using &lt;a href=&quot;https://umami.is/&quot;&gt;Umami&lt;/a&gt;. I’ve been using &lt;a href=&quot;https://plausible.io/&quot;&gt;Plausible Analytics&lt;/a&gt; for a few years now. But, I wanted to cut down on costs, take control of my data, and learn more about hosting services on a VPS.&lt;/p&gt;
&lt;p&gt;In this post, I’ll share my experience setting up Umami on a VPS and what I’ve learned through this process. If you’d like to self-host Umami yourself, &lt;a href=&quot;https://github.com/photon-collider/selfhost-umami-analytics&quot;&gt;check out this Github repository where I’ve documented my setup in more detail&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;why-i-switched-to-umami&quot;&gt;Why I switched to Umami&lt;a href=&quot;#why-i-switched-to-umami&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;At the time of this writing, the cheapest Plausible Cloud plan costs $90/year (billed annually). This plan includes: a data retention period of 3 years, up to 10,000 page views per month, and analytics tracking for one site.&lt;/p&gt;
&lt;p&gt;However, I’d like to retain my data for more than 3 years. Paying $90 per year also feels steep for my relatively low traffic site. I am also planning to create more websites in the future, and don’t want to upgrade my plan so I can have analytics on these new sites.&lt;/p&gt;
&lt;p&gt;After researching alternatives, I opted to try Umami as it looked easier to self host compared to the &lt;a href=&quot;https://github.com/plausible/community-edition/&quot;&gt;community edition of Plausible.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One tradeoff is that the self-hosted version of Umami uses Postgres for data persistence, which doesn’t scale as well as other analytics services that use &lt;a href=&quot;https://clickhouse.com&quot;&gt;ClickHouse&lt;/a&gt;, a database optimized for analytics workloads. Given that my traffic volume is quite low, I don’t expect this to be an issue for me in the foreseeable future.&lt;/p&gt;
&lt;p&gt;Umami does also have a &lt;a href=&quot;https://umami.is/blog/umami-v3&quot;&gt;nicer UI&lt;/a&gt; and a few extra features compared to Plausible, which is a bonus.&lt;/p&gt;
&lt;p&gt;I briefly considered using the free tier on Umami Cloud, but that only offers 6 months of data retention. The next option would be to pay for the pro plan which costs $20 per month. Hence, I decided to self-host this service myself instead to minimize costs.&lt;/p&gt;
&lt;h2 id=&quot;building-my-self-hosted-stack&quot;&gt;Building my self-hosted stack&lt;a href=&quot;#building-my-self-hosted-stack&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Aside from minimizing costs, I was also eager to learn how to self-host services for myself on a VPS. This felt intimidating at first, especially the networking aspects. But, it wasn’t too difficult with the help of Claude.&lt;/p&gt;
&lt;p&gt;I learned how to provision my VPS, secure it with a firewall (using &lt;code&gt;ufw&lt;/code&gt;) and SSH access, and point a subdomain to my server’s IP address using DNS A records. One protip I learned is that you can add an entry like so in your &lt;code&gt;.ssh/config&lt;/code&gt; file to simplify SSH access to your server:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;ssh-config&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;Host&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; hetzner-umami&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  HostName&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;YOUR&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;SERVER&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;IP&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;ADDRESS&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  User&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; &amp;lt;your-username&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  IdentityFile&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ~/.ssh/hetzner_ed25519&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  AddKeysToAgent&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; yes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  UseKeychain&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; yes&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From here, you can simply run &lt;code&gt;ssh hetzner-umami&lt;/code&gt; to connect to your server.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://umami.is/docs/install&quot;&gt;official Umami documentation&lt;/a&gt; also provided helpful guidance on setting things up using docker compose.&lt;/p&gt;
&lt;p&gt;My final setup is hosted on a Hetzner CAX11 (ARM-based VPS) for €4.45/month, which includes the cost of automatic daily backups. This was surprisingly affordable compared to other VPS providers like Digital Ocean.&lt;/p&gt;
&lt;p&gt;On this platform, I’m leveraging docker compose to run Umami together with a Postgres database, and &lt;a href=&quot;https://caddyserver.com/&quot;&gt;Caddy&lt;/a&gt; as a reverse proxy. Caddy here handles HTTPS automatically out of the box via Let’s Encrypt.&lt;/p&gt;
&lt;p&gt;This may be overkill but I did also set up monthly backups of my Postgres database to AWS S3 Glacier using a cron job. I’ve been learning about AWS at work so this side project offered an opportunity to get some hands-on experience with S3.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;a href=&quot;#final-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This took a few hours on a Saturday afternoon to set up. But, I’m quite happy I spent the time learning how to self-host a web service with the added bonus of saving some money in the long run. I’d definitely recommend giving this a try if you love tinkering and want to learn more about web hosting!&lt;/p&gt;</content:encoded></item><item><title>Fix Claude Code Credential Errors for Pro Users by Switching to Sonnet</title><link>https://bryananthonio.com/blog/fixing-claude-code-api-errors-pro-users/</link><guid isPermaLink="true">https://bryananthonio.com/blog/fixing-claude-code-api-errors-pro-users/</guid><description>Configure Claude Code to use the Sonnet model instead of Opus to fix Pro user API authentication in CLI and GitHub workflows.</description><pubDate>Sat, 27 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;aside class=&quot;callout callout--warning&quot; data-astro-cid-q2ml7llr&gt;&lt;div class=&quot;callout__header&quot; data-astro-cid-q2ml7llr&gt;&lt;svg fill=&quot;none&quot; viewBox=&quot;0 0 24 24&quot; stroke-width=&quot;1.5&quot; stroke=&quot;currentColor&quot; class=&quot;callout__icon&quot; data-astro-cid-q2ml7llr=&quot;true&quot;&gt;
  &lt;path stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; d=&quot;M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z&quot; /&gt;
&lt;/svg&gt;&lt;strong class=&quot;callout__title&quot; data-astro-cid-q2ml7llr&gt;OpenCode Users: This Won&amp;#39;t Help&lt;/strong&gt;&lt;/div&gt;&lt;div class=&quot;callout__content&quot; data-astro-cid-q2ml7llr&gt;&lt;p&gt;This post addresses an old issue (now solved) occuring for Pro-tier users when
Opus 4.1 was released for Claude Code. You won’t be able to use your Claude
Pro or Max credentials with OpenCode or another coding agent tool due to a
recent change by Anthropic. &lt;a href=&quot;https://github.com/anomalyco/opencode/issues/10937&quot;&gt;See here for details.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;/aside&gt;
&lt;p&gt;I recently encountered this error when using Claude Code and my Claude Code GitHub Actions:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;API Error: 400&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;This credential is only authorized for use with Claude Code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;and cannot be used for other API requests.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This was puzzling given that I had followed the &lt;a href=&quot;https://docs.claude.com/en/docs/claude-code/setup&quot;&gt;official setup instructions&lt;/a&gt; to get started. After some investigation, I discovered &lt;a href=&quot;https://github.com/anthropics/claude-code/issues/8046&quot;&gt;this affects Pro-tier users specifically&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What’s the root cause? Claude Code’s default configuration uses the &lt;code&gt;Opus&lt;/code&gt; model, &lt;a href=&quot;https://support.claude.com/en/articles/11145838-using-claude-code-with-your-pro-or-max-plan#h_50f6dec29dAn&quot;&gt;which Pro-tier users don’t have access to&lt;/a&gt;. You can upgrade to the Max-tier if you’d like to use the &lt;code&gt;Opus&lt;/code&gt; model. However, a workaround solution is to configure Claude Code to use the &lt;code&gt;Sonnet&lt;/code&gt; model instead.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#fixing-the-cli-configuration&quot;&gt;Fixing the CLI Configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#fixing-github-actions&quot;&gt;Fixing GitHub Actions&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#updating-claudeyml&quot;&gt;Updating claude.yml&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#updating-claude-code-reviewyml&quot;&gt;Updating claude-code-review.yml&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;fixing-the-cli-configuration&quot;&gt;Fixing the CLI Configuration&lt;a href=&quot;#fixing-the-cli-configuration&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The quickest fix is to use the &lt;code&gt;/model&lt;/code&gt; command in Claude Code to select Sonnet as your model:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt; /model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;╭────────────────────────────────────────────────────────────────────────╮&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│                                                                        │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│  Select model                                                          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│  Switch between Claude models. Applies to this session and future      │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│  Claude Code sessions. For custom model names, specify with --model.   │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│                                                                        │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│     1. Default (recommended)   Opus 4.1 for up to 50% of usage         │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│                                limits, then use Sonnet 4               │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│     2. Opus                    Opus 4.1 for complex tasks · Reaches    │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│                                usage limits faster                     │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│   ❯ 3. Sonnet                  Sonnet 4 for daily use ✔                │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│     4. Opus Plan Mode          Use Opus 4.1 in plan mode, Sonnet 4     │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│                                otherwise                               │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;│                                                                        │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;╰────────────────────────────────────────────────────────────────────────╯&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Select option 3 (Sonnet) to resolve the API credential issue. This setting will persist across future Claude Code sessions.&lt;/p&gt;
&lt;h2 id=&quot;fixing-github-actions&quot;&gt;Fixing GitHub Actions&lt;a href=&quot;#fixing-github-actions&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you’re using Claude Code in GitHub Actions, you’ll need to update your workflow files to specify the Sonnet model.&lt;/p&gt;
&lt;h3 id=&quot;updating-claudeyml&quot;&gt;Updating claude.yml&lt;a href=&quot;#updating-claudeyml&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In your &lt;code&gt;claude.yml&lt;/code&gt; file, add the &lt;code&gt;claude_args&lt;/code&gt; parameter with the &lt;code&gt;--model sonnet&lt;/code&gt; flag:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon has-highlighted&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# .github/workflows/claude.yml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;Run Claude Code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;  id&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;claude&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;  uses&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;anthropics/claude-code-action@v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;  with&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;    claude_code_oauth_token&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;    additional_permissions&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;      actions: read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line highlighted&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;    claude_args&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;--model sonnet&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The key addition is the &lt;code&gt;claude_args: &amp;quot;--model sonnet&amp;quot;&lt;/code&gt; line at the bottom of the step configuration.&lt;/p&gt;
&lt;h3 id=&quot;updating-claude-code-reviewyml&quot;&gt;Updating claude-code-review.yml&lt;a href=&quot;#updating-claude-code-reviewyml&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;For the code review workflow update the existing &lt;code&gt;claude_args&lt;/code&gt; parameter to include the model specification:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# github/workflows/claude-code-review.yml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;claude_args&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;--model sonnet --allowed-tools &amp;quot;Bash(gh issue view:*),Bash(gh search:*),Bash(gh issue list:*),Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr list:*)&amp;quot;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Simply add &lt;code&gt;--model sonnet&lt;/code&gt; at the beginning of the existing &lt;code&gt;claude_args&lt;/code&gt; string.&lt;/p&gt;</content:encoded></item><item><title>The Shift from ML Engineering to AI Engineering</title><link>https://bryananthonio.com/blog/shift-from-ml-to-ai-engineering/</link><guid isPermaLink="true">https://bryananthonio.com/blog/shift-from-ml-to-ai-engineering/</guid><description>Exploring the key differences between ML and AI engineering, foundation models, and the challenges of building AI applications.</description><pubDate>Sun, 14 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I recently started reading through &lt;a href=&quot;https://www.oreilly.com/library/view/ai-engineering/9781098166298/&quot;&gt;AI Engineering by Chip Huyen&lt;/a&gt; to learn more about how to build applications on top of foundation models. As I finish each chapter, I’ll be writing posts on my top takeaways.&lt;/p&gt;
&lt;p&gt;This time I’m starting with the first chapter. First, I’ll note that it covers a variety of topics such as different use cases of AI models, how to train foundation models, and how to decide whether to build an AI application. Instead of covering each of these I’m going to focus on these topics I found interesting:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What AI engineering is and and how it differs from ML engineering.&lt;/li&gt;
&lt;li&gt;What foundation models are.&lt;/li&gt;
&lt;li&gt;The challenges associated with integrating foundation models in AI applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#ml-engineering-vs-ai-engineering&quot;&gt;ML Engineering vs AI Engineering&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#ml-engineering&quot;&gt;ML Engineering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ai-engineering&quot;&gt;AI Engineering&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#what-are-foundation-models&quot;&gt;What Are Foundation Models?&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#foundation-models&quot;&gt;Foundation Models&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#large-language-models&quot;&gt;Large Language Models&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#integrating-foundation-models-in-applications&quot;&gt;Integrating Foundation Models in Applications&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#adapting-models-to-specific-use-cases&quot;&gt;Adapting Models to Specific Use Cases&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#evaluating-models&quot;&gt;Evaluating Models&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#optimizing-model-inference&quot;&gt;Optimizing Model Inference&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#final-thoughts&quot;&gt;Final Thoughts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;ml-engineering-vs-ai-engineering&quot;&gt;ML Engineering vs AI Engineering&lt;a href=&quot;#ml-engineering-vs-ai-engineering&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One of the goals of the first chapter is to draw a distinction between machine learning (ML) engineering and what is now known as artificial intelligence (AI) engineering.&lt;/p&gt;
&lt;h3 id=&quot;ml-engineering&quot;&gt;ML Engineering&lt;a href=&quot;#ml-engineering&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Traditional ML engineering involves curating a dataset to train a model from scratch which can involve &lt;a href=&quot;https://en.wikipedia.org/wiki/Feature_engineering&quot;&gt;feature engineering&lt;/a&gt;, doing evaluations on that model, and then deploying it in production. The challenge here is that gathering training data and training a model is both non-trivial and time consuming. Thus, ML engineering tends to be unfeasible if you lack the resources needed to create your own model.&lt;/p&gt;
&lt;h3 id=&quot;ai-engineering&quot;&gt;AI Engineering&lt;a href=&quot;#ai-engineering&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In contrast, AI engineering focuses on taking general-purpose models, known as &lt;a href=&quot;https://en.wikipedia.org/wiki/Foundation_model&quot;&gt;foundation models&lt;/a&gt;, and adapting them to specific use cases. The barrier to entry to get started on a project is lower as you don’t have to do the model development by yourself.&lt;/p&gt;
&lt;p&gt;However, foundation models come with their own challenges. It’s not always straightforward to evaluate their performance and they do require higher amounts of compute resources and infrastructure to operate. Before touching on these points, I’ll first dive into the basics of foundation models.&lt;/p&gt;
&lt;h2 id=&quot;what-are-foundation-models&quot;&gt;What Are Foundation Models?&lt;a href=&quot;#what-are-foundation-models&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Chip Huyen provides a basic overview of foundation models and how they relate to &lt;a href=&quot;https://en.wikipedia.org/wiki/Large_language_model&quot;&gt;large language models (LLMs)&lt;/a&gt;. I’ll give a summary here and go into more details on this topic in future posts as I read through the rest of the book.&lt;/p&gt;
&lt;h3 id=&quot;foundation-models&quot;&gt;Foundation Models&lt;a href=&quot;#foundation-models&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The gist here is that foundation models, typically trained on very large quantities of data, are models that are suited for a wide variety of tasks. These include LLMs, multimodal models which can handle various data modalities (text, images, audio, and more), and other large pre-trained models.&lt;/p&gt;
&lt;h3 id=&quot;large-language-models&quot;&gt;Large Language Models&lt;a href=&quot;#large-language-models&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;LLMs capture statistical patterns of language. These models use what are called tokens as fundamental units of written language. For instance, a model may represent the word &lt;code&gt;tokenization&lt;/code&gt; using the tokens &lt;code&gt;token&lt;/code&gt; and &lt;code&gt;ization&lt;/code&gt;. Here, the set of tokens a model can use represents the model’s vocabulary.&lt;/p&gt;
&lt;p&gt;One thing to note here is that there are two types of language models. The first are known as masked language models, and these are trained to handle fill in the blank queries. The second type of language models are known as autoregressive models, which are trained to predict the next token when presented a sequence of tokens. These are the models used in generative AI applications for use cases such as summarizing documents, writing essays, or answering questions.&lt;/p&gt;
&lt;h2 id=&quot;integrating-foundation-models-in-applications&quot;&gt;Integrating Foundation Models in Applications&lt;a href=&quot;#integrating-foundation-models-in-applications&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Successfully deploying foundation models in production requires addressing multiple technical and operational challenges.&lt;/p&gt;
&lt;h3 id=&quot;adapting-models-to-specific-use-cases&quot;&gt;Adapting Models to Specific Use Cases&lt;a href=&quot;#adapting-models-to-specific-use-cases&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;There are three common pathways to adapt AI models into applications. These include prompt engineering, retrieval-augmented generation, and fine-tuning.&lt;/p&gt;
&lt;p&gt;Prompt engineering involves providing the model a set of instructions and/or providing examples of what desired responses look like when the model receives a query. I understand that it’s typically the first technique to try in most situations as it requires fewer resources.&lt;/p&gt;
&lt;p&gt;Retrieval-augmented generation requires providing the model additional context from a database of information that may be relevant to an incoming query. This technique is useful to enhance a model’s performance when domain-specific knowledge is needed.&lt;/p&gt;
&lt;p&gt;Fine-tuning is the practice of updating the model’s weights using a curated dataset to improve its performance. This may be worth trying to eke out even more performance especially when there are tight specific requirements on the quality of the model’s output.&lt;/p&gt;
&lt;h3 id=&quot;evaluating-models&quot;&gt;Evaluating Models&lt;a href=&quot;#evaluating-models&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;While it’s easy to get started with integrating a model in an application, it’s hard to evaluate how well a model performs for your intended use case. Foundation models can suit a variety of open-ended use cases like making recipe suggestions, responding to customer queries, writing code, and more.&lt;/p&gt;
&lt;p&gt;Thus, it can be difficult to objectively evaluate a model’s performance. That’s the subject of evaluations (commonly abbreviated as evals) which will be covered in a later chapter in the book.&lt;/p&gt;
&lt;h3 id=&quot;optimizing-model-inference&quot;&gt;Optimizing Model Inference&lt;a href=&quot;#optimizing-model-inference&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A related challenge is how to optimize the cost of running a model while maintaining performance. Every query that a model has respond to incurs a cost.&lt;/p&gt;
&lt;p&gt;Huyen highlights at a high level that there are additional techniques to optimize the cost of doing model inference such as distillation, quantization, and parallelism, which again will be covered in later chapters.&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;Final Thoughts&lt;a href=&quot;#final-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My biggest takeaway from the first chapter is the distinction between ML engineering and AI engineering and the challenges associated with integrating an AI model in an application.&lt;/p&gt;
&lt;p&gt;As I read more chapters of this book I’ll be curious to learn more about how foundation models are evaluated and how to decide which models to pick given that more and more models are available on the market nowadays.&lt;/p&gt;</content:encoded></item><item><title>Redesign: Version 4.0</title><link>https://bryananthonio.com/blog/redesign-4/</link><guid isPermaLink="true">https://bryananthonio.com/blog/redesign-4/</guid><description>Documenting my newest website redesign, ahead of some further design tweaks.</description><pubDate>Sun, 06 Apr 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The last time I wrote about redesigning my website &lt;a href=&quot;https://bryananthonio.com/blog/redesign-3/&quot;&gt;was back in 2023.&lt;/a&gt; Since then, I’ve learned so much more about user interface design. My latest insights come from completing half of the &lt;a href=&quot;https://www.learnui.design&quot;&gt;Learn UI Design course&lt;/a&gt; last year.&lt;/p&gt;
&lt;p&gt;Here, I wanted to apply my new design skills by redesigning this website. Unlike my previous redesigns, I created mockups this time in Figma.&lt;/p&gt;
&lt;p&gt;This helped me iterate over so many more different ideas than before. In the past, I would try to just design by writing the code first and seeing if I liked it. I realize now that this was much slower and it prevented me from exercising my creativity.&lt;/p&gt;
&lt;p&gt;This latest redesign was finished sometime in 2024. I’m already thinking about making some more tweaks but I wanted to document what I have so far before I proceed with those.&lt;/p&gt;
&lt;h2 id=&quot;some-notes&quot;&gt;Some Notes&lt;a href=&quot;#some-notes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;With this design, I wanted to lean more towards keeping things clean and simple.&lt;/li&gt;
&lt;li&gt;I removed the projects page because I wasn’t really creating new projects to share. Hence, this page became redundant.&lt;/li&gt;
&lt;li&gt;This design uses two fonts: &lt;a href=&quot;https://www.fontshare.com/fonts/satoshi&quot;&gt;Satoshi Variable&lt;/a&gt; and &lt;a href=&quot;https://fonts.google.com/specimen/Source+Serif+4&quot;&gt;Source Serif 4&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;There’s a new Reading page where I share a list of books I’ve read.&lt;/li&gt;
&lt;li&gt;I added a Now page featuring a short post on what I’m working on. This was inspired by &lt;a href=&quot;https://nownownow.com/about&quot;&gt;NowNowNow by Derek Sivers&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;I spent a bit of time on updating the layout for blog posts. I’m really happy with how it turned out.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;screenshots&quot;&gt;Screenshots&lt;a href=&quot;#screenshots&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;home-page&quot;&gt;Home Page&lt;a href=&quot;#home-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/home-1_Z1zWeQq.png&quot; alt=&quot;New design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/home-2_12dqpk.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;blog&quot;&gt;Blog&lt;a href=&quot;#blog&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-1_Z2rUy1f.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-2_af7fv.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;blog-post&quot;&gt;Blog Post&lt;a href=&quot;#blog-post&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-post-1_1aWE7s.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog-post-2_Z1h3NpI.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;now-page&quot;&gt;Now Page&lt;a href=&quot;#now-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/now-1_1nnG4u.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;reading-page&quot;&gt;Reading Page&lt;a href=&quot;#reading-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/reading-1_Z15vuqW.png&quot; alt=&quot;Another screenshot of the new design of the home page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I had a lot of fun putting together this redesign. Indeed, this work marked a significant evolution in my design skills and my vision for my website. I’m only just getting started in my design journey and look forward to seeing what comes next.&lt;/p&gt;</content:encoded></item><item><title>A First Look at Setting Up Tailwind CSS v4.0</title><link>https://bryananthonio.com/blog/configuring-tailwind-css-v4/</link><guid isPermaLink="true">https://bryananthonio.com/blog/configuring-tailwind-css-v4/</guid><description>How Tailwind CSS v4.0 simplifies design token configuration.</description><pubDate>Sun, 26 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Just three days ago, &lt;a href=&quot;https://tailwindcss.com/blog/tailwindcss-v4&quot;&gt;Tailwind CSS released v4.0&lt;/a&gt; which introduces major changes to how we configure and manage design tokens with this framework.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://bryananthonio.com/blog/simplifying-my-website-css-using-tailwind/&quot;&gt;As someone who uses Tailwind CSS extensively&lt;/a&gt; for both personal and professional work, I immediately migrated my personal website to test the new system.&lt;/p&gt;
&lt;p&gt;In this post, I’ll share my experience migrating to TailwindCSS v4, focusing on the new &lt;code&gt;@theme&lt;/code&gt; directive and how it simplifies design token management. You’ll see how the new approach eliminates the need for a separate JavaScript configuration file and makes working with CSS variables more straightforward.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#the-old-way-to-configure-tailwind-css-with-css-variables&quot;&gt;The Old Way to Configure Tailwind CSS With CSS Variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#introducing-the-new-config-workflow&quot;&gt;Introducing the New Config Workflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#migration-experience&quot;&gt;Migration Experience&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-old-way-to-configure-tailwind-css-with-css-variables&quot;&gt;The Old Way to Configure Tailwind CSS With CSS Variables&lt;a href=&quot;#the-old-way-to-configure-tailwind-css-with-css-variables&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Previous versions of Tailwind CSS have supported a clunky setup for integrating CSS variables. All config settings have to be kept in a JavaScript (JS) config file &lt;code&gt;tailwind.config.mjs&lt;/code&gt; found in the project’s root directory.&lt;/p&gt;
&lt;p&gt;For instance, I typically define CSS variables for a custom color palette like this one below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;css&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:root&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --primary-color: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 100%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 7%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --link-color: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 100%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 7%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --line-color: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 23%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 90%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --tag-color: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 22%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 88%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A common way to integrate these in the Tailwind CSS system has been to set up the JS config file like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  content&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  theme&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    extend&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;      colors&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;        primary&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;var(--primary-color)&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;        link&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;var(--primary-link-color)&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;        line&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;var(--line-color)&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;        tag&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;var(--tag-color)&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;      },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;  plugins&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One motivation for this approach is that it’s straightforward to implement light and dark themes. To implement a light/dark mode toggle button you could write some Javascript to toggle the values of the color CSS variables. I prefer this method over the recommended workflow which &lt;a href=&quot;https://tailwindcss.com/docs/dark-mode&quot;&gt;requires you to add more styles to components.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The downside of this approach is that adding a new color requires creating a new CSS variable for the color in &lt;code&gt;:root&lt;/code&gt; and a corresponding entry in the JS config file. Fortunately, the newest release of tailwind provides a more optimal solution.&lt;/p&gt;
&lt;h2 id=&quot;introducing-the-new-config-workflow&quot;&gt;Introducing the New Config Workflow&lt;a href=&quot;#introducing-the-new-config-workflow&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Tailwind CSS v4 allows you to configure all of your CSS in a single CSS file, without having to maintain a Javascript config file. Taking the example setup introduced in the last section, this is how it would look in the new system:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;css&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;@import&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;tailwindcss&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;@theme&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --color-primary: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 100%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 7%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --color-link: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 100%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 7%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --color-line: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 23%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 90%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  --color-tag: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;hsl&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;49&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 22%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 88%&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s it! In addition, you no longer need to include the boilerplate found in the old JS config file.&lt;/p&gt;
&lt;p&gt;The tailwind compiler will do the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Add these CSS variables to &lt;code&gt;:root&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Generate utility classes such as &lt;code&gt;bg-primary&lt;/code&gt;, &lt;code&gt;text-primary&lt;/code&gt;, and &lt;code&gt;color-link&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In other words, CSS variables that have the namespace &lt;code&gt;--color-*&lt;/code&gt; will be used to generate new color utility classes.&lt;/p&gt;
&lt;p&gt;I’ve sometimes found it tricky to figure out how to use the other namespaces that have been introduced, &lt;a href=&quot;https://tailwindcss.com/docs/theme#theme-variable-namespaces&quot;&gt;as you can see here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For instance, if you want to create a custom utility class named &lt;code&gt;max-w-large-page&lt;/code&gt;, you’d have to add &lt;code&gt;--container-large-page: 2500px;&lt;/code&gt; to your &lt;code&gt;@theme&lt;/code&gt;. This would generate the desired utility class &lt;code&gt;max-w-large-page&lt;/code&gt; in addition to other classes such as &lt;code&gt;min-w-large-page&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I love this new system because it means I no longer need to context switch between my CSS and JS files when editing design tokens. Everything lives in one CSS file.&lt;/p&gt;
&lt;h2 id=&quot;migration-experience&quot;&gt;Migration Experience&lt;a href=&quot;#migration-experience&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As I refactored my website’s CSS, the migration process itself was surprisingly smooth. Since the new &lt;code&gt;@theme&lt;/code&gt; system maps directly to the old configuration approach, I mainly needed to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Update my astro config to use the new &lt;code&gt;@tailwindcss/vite&lt;/code&gt; plugin. &lt;a href=&quot;https://tailwindcss.com/docs/installation/framework-guides/astro&quot;&gt;Here’s the official guide on how to do that.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Move my design tokens from &lt;code&gt;tailwind.config.mjs&lt;/code&gt; to my CSS file.&lt;/li&gt;
&lt;li&gt;Update the variable naming to match the new namespace conventions.&lt;/li&gt;
&lt;li&gt;Remove the now-unnecessary JavaScript configuration.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Overall, I’m happy with the new version of Tailwind CSS. The main learning curve I’ve identified is in learning which namespaces to use for CSS variables. Nonetheless, I find it even easier than before to set up a design system.&lt;/p&gt;</content:encoded></item><item><title>2024: My Year in Review</title><link>https://bryananthonio.com/blog/2024-in-review/</link><guid isPermaLink="true">https://bryananthonio.com/blog/2024-in-review/</guid><description>Reviewing my highlights from 2024 and plans for 2025.</description><pubDate>Tue, 07 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;2024 was full of significant milestones–from proposing to my partner to establishing myself as a full-stack developer, among other achievements. Let’s unpack it all.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#my-previous-yearly-reviews&quot;&gt;My Previous Yearly Reviews&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#3-key-lessons-i-learned&quot;&gt;3 Key Lessons I Learned&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#reflecting-on-my-goals-for-2024&quot;&gt;Reflecting on My Goals for 2024&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#learn-about-ai-computing&quot;&gt;Learn About AI &amp;amp; Computing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#develop-ui-design-expertise&quot;&gt;Develop UI Design Expertise&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#read-12-books&quot;&gt;Read 12+ books.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#ship-3-projects&quot;&gt;Ship 3 Projects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#publish-12-blog-posts&quot;&gt;Publish 12+ blog posts.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#gain-conversational-fluency-in-spanish&quot;&gt;Gain Conversational Fluency in Spanish&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#other-highlights&quot;&gt;Other Highlights&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#i-got-engaged&quot;&gt;I Got Engaged!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#we-visited-two-new-destinations&quot;&gt;We Visited Two New Destinations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#two-of-my-blog-posts-went-viral&quot;&gt;Two Of My Blog Posts Went Viral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#finished-one-year-as-a-software-developer&quot;&gt;Finished One Year as a Software Developer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#goals-for-2025&quot;&gt;Goals for 2025&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#improve-my-fitness&quot;&gt;Improve My Fitness&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#learn-about-distributed-systems&quot;&gt;Learn About Distributed Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#finish-learn-ui-design-course&quot;&gt;Finish Learn UI Design Course&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#network-with-other-software-developers&quot;&gt;Network with Other Software Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#read-12-books&quot;&gt;Read 12 Books&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;my-previous-yearly-reviews&quot;&gt;My Previous Yearly Reviews&lt;a href=&quot;#my-previous-yearly-reviews&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here are previous yearly reviews that I’ve written:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://bryananthonio.com/blog/2023-in-review/&quot;&gt;2023: My Year in Review&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://bryananthonio.com/blog/2022-in-review/&quot;&gt;2022: My Year in Review&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://bryananthonio.com/blog/2020-in-review/&quot;&gt;2020: My Year in Review&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;3-key-lessons-i-learned&quot;&gt;3 Key Lessons I Learned&lt;a href=&quot;#3-key-lessons-i-learned&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Simplicity is key in problem-solving.&lt;/strong&gt; When approaching complex problems it’s best to find the simplest solution first before adding complexity. A simple solution is often easier to understand, implement, and maintain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sleep is the ultimate productivity hack.&lt;/strong&gt; I finally tamed my insomnia and got 7+ hours of sleep a night more consistently. With good sleep I feel much more creative and inspired.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Every pursuit comes at the expense of another.&lt;/strong&gt; Instead of chasing every interesting path like learning Rust, I learned to ask whether each opportunity truly aligned with my core goals. By forgoing non-essential projects, I could focus deeply on what mattered most to me. Through this, I discovered that doing less, paradoxically, enabled me to achieve more.&lt;/p&gt;
&lt;h2 id=&quot;reflecting-on-my-goals-for-2024&quot;&gt;Reflecting on My Goals for 2024&lt;a href=&quot;#reflecting-on-my-goals-for-2024&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I set goals for 2024 in &lt;a href=&quot;https://bryananthonio.com/blog/2023-in-review/&quot;&gt;my previous yearly review&lt;/a&gt;. Here’s a recap of my progress.&lt;/p&gt;
&lt;h3 id=&quot;learn-about-ai-computing&quot;&gt;Learn About AI &amp;amp; Computing&lt;a href=&quot;#learn-about-ai-computing&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I accomplished part of this goal by &lt;a href=&quot;https://bryananthonio.com/blog/insights-from-nand2tetris-part1/&quot;&gt;completing the first part of the NAND2Tetris course&lt;/a&gt;. This course gave me a lot of insight regarding the hardware components that make modern computers work.&lt;/p&gt;
&lt;p&gt;I considered learning more about LLM technologies. To that end, I purchased &lt;a href=&quot;https://www.manning.com/books/build-a-large-language-model-from-scratch&quot;&gt;this book by Sebastian Raschka&lt;/a&gt; to learn how to build an LLM from scratch and &lt;a href=&quot;https://bryananthonio.com/blog/autogen-weather-chatbot/&quot;&gt;built a weather chatbot&lt;/a&gt; to learn about agents.&lt;/p&gt;
&lt;p&gt;But, my interests eventually shifted onto other topics such as distributed systems. I may revisit this goal sometime in the future.&lt;/p&gt;
&lt;h3 id=&quot;develop-ui-design-expertise&quot;&gt;Develop UI Design Expertise&lt;a href=&quot;#develop-ui-design-expertise&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’m very proud to say that I accomplished this goal by finishing 4 out of 7 modules of the &lt;a href=&quot;https://www.learnui.design&quot;&gt;Learn UI Design course&lt;/a&gt;! I’ve gotten much more confident in my UI design skills since then. I even redesigned my website to apply what I learned.&lt;/p&gt;
&lt;h3 id=&quot;read-12-books&quot;&gt;Read 12+ books.&lt;a href=&quot;#read-12-books&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I read 11 books this year:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Expanse series by James S.A. Corey
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/8855321-leviathan-wakes&quot;&gt;Leviathan Wakes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/12591698-caliban-s-war&quot;&gt;Caliban’s War&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/16131032-abaddon-s-gate&quot;&gt;Abaddon’s Gate&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/18656030-cibola-burn&quot;&gt;Cibola Burn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/27833670-dark-matter&quot;&gt;Dark Matter&lt;/a&gt; by Blake Crouch&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/61439040-1984?ref=nav_sb_ss_1_3&quot;&gt;1984&lt;/a&gt; by George Orwell&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/5129.Brave_New_World&quot;&gt;Brave New World&lt;/a&gt; by Aldous Huxley&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/61190770-chain-gang-all-stars&quot;&gt;Chain-Gang All-Stars&lt;/a&gt; by Nana Kawme Adjei-Brenyah&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/50202953-piranesi&quot;&gt;Piranesi&lt;/a&gt; by Susanna Clarke&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/58502800-engineering-management-for-the-rest-of-us&quot;&gt;Engineering Management for the Rest of Us&lt;/a&gt; by Sarah Drasner&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/10569.On_Writing&quot;&gt;On Writing: A Memoir of the Craft&lt;/a&gt; by Stephen King&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;ship-3-projects&quot;&gt;Ship 3 Projects&lt;a href=&quot;#ship-3-projects&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;My goal for this was to share 3 small projects on Github that I worked on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/photon-collider/celery-task-metadata-persistence&quot;&gt;Celery Task Metadata Persistence&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/photon-collider/rate-limiter-example&quot;&gt;FastAPI Rate Limiter Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/photon-collider/autogen-weather-chatbot-example&quot;&gt;Autogen Weather Chatbot Example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are all toy projects I built to achieve my learning goals.&lt;/p&gt;
&lt;h3 id=&quot;publish-12-blog-posts&quot;&gt;Publish 12+ blog posts.&lt;a href=&quot;#publish-12-blog-posts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I only published 8 blog posts this year. Though I didn’t achieve my goal it may be best to focus on quality rather than quantity. I enjoy writing about technical topics the most so I’ll focus more on that in the coming year.&lt;/p&gt;
&lt;h3 id=&quot;gain-conversational-fluency-in-spanish&quot;&gt;Gain Conversational Fluency in Spanish&lt;a href=&quot;#gain-conversational-fluency-in-spanish&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’ve made this my goal for two years in a row but I’ve yet to make progress on this. Learning a new language requires consistent effort which I haven’t made time for.&lt;/p&gt;
&lt;h2 id=&quot;other-highlights&quot;&gt;Other Highlights&lt;a href=&quot;#other-highlights&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;i-got-engaged&quot;&gt;I Got Engaged!&lt;a href=&quot;#i-got-engaged&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I finally got down on one knee and proposed to my partner! We’re now looking forward to getting married in 2025.&lt;/p&gt;
&lt;h3 id=&quot;we-visited-two-new-destinations&quot;&gt;We Visited Two New Destinations&lt;a href=&quot;#we-visited-two-new-destinations&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Italian Dolomites:&lt;/strong&gt; We explored the Dolomites shortly before summer started. Despite the spotty weather, the views were incredible! I look forward to revisiting this place in the future.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kauai:&lt;/strong&gt; We visited Kauai over Thanksgiving break. We’ve always enjoyed visiting Hawaii once each year and this trip didn’t disappoint!&lt;/li&gt;
&lt;/ul&gt;
&lt;figure class=&quot;photo-wide photo-wide--portrait&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/seceda_1800h_Z1B0nrC.jpg&quot; alt=&quot;The Seceda ridgeline.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1440&quot; height=&quot;1800&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Our view of the Seceda ridgeline.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--portrait&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/kauai_hills_1800w_26rkBU.jpg&quot; alt=&quot;Aerial view of Kauai&apos;s lush, emerald-green mountain ridges with dramatic folded valleys and a distant waterfall visible in the background.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1440&quot; height=&quot;1800&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;One of my favorite views from Kauai.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;two-of-my-blog-posts-went-viral&quot;&gt;Two Of My Blog Posts Went Viral&lt;a href=&quot;#two-of-my-blog-posts-went-viral&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;My blog posts on &lt;a href=&quot;https://bryananthonio.com/blog/creating-simple-rest-api-elixir-phoenix/&quot;&gt;Elixir REST APIs&lt;/a&gt; and &lt;a href=&quot;https://bryananthonio.com/blog/pydantic-custom-dictionary-types/&quot;&gt;Pydantic dictionary types&lt;/a&gt; went viral this year. This came as a surprise to me because I wrote these mostly for my future self. I’m glad others found them useful.&lt;/p&gt;
&lt;p&gt;According to my website analytic these two posts were viewed more than 3,000 times combined in the last 12 months. In examining this, I noticed that the blog post on Pydantic dictionary types was shared broadly on various newsletters, such as &lt;a href=&quot;https://pycoders.com/issues/652&quot;&gt;Pycoder’s Weekly #652&lt;/a&gt;. Otherwise, the vast majority of traffic has come from organic Google searches.&lt;/p&gt;
&lt;h3 id=&quot;finished-one-year-as-a-software-developer&quot;&gt;Finished One Year as a Software Developer&lt;a href=&quot;#finished-one-year-as-a-software-developer&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This was my first full year working as a professional software developer!&lt;/p&gt;
&lt;p&gt;Before this current stint, I worked as a data scientist for 2 years which wasn’t the best fit for my interests. I’m much more content with my current role as a full-stack developer and the technical challenges I get to work on.&lt;/p&gt;
&lt;h2 id=&quot;goals-for-2025&quot;&gt;Goals for 2025&lt;a href=&quot;#goals-for-2025&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Focusing on simplicity, I’ve streamlined my goals for the coming year.&lt;/p&gt;
&lt;h3 id=&quot;improve-my-fitness&quot;&gt;Improve My Fitness&lt;a href=&quot;#improve-my-fitness&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;I’d like to be more consistent about working out at the gym.&lt;/strong&gt; Here are my strength training goals (3 rep maxes):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Squat – 250 lbs&lt;/li&gt;
&lt;li&gt;Bench Press – 180 lbs&lt;/li&gt;
&lt;li&gt;Overhead Press – 125 lbs&lt;/li&gt;
&lt;li&gt;Deadlifts – 300 lbs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;I’m going to work on my cardiovascular fitness as well.&lt;/strong&gt; Since I enjoy running I’m going to use the Nike Running App to manage my workouts. These are my goals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Run a mile in 7 minutes&lt;/li&gt;
&lt;li&gt;Run a 10K&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;I want to take a swim class to improve my breathing technique for freestyle swimming.&lt;/strong&gt; I’d like to incorporate swimming in my exercise routine.&lt;/p&gt;
&lt;h3 id=&quot;learn-about-distributed-systems&quot;&gt;Learn About Distributed Systems&lt;a href=&quot;#learn-about-distributed-systems&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I been doing more and more backend development at work these days. I’d like to learn more about distributed systems to understand how to scale up web applications to support more users.&lt;/p&gt;
&lt;p&gt;I’m aiming to write more about my learnings in future blog posts. I’ve gotten started on this in some of my recent posts regarding &lt;a href=&quot;https://bryananthonio.com/blog/implementing-rate-limiter-fastapi-redis/&quot;&gt;Redis&lt;/a&gt; and &lt;a href=&quot;https://bryananthonio.com/blog/custom-celery-mongodb-backend/&quot;&gt;Celery&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;finish-learn-ui-design-course&quot;&gt;Finish Learn UI Design Course&lt;a href=&quot;#finish-learn-ui-design-course&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’m planning to finish the remaining 3 modules that I have left in this course.&lt;/p&gt;
&lt;h3 id=&quot;network-with-other-software-developers&quot;&gt;Network with Other Software Developers&lt;a href=&quot;#network-with-other-software-developers&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’d like more opportunities to learn from other software developers. I’m aiming to attend one regular meetup each month. I’ve also been interacting more with the web development community on Bluesky.&lt;/p&gt;
&lt;h3 id=&quot;read-12-books&quot;&gt;Read 12 Books&lt;a href=&quot;#read-12-books&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;While I enjoy reading sci-fi, I’m looking to read more books of other genres in 2025. For instance, I’ve heard a lot of good things about Brandon Sanderson’s novels.&lt;/p&gt;</content:encoded></item><item><title>Persisting Custom Task Metadata in MongoDB with Celery</title><link>https://bryananthonio.com/blog/custom-celery-mongodb-backend/</link><guid isPermaLink="true">https://bryananthonio.com/blog/custom-celery-mongodb-backend/</guid><description>How to create and maintain task metadata with Celery&apos;s MongoDB backend.</description><pubDate>Sat, 21 Dec 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://docs.celeryq.dev/en/stable/index.html&quot;&gt;Celery is the de-facto standard framework&lt;/a&gt; for managing task queues in the Python ecosystem. While its default MongoDB backend works well for most use cases, it poses limitations when you need to persist custom task metadata in a database throughout a task’s lifecycle.&lt;/p&gt;
&lt;p&gt;This metadata might include things like original filenames for uploaded files, user-specific processing parameters, or other task-specific data needed to facilitate database queries of interest.&lt;/p&gt;
&lt;p&gt;In particular, when Celery workers start or complete a task, they overwrite the task’s corresponding document in MongoDB. As a result, any custom metadata added to these task documents in advance will be erased.&lt;/p&gt;
&lt;p&gt;This blog post demonstrates how to extend Celery’s MongoDB backend to preserve this metadata.&lt;/p&gt;
&lt;h2 id=&quot;project-setup&quot;&gt;Project Setup&lt;a href=&quot;#project-setup&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this post, let’s assume we’re working with a Celery app that has these settings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Redis as the message broker for distributing tasks&lt;/li&gt;
&lt;li&gt;MongoDB as the result backend for storing task states and outputs&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;a href=&quot;#problem&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;By default, Celery workers will create a document for a corresponding task in the MongoDB backend &lt;em&gt;only&lt;/em&gt; after completing the task. This document has the field &lt;code&gt;result&lt;/code&gt;, which contains the output of the completed task, and a few other fields created by Celery.&lt;/p&gt;
&lt;p&gt;In my use case, I wanted to achieve the following goals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Before a task is dispatched to the Redis queue, the app should create a document, assigned to the task, with custom metadata in the MongoDB backend.&lt;/li&gt;
&lt;li&gt;When a Celery worker starts working on a task, update the &lt;code&gt;status&lt;/code&gt; field in the task’s document to &lt;code&gt;STARTED&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After a worker completes a task, the task’s document should be updated to contain the outputs of the completed task.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;a href=&quot;#solution&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The complete solution involves three key components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implementing a signal handler to create initial task documents.&lt;/li&gt;
&lt;li&gt;Setting a parameter in Celery’s configuration to track when tasks start.&lt;/li&gt;
&lt;li&gt;Creating a custom MongoDB backend to preserve metadata.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s dive into each component and understand how they work together.&lt;/p&gt;
&lt;h3 id=&quot;github-repo&quot;&gt;Github Repo&lt;a href=&quot;#github-repo&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’ve created a repo showcasing a demo of how this works. &lt;a href=&quot;https://github.com/photon-collider/celery-task-metadata-persistence&quot;&gt;Check it out here!&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;create-an-initial-mongodb-document-with-custom-metadata&quot;&gt;Create an Initial Mongodb Document with Custom Metadata&lt;a href=&quot;#create-an-initial-mongodb-document-with-custom-metadata&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The first step is to create an initial document in the database backend with the custom metadata. &lt;a href=&quot;https://docs.celeryq.dev/en/stable/userguide/signals.html#before-task-publish&quot;&gt;Celery provides a decorator&lt;/a&gt; called &lt;code&gt;@before_task_publish.connect&lt;/code&gt; that makes this easy to implement:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;before_task_publish&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;connect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; on_task_publish&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(headers&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, body&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, exchange&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, routing_key&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;kwargs):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    task_id &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; headers.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;    # Body structure is (args, kwargs, embedding)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    task_args, task_kwargs, _ &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    meta &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;upload_date&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: task_kwargs.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;upload_date&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;group_id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: task_kwargs.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;group_id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;filename&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: task_kwargs.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;filename&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    celery_app.backend.collection.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;insert_one&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;_id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; task_id&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;metadata&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; meta&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;status&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;QUEUED&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;result&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;traceback&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;children&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we use the &lt;code&gt;insert_one&lt;/code&gt; method to insert a document in the database with fields specified by the input dictionary.&lt;/p&gt;
&lt;p&gt;For consistency, the document’s ID needs to be the task ID. This ID is how the celery worker will know which document to update after completing a task. We can obtain this ID from the &lt;code&gt;headers&lt;/code&gt; argument.&lt;/p&gt;
&lt;h3 id=&quot;updating-task-status-when-a-worker-starts-a-task&quot;&gt;Updating Task Status When a Worker Starts a Task&lt;a href=&quot;#updating-task-status-when-a-worker-starts-a-task&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Achieving this step only requires setting &lt;code&gt;task_tracking=True&lt;/code&gt; in the Celery app like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;celery_app &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Celery&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(task_track_started&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When this setting is enabled, Celery will automatically update the task’s document with the status &lt;code&gt;STARTED&lt;/code&gt; as soon as a worker begins processing it.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.celeryq.dev/en/stable/userguide/configuration.html#task-track-started&quot;&gt;See here for more details&lt;/a&gt; about this setting.&lt;/p&gt;
&lt;h3 id=&quot;prevent-custom-metadata-from-being-overwritten&quot;&gt;Prevent Custom Metadata from Being Overwritten&lt;a href=&quot;#prevent-custom-metadata-from-being-overwritten&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;By default, when a celery worker completes a task it will overwrite the tasks’s corresponding document in the MongoDB backend. As a consequence, the custom metadata added in the &lt;code&gt;before_task_publish&lt;/code&gt; signal function will be deleted. Let’s examine how this happens.&lt;/p&gt;
&lt;p&gt;When a Celery app is created, it instantiates an object from a backend class that handles logic for how workers should interface with the database backend.&lt;/p&gt;
&lt;p&gt;Because we’re using MongoDB as the backend, Celery will select the class &lt;code&gt;MongoBackend&lt;/code&gt; defined in the &lt;code&gt;celery.backends.mongodb&lt;/code&gt; module. This class has a method called &lt;code&gt;_store_result&lt;/code&gt; that is called when the task’s document needs to be updated or created. In the &lt;a href=&quot;https://github.com/celery/celery/blob/main/celery/backends/mongodb.py#L181&quot;&gt;default implementation&lt;/a&gt;, reproduced below, the task’s document will be overwritten whenever this function is called:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; _store_result&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(self, task_id, result, state,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;                    traceback&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, request&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;kwargs):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Store return value and state of an executed task.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    meta &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;_get_result_meta&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(result&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;encode&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;                                    traceback&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;traceback&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; request&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;                                    format_date&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;    # Add the _id for mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    meta[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;_id&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; task_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;        # this line below replaces the stored document&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.collection.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;replace_one&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;_id&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; task_id&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; meta&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; upsert&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; InvalidDocument &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; exc:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        raise&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; EncodeError&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;exc&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; result&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To my knowledge, the easiest way to prevent this from happening is to create a custom Celery backend to override this method. This requires creating a new class that inherits from the original &lt;code&gt;MongoBackend&lt;/code&gt; class:&lt;/p&gt;

&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# custom_backend.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; celery.backends.mongodb &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; MongoBackend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; kombu.exceptions &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; EncodeError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; InvalidDocument&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; CustomMongoBackend&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;MongoBackend&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; _store_result&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        self, task_id, result, state, traceback&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, request&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;kwargs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    ):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;        # contains task data: result, date_done, traceback, etc..&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        task_info &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;_get_result_meta&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            result&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;encode&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            state&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            traceback&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;traceback&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            request&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            format_date&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        task_info[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;_id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; task_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;            # change to update the document instead of replacing it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt;            self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.collection.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;update_one&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;                {&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;_id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; task_id&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;$set&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; task_info&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;},&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; upsert&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        except&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; InvalidDocument &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; exc:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;            raise&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; EncodeError&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;exc&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; result&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The logic in the new &lt;code&gt;_store_result&lt;/code&gt; is almost the same as the original except for a minor change made to preserve the task document’s custom metadata. It now uses &lt;code&gt;update_one&lt;/code&gt; instead of &lt;code&gt;replace_one&lt;/code&gt; to modify the specified fields while preserving others.&lt;/p&gt;
&lt;p&gt;To use this class, you’ll have to include it in the celery config like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# tasks.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; os&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; celery &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Celery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;MONGODB_HOST &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;mongodb://localhost:27017/celery_result_db&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;CELERY_BROKER_URL &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;redis://redis:6379/0&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;celery_app &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Celery&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;tasks&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    broker&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;CELERY_BROKER_URL&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    backend&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;app.custom_backend.CustomBackend&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    task_track_started&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    mongodb_backend_settings&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;host&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; MONGODB_HOST&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;database&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;tasks&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;taskmeta_collection&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;task_results&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This assumes the following project structure:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;app/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;├── __init__.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;├── main.py # FastAPI app entry point&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;├── tasks.py # Celery app configuration and task definitions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;└── custom_backend.py # Custom MongoDB backend implementation&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this post, we explored how to extend Celery’s MongoDB backend to preserve custom task metadata. By creating task documents in advance via a signal handler and modifying how the backend stores results, we prevented metadata loss while maintaining Celery’s core functionality.&lt;/p&gt;</content:encoded></item><item><title>Implementing a Rate Limiter with FastAPI and Redis</title><link>https://bryananthonio.com/blog/implementing-rate-limiter-fastapi-redis/</link><guid isPermaLink="true">https://bryananthonio.com/blog/implementing-rate-limiter-fastapi-redis/</guid><description>A step-by-step guide on building a rate limiter from scratch.</description><pubDate>Mon, 02 Dec 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Rate limiting mechanisms are a crucial feature of public APIs. They protect these services from becoming overloaded with too many requests while providing fair access to all clients.&lt;/p&gt;
&lt;p&gt;Every public API service has limits on how many API calls clients can make over a given duration. But, how are these rate limiting mechanisms implemented?&lt;/p&gt;
&lt;p&gt;To answer that, we’ll explore how to implement a basic rate limiter from scratch for an API service built with &lt;a href=&quot;https://fastapi.tiangolo.com&quot;&gt;FastAPI&lt;/a&gt; by using &lt;a href=&quot;https://redis.io&quot;&gt;Redis&lt;/a&gt;. We’ll build the same type of system that powers API request management in many enterprise platforms.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#basic-design-of-a-rate-limiter&quot;&gt;Basic Design of a Rate Limiter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#token-bucket-algorithm-for-rate-limiting&quot;&gt;Token Bucket Algorithm for Rate Limiting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#rate-limiter-implementation&quot;&gt;Rate Limiter Implementation&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#github-repo&quot;&gt;Github Repo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#redis-lua-script&quot;&gt;Redis Lua Script&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#fastapi-middleware&quot;&gt;FastAPI Middleware&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#testing-our-rate-limiter&quot;&gt;Testing Our Rate Limiter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;basic-design-of-a-rate-limiter&quot;&gt;Basic Design of a Rate Limiter&lt;a href=&quot;#basic-design-of-a-rate-limiter&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Incoming client requests are first routed to a middleware, which acts as a gatekeeper to the API backend service. Here, the middleware checks whether a client’s request needs to be rate limited before allowing or rejecting them.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/system-diagram_ZOmIch.webp&quot; alt=&quot;Architectural diagram showing Client, Middleware with Redis, and API Server components connected by directional arrows.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1220&quot; height=&quot;752&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;High-level architecture for a rate limiter.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;To do this, it leverages a cache provided by a Redis service. Here, Redis is a high-performance key-value store we’ll use to track to track API usage for each client.&lt;/p&gt;
&lt;p&gt;Putting this all together requires implementing an algorithm to determine when to rate limit a client. While there are various algorithms available, we’ll focus on one of the simplest: the token bucket algorithm.&lt;/p&gt;
&lt;h2 id=&quot;token-bucket-algorithm-for-rate-limiting&quot;&gt;Token Bucket Algorithm for Rate Limiting&lt;a href=&quot;#token-bucket-algorithm-for-rate-limiting&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Token_bucket&quot;&gt;token bucket algorithm&lt;/a&gt; is a straightforward approach to rate limiting. Imagine each client is assigned a bucket that contains a set number of tokens.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/token-bucket_Z2s0iXC.webp&quot; alt=&quot;Flowchart showing token bucket decision process for accepting or rejecting requests based on token availability.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2308&quot; height=&quot;1064&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;Token bucket algorithm implementation flow.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Each time a client makes an API call, we remove one token from their bucket. In addition, the client’s bucket is refilled with tokens at predetermined rate.&lt;/p&gt;
&lt;p&gt;If a client has no more tokens in their bucket, they can’t make new API requests until more tokens are added.&lt;/p&gt;
&lt;p&gt;While this algorithm is intuitive, it requires careful consideration to choose the bucket capacity and token replenishment rate that are appropriate for your use case.&lt;/p&gt;
&lt;h2 id=&quot;rate-limiter-implementation&quot;&gt;Rate Limiter Implementation&lt;a href=&quot;#rate-limiter-implementation&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this section, we’ll write code to implement a rate limiter with the token bucket algorithm.&lt;/p&gt;
&lt;p&gt;Note that if you’re trying to do this for a production system, there are libraries such as &lt;a href=&quot;https://github.com/laurentS/slowapi&quot;&gt;slowapi&lt;/a&gt; and &lt;a href=&quot;https://github.com/long2ice/fastapi-limiter&quot;&gt;fastapi-limiter&lt;/a&gt; that are sufficient for this task.&lt;/p&gt;
&lt;h3 id=&quot;github-repo&quot;&gt;Github Repo&lt;a href=&quot;#github-repo&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You can find the code for this section in &lt;a href=&quot;https://github.com/photon-collider/rate-limiter-example&quot;&gt;this Github repository&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;redis-lua-script&quot;&gt;Redis Lua Script&lt;a href=&quot;#redis-lua-script&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;First, we’ll start by defining the logic that our Redis service needs to track client requests. Redis allows us to define this logic by writing a Lua script. If you’re curious of the benefits of Lua scripting in this case, &lt;a href=&quot;https://redis.io/docs/latest/develop/interact/programmability/eval-intro/&quot;&gt;check out this resource for more details&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The Lua script described in this section is an edited version of the one presented in &lt;a href=&quot;https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#request-rate-limiter&quot;&gt;this Github gist&lt;/a&gt;. In this project, the contents of this script will be stored in a file called &lt;code&gt;request_rate_limiter.lua&lt;/code&gt;. First, we’ll define variables that we’ll use in the rest of the script:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; tokens_key &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; KEYS[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- key used to identify client&amp;#39;s bucket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; timestamp_key &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; KEYS[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- key to obtain unix time of client&amp;#39;s previous API request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; refill_rate &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tonumber&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(ARGV[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- token bucket refill rate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; capacity &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tonumber&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(ARGV[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- token bucket capacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time_now &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tonumber&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(ARGV[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- current unix time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; requested &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tonumber&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(ARGV[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- number of tokens requested&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- time to completely fill an empty bucket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; fill_time &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; capacity&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;refill_rate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- time to live for cached values&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ttl &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; math.floor&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(fill_time&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the middleware will provide the keys, given as&lt;code&gt;KEYS&lt;/code&gt;, and arguments, given as&lt;code&gt;ARGV&lt;/code&gt;, for Redis to execute this script. We’ll see more details on that in the next section.&lt;/p&gt;
&lt;p&gt;The next step focuses on fetching the cached values of the clients token count, given as &lt;code&gt;last_tokens&lt;/code&gt;, and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Unix_time&quot;&gt;Unix time&lt;/a&gt; of the client’s previous API request &lt;code&gt;time_last_refreshed&lt;/code&gt;. If either of these values are nonexistent in the cache, we assign default values to them.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- obtain current tokens in client&amp;#39;s bucket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; last_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tonumber&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(redis.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;get&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, tokens_key))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- if the cached value is empty, then assign the client a full token bucket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; last_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  last_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; capacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- if the cached value is empty, then assign the current unix time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time_last_refreshed &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tonumber&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(redis.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;get&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, timestamp_key))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time_last_refreshed &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    time_last_refreshed &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time_now&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After that, we now compute the number of tokens the client has gained in the time since their last request:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; delta_time_since_refresh &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; math.max&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, time_now &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time_last_refreshed)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; replenished_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; delta_time_since_refresh &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; refill_rate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;-- make sure the number of tokens doesn&amp;#39;t exceed bucket&amp;#39;s capacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; filled_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; math.min&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(capacity, last_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; replenished_tokens)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From here, we can now decide whether to allow the client’s request:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; allowed &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; filled_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; requested&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; new_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; filled_tokens&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; allowed &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  new_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; filled_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; requested&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;redis.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;setex&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, tokens_key, ttl, new_tokens)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;redis.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;setex&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, timestamp_key, ttl, time_now)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; { allowed, new_tokens }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;redis.call&lt;/code&gt; function is used here to cache quantity of tokens left in the client’s bucket and the current timestamp. We assign a time to life for these cached values in case the client doesn’t make any further requests for an extended period.&lt;/p&gt;
&lt;p&gt;The output values are returned as a tuple to the middleware, which we’ll define next.&lt;/p&gt;
&lt;h3 id=&quot;fastapi-middleware&quot;&gt;FastAPI Middleware&lt;a href=&quot;#fastapi-middleware&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://fastapi.tiangolo.com/tutorial/middleware/#create-a-middleware&quot;&gt;FastAPI allows you to define the middleware&lt;/a&gt; by using the &lt;code&gt;middleware(“http”)&lt;/code&gt; decorator like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; fastapi &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; FastAPI, Request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; FastAPI&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;middleware&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; check_request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(request: Request, call_next):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    client_ip &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; request.client.host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; call_next&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This middleware defined above extracts the client’s IP address, which we can we can use to distinguish between clients. After that, it uses the &lt;code&gt;call_next&lt;/code&gt; function to route the request to the endpoint specified by the client’s API call.&lt;/p&gt;
&lt;p&gt;We can edit this middleware to implement rate limiting with a few more additions. First, we need to specify the max number of tokens a client has access to and the rate at which tokens are replenished. We also need to label how many tokens left that client has access to and the timestamp during which the client’s token bucket as last replenished.&lt;/p&gt;
&lt;p&gt;These additions are shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; fastapi &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; FastAPI, Request&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;BUCKET_REPLENISH_RATE &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;BUCKET_CAPACITY &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;TOKENS_PER_REQUEST &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; FastAPI&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;middleware&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; check_request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(request: Request, call_next):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    client_ip &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; request.client.host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;request_rate_limiter.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; client_ip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    keys &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;.tokens&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;.timestamp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [BUCKET_REPLENISH_RATE, BUCKET_CAPACITY, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(), TOKENS_PER_REQUEST]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; call_next&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we’ve define two keys to track a client’s tokens and the timestamp of the client’s last request. We’ve also created a list of arguments that will be provided to the Redis Lua script.&lt;/p&gt;
&lt;p&gt;Speaking of which, we now need to load the script and allow the middleware to communicate with the Redis service.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://redis.io/docs/latest/commands/script-load/&quot;&gt;Redis has a function&lt;/a&gt; to load a script into its cache and provide a hash that can be used to invoke this script.&lt;/p&gt;
&lt;p&gt;The snippet below creates an asynchronous Redis client, loads the Lua script, and provides the corresponding hash to the variable &lt;code&gt;script_hash&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; contextlib &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; asynccontextmanager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; redis.asyncio &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Redis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; os&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;redis_client &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;script_hash &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;asynccontextmanager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; lifespan&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(app: FastAPI):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    global&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; redis_client, script_hash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    redis_client &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Redis&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(host&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;getenv&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;REDIS_HOST&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; port&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;getenv&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;REDIS_PORT&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    script_hash &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_script_hash&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;redis_client&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;./request_rate_limiter.lua&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    yield&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; redis_client.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;app &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; FastAPI&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(lifespan&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lifespan&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This uses the &lt;a href=&quot;https://fastapi.tiangolo.com/advanced/events/&quot;&gt;lifespan events&lt;/a&gt; API from FastAPI to execute this logic when the FastAPI server starts up. The app will close the client connection to the Redis service upon shutdown.&lt;/p&gt;
&lt;p&gt;From here, we can implement rate limiting as shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; fastAPI &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;middleware&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; check_request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(request: Request, call_next):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    client_ip &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; request.client.host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;request_rate_limiter.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; client_ip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    keys &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;.tokens&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;.timestamp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [BUCKET_REPLENISH_RATE, BUCKET_CAPACITY, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(), TOKENS_PER_REQUEST]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		allowed, new_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; redis_client.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;evalsha&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;script_hash&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; allowed:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; call_next&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;			return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		else&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;			return&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Response&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            content&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Too many requests&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            status_code&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;HTTP_429_TOO_MANY_REQUESTS&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, it’s best practice provide the headers &lt;code&gt;X-Rate-Limit-Remaining&lt;/code&gt; and &lt;code&gt;X-RateLimit-Reset&lt;/code&gt; in the response. Here’s a revised version of the middleware that accomplishes this:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;middleware&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; check_request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(request: Request, call_next):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    client_ip &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; request.client.host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;request_rate_limiter.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; client_ip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;    # Redis keys for token bucket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    keys &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;.tokens&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;.timestamp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [BUCKET_REPLENISH_RATE, BUCKET_CAPACITY, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(), TOKENS_PER_REQUEST]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    allowed, new_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; redis_client.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;evalsha&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;script_hash&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    retry_after &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; math.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ceil&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;TOKENS_PER_REQUEST &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; BUCKET_REPLENISH_RATE&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; allowed:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; call_next&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;request&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        response.headers[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;X-RateLimit-Remaining&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;new_tokens&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        response.headers[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Retry-After&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;retry_after&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Response&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            content&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Too many requests&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            status_code&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;HTTP_429_TOO_MANY_REQUESTS&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            media_type&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;text/plain&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            headers&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;X-RateLimit-Remaining&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;Retry-After&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;retry_after&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, let’s add an example endpoint:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/api/example&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_example&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;message&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;This is a rate limited endpoint&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the next section, we’ll test our rate limiter on client requests to this endpoint.&lt;/p&gt;
&lt;h2 id=&quot;testing-our-rate-limiter&quot;&gt;Testing Our Rate Limiter&lt;a href=&quot;#testing-our-rate-limiter&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here’s a curl script we can use to test how the rate limiter handles several consecutive client requests:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;API_URL&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;http://localhost:8000/api/example&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;REQUESTS&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;DELAY&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;0.01&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;Testing rate limits on &lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;$API_URL&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;$REQUESTS&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; requests, &lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;${DELAY}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;s delay)&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;----------------------------------------&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;seq&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; $REQUESTS); &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    response&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -i&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -s&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -w&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;\n%{http_code}&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; $API_URL)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    status_code&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;$response&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; tail&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -n1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    printf&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;\nRequest %d: Status %s%d\033[0m&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; $i &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        $([[ $status_code &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ]] &amp;amp;&amp;amp; &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;\033[32m&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;\033[31m&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) $status_code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;$response&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;X-RateLimit-Remaining|Retry-After&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    [[ $i &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-lt&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; $REQUESTS ]] &amp;amp;&amp;amp; &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; $DELAY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -e&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;\n----------------------------------------\nTest completed!&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This script executes 20 requests that are spaced apart by 10 ms. The script outputs the following:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Testing&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; rate&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; limits&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; on&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; http://localhost:8000/api/example&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; (20 &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;requests,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 0.01s&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; delay&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;----------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 1:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 2:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 3:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 4:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 5:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 6:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 7:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 8:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 9:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 10:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 11:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 12:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 13:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 14:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 15:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 16:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 17:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 18:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 19:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 429&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 20:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Status&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; 429-e&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;----------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Test&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; completed!&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We see that the first 10 requests succeed but that subsequent requests are rate limited. The 15th request succeeds because a token has been added to the token bucket. The other requests fail because not enough time has passed between these requests for the token bucket to refill.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this guide, we built a custom rate limiter using FastAPI and Redis. While this implementation illustrates core concepts like the token bucket algorithm and middleware integration, you should consider using libraries like slowapi or fastapi-limiter in production systems.&lt;/p&gt;
&lt;p&gt;Even so, understanding the fundamentals of rate limiters is a necessary step toward designing scalable and resilient API systems.&lt;/p&gt;</content:encoded></item><item><title>Develop a Smart Weather Chatbot Using AutoGen</title><link>https://bryananthonio.com/blog/autogen-weather-chatbot/</link><guid isPermaLink="true">https://bryananthonio.com/blog/autogen-weather-chatbot/</guid><description>Learn to code an AI assistant that understands and responds to weather queries.</description><pubDate>Sat, 27 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;One exciting trend in AI is the use of large language models (LLMs) to create agents that can autonomously perform tasks. As someone fascinated by these developments, I’ve been trying to understand how these agents are created.&lt;/p&gt;
&lt;p&gt;There are various frameworks for this purpose, including &lt;a href=&quot;https://langchain-ai.github.io/langgraph/&quot;&gt;LangGraph&lt;/a&gt;, &lt;a href=&quot;https://www.crewai.com&quot;&gt;CrewAI&lt;/a&gt; and &lt;a href=&quot;https://microsoft.github.io/autogen/&quot;&gt;AutoGen&lt;/a&gt;. After taking &lt;a href=&quot;https://www.deeplearning.ai/short-courses/ai-agentic-design-patterns-with-autogen/&quot;&gt;this short course on creating agentic workflows using Autogen&lt;/a&gt;, I leveraged this framework to build an agent.&lt;/p&gt;
&lt;p&gt;I started with a simple example: a weather chatbot that can fetch current weather information to answer queries. In this blog post, I’ll walk through all the steps needed to put this together.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#what-is-autogen&quot;&gt;What is AutoGen?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#overall-sketch&quot;&gt;Overall Sketch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#jupyter-notebook&quot;&gt;Jupyter Notebook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#getting-api-keys&quot;&gt;Getting API Keys&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#creating-the-weather-assistant-agent&quot;&gt;Creating the Weather Assistant Agent&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#implementing-tool-use&quot;&gt;Implementing Tool Use&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#chatting-with-the-weather-assistant&quot;&gt;Chatting with the Weather Assistant&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#demo&quot;&gt;Demo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-is-autogen&quot;&gt;What is AutoGen?&lt;a href=&quot;#what-is-autogen&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;AutoGen is an &lt;a href=&quot;https://github.com/microsoft/autogen&quot;&gt;open-source Python framework&lt;/a&gt; for creating agentic AI systems using LLMs. Using this framework, you can create agents that interact with each other, use tools, and participate in custom conversation patterns.&lt;/p&gt;
&lt;h2 id=&quot;overall-sketch&quot;&gt;Overall Sketch&lt;a href=&quot;#overall-sketch&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here’s a diagram of how we’ll set up the workflow for the weather chatbot:&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/autogen_chatbot_sketch_Z1aGjo1.webp&quot; alt=&quot;Diagram of a NAND gate and its truth table&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1977&quot; height=&quot;624&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;The three agents we&amp;#39;ll create in this exercise.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The user proxy agent facilitates communications between the user and the weather assistant. It prompts the user to provide feedback whenever it receives a response from the weather assistant.&lt;/p&gt;
&lt;p&gt;Furthermore, the weather assistant responds to user queries using a LLM. It also fetches information about current weather conditions by making API requests to OpenWeather REST API with the help of the weather api proxy agent.&lt;/p&gt;
&lt;h2 id=&quot;jupyter-notebook&quot;&gt;Jupyter Notebook&lt;a href=&quot;#jupyter-notebook&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can find the code for this example &lt;a href=&quot;https://github.com/photon-collider/autogen-weather-chatbot-example&quot;&gt;in this Github repo&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;getting-api-keys&quot;&gt;Getting API Keys&lt;a href=&quot;#getting-api-keys&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For this weather chatbot example, you’ll need to get API keys from OpenAI and OpenWeather. Follow the instructions in these links to get started:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://platform.openai.com/docs/quickstart/step-2-set-up-your-api-key&quot;&gt;OpenAI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://openweathermap.org/one-call-transfer#how&quot;&gt;OpenWeather&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Create these two global variables to hold the API keys once you have them:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;OPENAI_API_KEY &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&amp;lt;insert-api-key&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;OPENWEATHER_API_KEY &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&amp;lt;insert-api-key&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;creating-the-weather-assistant-agent&quot;&gt;Creating the Weather Assistant Agent&lt;a href=&quot;#creating-the-weather-assistant-agent&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;AutoGen provides the &lt;code&gt;ConversableAgent&lt;/code&gt; class for creating custom agents. We can write the following to create our weather assistant agent:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; autogen &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; ConversableAgent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;llm_config &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;config_list&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: [{&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;model&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;gpt-4o-mini&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;api_key&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: OPENAI_API_KEY}],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;cache_seed&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;weather_assistant &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ConversableAgent&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Weather Assistant&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    system_message&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;You are a helpful AI weather assistant. &amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;You can help with providing recommendations to the user based on available weather data. &amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;    &amp;quot;Return &amp;#39;TERMINATE&amp;#39; when the task is done.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    llm_config&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;llm_config&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    is_termination_msg&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;lambda&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; msg:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;terminate&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; msg&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;content&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using this class, you can name agents and provide them a system prompt using the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;system_message&lt;/code&gt; arguments, respectively.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;llm_config&lt;/code&gt; argument sets up parameters for LLM used by the agent and an API key for. This example uses &lt;code&gt;gpt-4o-mini&lt;/code&gt;. By default, Autogen will cache the agent’s responses. Setting the &lt;code&gt;cache_seed&lt;/code&gt; to &lt;code&gt;None&lt;/code&gt; disables this.&lt;/p&gt;
&lt;p&gt;You can also instruct an agent on how to end conversations using the system prompt. Here, we’ve instructed the agent to “Return ‘TERMINATE’ when the task is done.” in the &lt;code&gt;is_termination_msg&lt;/code&gt; argument. The agent will check if the word “terminate” appears in its speech output to decide whether to end the conversation.&lt;/p&gt;
&lt;h2 id=&quot;implementing-tool-use&quot;&gt;Implementing Tool Use&lt;a href=&quot;#implementing-tool-use&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;To implement tool use, we have to first create the proxy agent responsible for sending requests to the OpenWeather API and routing the API response to the weather assistant:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;weather_api_proxy &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ConversableAgent&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Weather API Proxy&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    llm_config&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    default_auto_reply&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Make an API request to get the latest weather as needed&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    human_input_mode&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;NEVER&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    max_consecutive_auto_reply&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We set the LLM config to &lt;code&gt;False&lt;/code&gt; because this agent won’t use an LLM to generate its responses.&lt;/p&gt;
&lt;p&gt;We’ll now define a function for getting the weather using the OpenWeather REST API:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; httpx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_current_weather&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    lat: Annotated[&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Latitude of the weather location&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    lon: Annotated[&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Longitude of the weather location&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) -&amp;gt; &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    base_url &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;https://api.openweathermap.org/data/3.0/onecall&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    arguments &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;?lat=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lat&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;amp;lon=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lon&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;amp;appid=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;OPENWEATHER_API_KEY&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;amp;units=metric&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    endpoint &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;base_url&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}{&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;arguments&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    response &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; httpx.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;endpoint&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; response.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function uses the &lt;code&gt;httpx&lt;/code&gt; library to fetch current weather information from the API endpoint for the input latitude and longitude. Learn more details about the API response schema &lt;a href=&quot;https://openweathermap.org/api/one-call-3#example&quot;&gt;by reading here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next, we need to register the function with the agents defined earlier:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; autogen &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; register_function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;register_function&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    get_current_weather&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    caller&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;weather_assistant&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    executor&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;weather_api_proxy&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    description&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;A tool for obtaining weather information. Units are given in metric.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this scheme, the weather assistant retrieves weather information from the OpenWeather API by making a request to the Weather API Proxy.&lt;/p&gt;
&lt;p&gt;Furthermore, the Weather API Proxy agent executes the request and returns the API response to the weather assistant. This is why the &lt;code&gt;caller&lt;/code&gt; and &lt;code&gt;executor&lt;/code&gt; arguments are assigned to the &lt;code&gt;weather_assistant&lt;/code&gt; and the &lt;code&gt;weather_api_proxy&lt;/code&gt;, respectively.&lt;/p&gt;
&lt;p&gt;Finally, the &lt;code&gt;description&lt;/code&gt; argument provides context to the weather assistant on what the tool is to be used for.&lt;/p&gt;
&lt;h2 id=&quot;chatting-with-the-weather-assistant&quot;&gt;Chatting with the Weather Assistant&lt;a href=&quot;#chatting-with-the-weather-assistant&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The next step involves creating a user proxy agent to facilitate communication with the weather assistant:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;user_proxy &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ConversableAgent&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;User&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    llm_config&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    is_termination_msg&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;lambda&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; msg:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; msg&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;content&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; is&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    or&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;terminate&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; msg&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;content&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    human_input_mode&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;ALWAYS&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With argument &lt;code&gt;human_input_mode=&amp;quot;ALWAYS&amp;quot;&lt;/code&gt;, the agent will prompt the user to respond to messages it receives from the weather assistant.&lt;/p&gt;
&lt;p&gt;Now, let’s set up the chat between the user proxy agent and the weather assistant. We can create the initial message like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;chats &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;sender&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: weather_assistant,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;recipient&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: user_proxy,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;message&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;Hello, I&amp;#39;m here to provide recommendations based on the current weather.&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;How may I help you?&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;summary_method&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;reflection_with_llm&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;max_turns&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This defines the sender of the initial message and its recipient. The &lt;code&gt;summary_method&lt;/code&gt; set to &lt;code&gt;”reflection_with_llm&amp;quot;&lt;/code&gt; will get the weather assistant to generate and share a summary of the chat after it ends. Here, &lt;code&gt;max_turns&lt;/code&gt; will allow the conversation to take two round trips between the user proxy agent and the weather assistant.&lt;/p&gt;
&lt;p&gt;We can now start the chat by running the following command:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; autogen &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; initiate_chats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;initiate_chats&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;chats&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;demo&quot;&gt;Demo&lt;a href=&quot;#demo&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here’s an example of a chat looks like:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;********************************************************************************&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Starting a new chat....&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;********************************************************************************&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Assistant (to User):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hello, I&amp;#39;m here to provide recommendations based on the current weather.How may I help you?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;--------------------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Provide feedback to Assistant. Press enter to skip and use auto-reply, or type &amp;#39;exit&amp;#39; to end the conversation:  I&amp;#39;m in Los Angeles, CA. Do I need an umbrella right now?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;User (to Assistant):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;I&amp;#39;m in Los Angeles, CA. Do I need an umbrella right now?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;--------------------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Assistant (to Weather API Proxy):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;***** Suggested tool call (call_06KXxPULAtvhPFwxpzXhYftQ): get_current_weather *****&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Arguments:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;{&amp;quot;lat&amp;quot;:34.0522,&amp;quot;lon&amp;quot;:-118.2437}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;************************************************************************************&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;--------------------------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Assistant (to User):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Currently in Los Angeles, CA, there are scattered clouds, but there is no precipitation expected.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The chance of rain is 0%. Therefore, you do not need an umbrella right now.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I’ve trimmed the chat log to contain the most relevant sections. The full log contains more content such as the conversation between the weather API proxy and the weather assistant.&lt;/p&gt;
&lt;p&gt;What’s interesting is that the LLM used for the weather assistant already has implicit knowledge of the coordinates of Los Angeles, CA. In more practical cases it would be good to validate the coordinates generated by the LLM to ensure they are correct.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this blog post, I’ve illustrated how to set up a weather assistant using the Autogen library. This included a walkthrough on implementing tool use and the steps for chatting with our agent.&lt;/p&gt;
&lt;p&gt;As we’ve seen, modern frameworks offer helpful abstractions to build functional agents. With the relentless pace of AI innovation, it’s exciting to imagine how much easier and more versatile agent creation might become in the near future.&lt;/p&gt;</content:encoded></item><item><title>Custom Dictionary Types in Pydantic</title><link>https://bryananthonio.com/blog/pydantic-custom-dictionary-types/</link><guid isPermaLink="true">https://bryananthonio.com/blog/pydantic-custom-dictionary-types/</guid><description>A quick primer on leveraging custom root types for this task.</description><pubDate>Wed, 19 Jun 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In one of my projects at work, I wanted to define a custom dictionary type using &lt;a href=&quot;https://github.com/pydantic/pydantic&quot;&gt;Pydantic&lt;/a&gt;. The aim was to define the keys using an &lt;code&gt;Enum&lt;/code&gt; class and values using a model type.&lt;/p&gt;
&lt;p&gt;It wasn’t obvious at first how to approach this task. But, after some troubleshooting, I found a reasonable long-term solution.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#the-problem&quot;&gt;The Problem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#brute-force-approach&quot;&gt;Brute-force Approach&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#final-solution&quot;&gt;Final Solution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;a href=&quot;#the-problem&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I had already defined the &lt;code&gt;Enum&lt;/code&gt; and model types, which I used throughout the codebase. For example, the types looked like these:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; enum &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Enum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pydantic &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; BaseModel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalSpecies&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	LION &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;lion&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	DOLPHIN &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;dolphin&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	ZEBRA &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;zebra&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalData&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;BaseModel&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	habitat: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	diet: &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;str&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we have an &lt;code&gt;Enum&lt;/code&gt; type containing different animal species and a model containing data about a given species.&lt;/p&gt;
&lt;p&gt;What if we want to create a dictionary with keys defined by &lt;code&gt;AnimalSpecies&lt;/code&gt; and values defined by &lt;code&gt;AnimalData&lt;/code&gt;?&lt;/p&gt;
&lt;h2 id=&quot;brute-force-approach&quot;&gt;Brute-force Approach&lt;a href=&quot;#brute-force-approach&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A quick way to do this would be to write&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalDict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;BaseModel&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	lion: AnimalData&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	dolphin: AnimalData&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	zebra: AnimalData&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This would work but it may lead to bugs later if someone added a new field to &lt;code&gt;AnimalSpecies&lt;/code&gt; without adding a corresponding field to &lt;code&gt;AnimalDict&lt;/code&gt;. I wanted to find an easier way to do this.&lt;/p&gt;
&lt;h2 id=&quot;final-solution&quot;&gt;Final Solution&lt;a href=&quot;#final-solution&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After digging through the documentation, I discovered that I could achieve my goals using &lt;a href=&quot;https://docs.pydantic.dev/latest/concepts/models/#rootmodel-and-custom-root-types&quot;&gt;custom root types&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Using the &lt;code&gt;RootModel&lt;/code&gt; class, we can rewrite the &lt;code&gt;AnimalDict&lt;/code&gt; class as follows:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pydantic &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; RootModel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; typing &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Dict&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalDict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;RootModel&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    root: Dict[AnimalSpecies, AnimalData]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; __getitem__&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(self, key: AnimalSpecies):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.root[key]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; __setitem__&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(self, key: AnimalSpecies, value: AnimalData):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.root[key] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;model_validator&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(mode&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;after&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; check_dictionary_types&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(self) -&amp;gt; Self:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        keys &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.root.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; key &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; keys:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; isinstance&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalSpecies&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;                raise&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; TypeError&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Dictionary key &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; isn&amp;#39;t of type AnimalSpecies&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            value &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.root[key]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; isinstance&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalData&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;                raise&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; TypeError&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;                    f&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;The object &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; bound to key &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; isn&amp;#39;t of type AnimalData&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;                )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6;font-style:italic&quot;&gt; self&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s how to use this class in practice:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;animals &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalDict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;animals[AnimalSpecies.LION] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalData&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    habitat&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;cave&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; diet&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;meat&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The model validator function &lt;code&gt;check_dictionary_types&lt;/code&gt; allows the &lt;code&gt;model_validate&lt;/code&gt; method to ensure that the root object obeys the schema. This function will trigger a type error if either a key or a value isn’t of the appropriate type.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;example &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; AnimalDict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;example[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;o&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# ⚠️ This line below will trigger an error ⚠️&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;AnimalDict.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;model_validate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;example&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Pydantic’s custom root types provide a flexible solution to define and validate complex types that might be challenging to express using other methods. By leveraging the &lt;code&gt;RootModel&lt;/code&gt; class, I created a dictionary type for my specific use case.&lt;/p&gt;
&lt;p&gt;This approach provides a scalable way to validate custom dictionary types throughout my codebase.&lt;/p&gt;</content:encoded></item><item><title>What I Learned About Computers in Nand2Tetris Part 1</title><link>https://bryananthonio.com/blog/insights-from-nand2tetris-part1/</link><guid isPermaLink="true">https://bryananthonio.com/blog/insights-from-nand2tetris-part1/</guid><description>Sharing key insights on how computers work, from logic gates and binary arithmetic to assembly languages.</description><pubDate>Sun, 24 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I’ve been so captivated by computers, yet I’ve never really stopped to learn how they work. This year I’m aiming to solve this mystery by completing the &lt;a href=&quot;https://www.nand2tetris.org&quot;&gt;Nand2Tetris course&lt;/a&gt;. I recently finished the first part of the course, which consists of 6 projects.&lt;/p&gt;
&lt;p&gt;Along the way, I built a 16-bit computer capable of performing integer arithmetic and managing input/output (I/O) operations from a keyboard and screen. I also wrote an assembler to convert programs written in a basic assembly language into binary code for this computer to run. In this post, I’ll share my favorite insights on computing from the course.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#everything-can-be-built-using-nand-gates&quot;&gt;Everything can be Built Using NAND Gates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#a-clever-binary-representation-for-signed-integers&quot;&gt;A Clever Binary Representation for Signed Integers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#what-clock-speeds-represent&quot;&gt;What Clock Speeds Represent&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#how-computers-interface-with-io-devices&quot;&gt;How Computers Interface with I/O Devices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#basic-structure-of-a-modern-computer&quot;&gt;Basic Structure of a Modern Computer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#the-purpose-of-assembly-languages&quot;&gt;The Purpose of Assembly Languages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;everything-can-be-built-using-nand-gates&quot;&gt;Everything can be Built Using NAND Gates&lt;a href=&quot;#everything-can-be-built-using-nand-gates&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It was surprising to learn that all hardware in a computer can be built using only NAND gates. The first two projects made this clear.&lt;/p&gt;
&lt;figure class=&quot;figure&quot; style=&quot;max-width: 400px; margin-inline: auto; margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/NAND%20Gate_1HxD8E.png&quot; alt=&quot;Diagram of a NAND gate and its truth table&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1006&quot; height=&quot;415&quot; class=&quot;block&quot;&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;NAND gates are fundamental building blocks of computer hardware systems.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Here, I started by building basic logic gates and leveraging these to construct more complex devices, such as an Arithmetic Logic Unit (ALU), that are required for binary arithmetic operations.&lt;/p&gt;
&lt;p&gt;This understanding helped me grasp how computers interpret binary programs. Binary instructions can serve as control parameters for hardware devices. For instance, the ALU I built has 6 bits that dictate which arithmetic operations it should perform. These control bits are just inputs to the NAND logic gates that comprise the ALU.&lt;/p&gt;
&lt;h2 id=&quot;a-clever-binary-representation-for-signed-integers&quot;&gt;A Clever Binary Representation for Signed Integers&lt;a href=&quot;#a-clever-binary-representation-for-signed-integers&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The method known as the &lt;a href=&quot;https://en.wikipedia.org/wiki/Two%27s_complement&quot;&gt;Two’s Complement&lt;/a&gt; provides a convenient way to represent signed integers in binary. In an &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;-bit system, this method can represent numbers in the range of &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;-2^{(n-1)}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.9713em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.888em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mopen mtight&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mbin mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mclose mtight&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; to &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;2^{(n-1)} - 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.9713em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.888em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mopen mtight&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mbin mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mclose mtight&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The Two’s Complement represents positive numbers in their standard binary representations. Negative numbers are obtained by taking their positive equivalents, flipping all the bits, and adding 1. For instance, the number 2 in a 4-bit system is represented as &lt;code&gt;0010&lt;/code&gt;, while -2 is represented as &lt;code&gt;1110&lt;/code&gt;. The nice thing about this is that subtraction gets handled as a special case of addition.&lt;/p&gt;
&lt;figure class=&quot;figure&quot; style=&quot;max-width: 400px; margin-inline: auto; margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/Two&apos;s%20Complement_Z1evSPW.webp&quot; alt=&quot;Example table illustrating the Two&apos;s Complement&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;921&quot; height=&quot;791&quot; class=&quot;block&quot;&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;An example showing how the Two&amp;#39;s Complement method is used to represent signed integers in a 3-bit system.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;For instance, &lt;code&gt;2-2&lt;/code&gt; can be computed by representing this as &lt;code&gt;2 + (-2)&lt;/code&gt; by calculating the result of &lt;code&gt;0010 + 1110&lt;/code&gt;). Here, the overflow would be ignored, yielding the correct answer &lt;code&gt;0000&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It’s also easy to tell apart positive numbers from negative numbers, as the most significant bit (MSB) serves as a sign bit. For positive numbers, the MSB is &lt;code&gt;0&lt;/code&gt;, whereas the MSB is &lt;code&gt;1&lt;/code&gt; for negative numbers.&lt;/p&gt;
&lt;h2 id=&quot;what-clock-speeds-represent&quot;&gt;What Clock Speeds Represent&lt;a href=&quot;#what-clock-speeds-represent&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Clocks speeds measure how fast computers can perform computations. When a computer chip executes a task, the binary outputs of that execution don’t become instantaneously available to other hardware components that are awaiting those results.&lt;/p&gt;
&lt;p&gt;It takes time for the results of computations to propagate to other chips. Here, time is measured in terms of indivisible units known as clock cycles. The clock speed determines the clock cycle duration.&lt;/p&gt;
&lt;p&gt;Hardware designers must ensure the clock cycle duration exceeds the maximum time required for computational results to propagate between chips and for executing the most time-consuming operations within a chip. If this isn’t the case, a computer’s hardware components won’t work in sync.&lt;/p&gt;
&lt;figure class=&quot;figure&quot; style=&quot;max-width: 480px; margin-inline: auto; margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/Clock%20Cycle_rvpuS.webp&quot; alt=&quot;Diagram of clock signal and delayed gate output signal in a digital circuit.&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1050&quot; height=&quot;667&quot; class=&quot;block&quot;&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;A logic gate&amp;#39;s output does not change instantaneously with the clock signal. The small delay between the start of clock cycle and the gate&amp;#39;s final output change is a crucial factor in determining the maximum clock speed and overall performance of high-speed digital systems.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Take, for example, the program consisting of the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Calculate &lt;code&gt;1 + 2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Store this result in memory&lt;/li&gt;
&lt;li&gt;Take the result of the previous computation and add 3 to it&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;One area where clock speed is important is the second step, where the results of the calculation &lt;code&gt;1 + 2&lt;/code&gt; are stored in memory.&lt;/p&gt;
&lt;p&gt;If the clock speed is too fast, the computer will not have enough time to store this into memory by the time it executes the third step. Thus, clock speeds are constrained by the physical limitations of hardware.&lt;/p&gt;
&lt;h2 id=&quot;how-computers-interface-with-io-devices&quot;&gt;How Computers Interface with I/O Devices&lt;a href=&quot;#how-computers-interface-with-io-devices&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In a computer, external devices are mapped onto chunks of memory. Computers then interface with these devices by reading and writing to these memory maps.&lt;/p&gt;
&lt;p&gt;For instance, a monitor’s pixels get mapped onto a series of memory registers, which store information regarding each pixel’s state. A computer can render graphics on the screen by writing, one pixel at a time, to the screen’s memory map.&lt;/p&gt;
&lt;p&gt;Similarly, keyboards get mapped to a register that stores information regarding the most recently typed character. When a user types a key, the computer’s operating system reads the keyboard’s corresponding memory register, fetches the bitmap image associated with the typed character, and writes to the screen’s memory map to display the character.&lt;/p&gt;
&lt;figure class=&quot;figure&quot; style=&quot;max-width: 320px; margin-inline: auto; margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/Memory%20Maps_2adRiw.webp&quot; alt=&quot;Diagram of computer memory components: Keyboard input, Screen output, and RAM for temporary storage.&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;720&quot; height=&quot;1130&quot; class=&quot;block&quot;&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;External I/O devices such as keyboards and monitors are mapped onto chunks of a computer&amp;#39;s internal memory.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;I now understand why it’s important to have drivers installed when connecting new devices. Without these, a computer will have no instruction manual on how to interface with the device’s memory maps to operate them.&lt;/p&gt;
&lt;h2 id=&quot;basic-structure-of-a-modern-computer&quot;&gt;Basic Structure of a Modern Computer&lt;a href=&quot;#basic-structure-of-a-modern-computer&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Most modern computers are based on the &lt;a href=&quot;https://en.wikipedia.org/wiki/Von_Neumann_architecture&quot;&gt;von Neumann architecture&lt;/a&gt;, created by John von Neumann. This consists of a central processing unit (CPU) that interfaces with a memory device, processes data from input devices (such as keyboards), and generates outputs to output devices (such as screens).&lt;/p&gt;
&lt;figure class=&quot;figure--breakout&quot; style=&quot;margin-block: var(--figure-vertical-spacing)&quot;&gt;&lt;div class=&quot;figure-card&quot;&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/Von%20Neumann%20Computer%20-%20Mobile_jaW2p.webp&quot; style=&quot;max-width: 260px; margin-inline: auto;&quot; alt=&quot;Diagram of the Von Neumann Computer Architecture.&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1020&quot; height=&quot;2467&quot; class=&quot;hide-above-sm&quot;&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/Von%20Neumann%20Computer_Z1HT7gJ.webp&quot; alt=&quot;Diagram of the Von Neumann Computer Architecture.&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2709&quot; height=&quot;1111&quot; class=&quot;show-above-sm&quot;&gt;&lt;/div&gt;&lt;figcaption class=&quot;figcaption&quot;&gt;The Von Neumann architecture comprises a computer system that contains memory and a CPU. The computer receives data from some input device, such as a keyboard, and generates outputs on output devices, such as monitors.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In this scheme, the memory’s hardware includes a series of addressable registers, of a fixed size, that each have a unique address. Access to the data contained in these registers occurs within the same clock cycle, regardless of the memory’s size and location.&lt;/p&gt;
&lt;p&gt;Furthermore, the CPU can read and write two types of memory. These include data memory, which stores programming artifacts like variables, objects, and arrays, as well as instruction memory, which stores instructions for programs.&lt;/p&gt;
&lt;p&gt;The CPU comprises the heart of the von Neumann architecture. It operates on a fetch-execute cycle, where the CPU fetches an instruction from memory, executes it, and repeats this procedure for the next instruction. CPUs leverage onboard memory registers to store interim values, addresses, and instructions that they need to perform computations. Loading these data directly from external memory is much slower and will bottleneck the CPU’s performance.&lt;/p&gt;
&lt;h2 id=&quot;the-purpose-of-assembly-languages&quot;&gt;The Purpose of Assembly Languages&lt;a href=&quot;#the-purpose-of-assembly-languages&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Computers can only run programs written in binary code. But, most people can’t efficiently read and write programs in binary.&lt;/p&gt;
&lt;p&gt;Assembly languages bridge that gap. They provide a means to write human-readable programs that can be directly translated into binary. There’s a direct 1:1 correspondence between a line of assembly code to a binary instruction.&lt;/p&gt;
&lt;p&gt;Furthermore, assembly languages are each tailored to a target hardware platform. This is because computer architectures, such as ARM and x86, each have different specifications for interpreting binary instructions. Here, it’s clear to me now why you can’t generate a binary that runs on any architecture.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Completing the exercises in Nand2Tetris Part 1 helped me understand the fundamental concepts of modern computers. These range from the basic logic gates that serve as building blocks all the way to the low-level assembly languages used to program the hardware. Now, computers are not as mysterious to me as they were when I first started the course.&lt;/p&gt;
&lt;p&gt;But, the only mystery left for me to resolve is how software, such as operating systems and compilers, fits in the bigger picture of computing. I look forward to learning more about these concepts in the second part of the course.&lt;/p&gt;</content:encoded></item><item><title>Ambiguity Fuels Creativity</title><link>https://bryananthonio.com/blog/ambiguity-fuels-creativity/</link><guid isPermaLink="true">https://bryananthonio.com/blog/ambiguity-fuels-creativity/</guid><description>Exploring how ambiguity serves as a catalyst for innovation in creative endeavors.</description><pubDate>Fri, 23 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;What do you think of when you hear the word “ambiguity”? Some people regard it as an adversary to conquer. For others, the concept might evoke feelings of being trapped in a maze. I used to have such feelings.&lt;/p&gt;
&lt;p&gt;But after reading the book &lt;a href=&quot;https://dschool.stanford.edu/book-collections/navigating-ambiguity&quot;&gt;&lt;em&gt;Navigating Ambiguity&lt;/em&gt;&lt;/a&gt; by Andrea Small and Kelly Schmutte, I’ve since understood that ambiguity isn’t to be feared. Indeed, it fuels creativity.&lt;/p&gt;
&lt;h2 id=&quot;the-role-of-ambiguity-in-creativity&quot;&gt;The Role of Ambiguity in Creativity&lt;a href=&quot;#the-role-of-ambiguity-in-creativity&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Ambiguity arises when the end goal for a project isn’t defined with extreme precision. This provides room to experiment with multiple approaches. For instance, in writing, you might rewrite a sentence or paragraph several ways until it feels just right.&lt;/p&gt;
&lt;p&gt;This freedom lends a personal touch to creative work and uncovers surprising possibilities. Yet, I haven’t always found it easy to accept.&lt;/p&gt;
&lt;h2 id=&quot;how-ive-learned-to-leverage-ambiguity&quot;&gt;How I’ve Learned to Leverage Ambiguity&lt;a href=&quot;#how-ive-learned-to-leverage-ambiguity&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I previously struggled to embrace ambiguity because of my perfectionism. As a result, I would rush to the obvious solution to a challenge and perfect it, instead of permitting myself to discover other directions.&lt;/p&gt;
&lt;p&gt;Nowadays, I treat the open-ended aspects of my projects as opportunities to go beyond the obvious.&lt;/p&gt;
&lt;p&gt;For instance, when planning vacations, I no longer immediately pick one destination. I first make a list of potential destinations and eliminate a few of these after researching fun activities and the best times to travel to each place. Only then do I make a final selection.&lt;/p&gt;
&lt;p&gt;In writing software, I’ve found that brainstorming various software architectures and examining their tradeoffs has enabled me to avoid overengineering solutions and find opportunities to simplify my code.&lt;/p&gt;
&lt;p&gt;I’ve also found creative exploration necessary in doing web design. This has pushed me to experiment with different fonts, layouts, and visual motifs to see what matches the brand I’m designing for. Here, I’ve been able to discover interesting design ideas I wouldn’t have previously thought of.&lt;/p&gt;
&lt;h2 id=&quot;handling-too-much-ambiguity&quot;&gt;Handling Too Much Ambiguity&lt;a href=&quot;#handling-too-much-ambiguity&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;With that being said, too much ambiguity hinders progress. If the breadth of potential avenues is endlessly vast, it becomes cumbersome to identify which ones to explore.&lt;/p&gt;
&lt;p&gt;These instances require a clearer vision, which necessitates setting narrower constraints. Doing so empowers you to leverage ambiguity as a bridge towards meaningful opportunities for innovation.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Ambiguity is a crucial ingredient in all creative endeavors.&lt;/p&gt;
&lt;p&gt;Treat it as an asset rather than a deficit. Lean into it to add a personal touch to your work and discover innovative approaches you wouldn’t have thought possible. Constrain it when it hinders your progress.&lt;/p&gt;
&lt;p&gt;In doing so, you’ll transform the unknown into a canvas for your imagination.&lt;/p&gt;</content:encoded></item><item><title>Stephen King’s Lesson on Embracing Failure</title><link>https://bryananthonio.com/blog/stephen-king-lesson-failure/</link><guid isPermaLink="true">https://bryananthonio.com/blog/stephen-king-lesson-failure/</guid><description>Discovering an essential lesson on creative work in Stephen King’s early encounters with rejection.</description><pubDate>Fri, 26 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;While reading Stephen King’s book &lt;a href=&quot;https://en.wikipedia.org/wiki/On_Writing:_A_Memoir_of_the_Craft&quot;&gt;&lt;em&gt;On Writing&lt;/em&gt;&lt;/a&gt;, I was impressed by how he dealt with failure in his teenage years.&lt;/p&gt;
&lt;p&gt;King submitted manuscripts to various magazines for publication, and many publishers rejected his drafts. But that didn’t deter him from continuing to write. In his book, King writes:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When I got the rejection slip from AHMM, I pounded a nail into the wall above the Webcor, wrote “Happy Stamps” on the rejection slip, and poked it onto the nail. Then I sat on my bed and listened to Fats sing “I’m Ready.” I felt pretty good, actually. When you’re still too young to shave, optimism is a perfectly legitimate response to failure. By the time I was fourteen (and shaving twice a week whether I needed to or not) the nail in my wall would no longer support the weight of the rejection slips impaled upon it. I replaced the nail with a spike and went on writing.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This resonates with me because I sometimes shy away from sharing my work, fearing the criticism I might receive. However, King’s story reminds me that failure is an inevitable step in a creative journey, not the end of one.&lt;/p&gt;</content:encoded></item><item><title>My Top Takeaways from ‘Practical UI’</title><link>https://bryananthonio.com/blog/takeaways-practical-ui-book/</link><guid isPermaLink="true">https://bryananthonio.com/blog/takeaways-practical-ui-book/</guid><description>Recapping some practical tips from Adham Dannaway&apos;s book on web design.</description><pubDate>Fri, 19 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;How do UI designers create sleek and elegant web designs? This question puzzled me each time I redesigned my personal website.&lt;/p&gt;
&lt;p&gt;However, my understanding of web design fundamentals improved after reading the book &lt;a href=&quot;https://www.practical-ui.com&quot;&gt;&lt;em&gt;Practical UI&lt;/em&gt; by Adham Dannaway&lt;/a&gt;. The book offered many useful tips on color, typography, layouts, and buttons. In this post, I’ll share key insights from the book that aided my &lt;a href=&quot;https://bryananthonio.com/blog/redesign-3/&quot;&gt;latest website redesign&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#color&quot;&gt;Color&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#typography&quot;&gt;Typography&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#layout&quot;&gt;Layout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#buttons&quot;&gt;Buttons&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;color&quot;&gt;Color&lt;a href=&quot;#color&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Prioritize accessibility when choosing colors.&lt;/strong&gt; It’s crucial ensure sufficient color contrast in user interfaces for readability and differentiation.&lt;/p&gt;
&lt;p&gt;Two standards for this include the Web Content Accessibility Guidelines 2 (WCAG) and the Accessible Perceptual Contrast Algorithm (APCA). Furthermore, tools such as the &lt;a href=&quot;https://developers.google.com/codelabs/devtools-cvd#0&quot;&gt;Chrome DevTools&lt;/a&gt; and the &lt;a href=&quot;https://www.figma.com/community/plugin/733159460536249875&quot;&gt;A11y Figma plugin&lt;/a&gt; can help address color contrast issues.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/text_contrast_Z1URRnj.webp&quot; alt=&quot;Two lines of text, with the first line showing poor contrast and second line showing accessible contrast.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1040&quot; height=&quot;404&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;An example of good and bad text contrast.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Embrace the HSB to create color palettes.&lt;/strong&gt; Unlike RBG or Hex, the hue-saturation-brightness (HSB) is more intuitive for describing colors. Creating variations of a single color requires fixing the hue and adjusting and the saturation and brightness. Learn more in this &lt;a href=&quot;https://www.learnui.design/blog/the-hsb-color-system-practicioners-primer.html&quot;&gt;primer on the HSB color system&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Highlight interactive elements using one color.&lt;/strong&gt; This principle can provide a unified look to a design and minimize confusion. However, it isn’t always necessary to follow when it’s obvious that elements, such as nav bars, are interactive.&lt;/p&gt;
&lt;h2 id=&quot;typography&quot;&gt;Typography&lt;a href=&quot;#typography&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Select a type scale for font sizes.&lt;/strong&gt; Instead of randomly picking font sizes, pick a type scale. This involves choosing a base font size and scaling factor to generate a range of sizes. Here’s a &lt;a href=&quot;https://typescale.com&quot;&gt;handy tool&lt;/a&gt; for this.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Keep line lengths between 40 - 80 characters&lt;/strong&gt;. Lines that are too long or short disrupt the reading experience for users. I doubted this wisdom at first. But, there’s plenty of &lt;a href=&quot;https://www.researchgate.net/publication/234578707_Optimal_Line_Length_in_Reading--A_Literature_Review#:~:text=Research%20has%20led%20to%20recommendations,about%2070%20characters%20per%20line.&quot;&gt;research to back up this claim&lt;/a&gt;.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/line_length_AUWfr.webp&quot; alt=&quot;Two example paragraphs showing the difference between a reasonable and a very long line length.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2398&quot; height=&quot;1064&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;Example paragraphs showing the difference between a long and a more legible line length.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;layout&quot;&gt;Layout&lt;a href=&quot;#layout&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Limit spacing options for simplicity.&lt;/strong&gt; This reduces friction in deciding how to space elements. It also prevents picking arbitrary spacing units, which can make designs look inconsistent. Nowadays, I use spacing units such as &lt;code&gt;2px&lt;/code&gt;, &lt;code&gt;4px&lt;/code&gt;, and integer multiples of &lt;code&gt;8px&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Apply the law of proximity.&lt;/strong&gt; When two visual elements are closely associated, they should be closer together. This principle provides structure and organization in visual designs. Learn more &lt;a href=&quot;https://lawsofux.com/law-of-proximity/&quot;&gt;about this here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;buttons&quot;&gt;Buttons&lt;a href=&quot;#buttons&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Avoid solely relying on color to establish a button hierarchy.&lt;/strong&gt; Consider using shape and style to accommodate colorblind users. An easy way to do this is to use a filled button, unfilled button, and an ‘empty’ button for the primary, secondary, and tertiary buttons, respectively, as shown below.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/buttons_iPK2H.webp&quot; alt=&quot;A filled primary button next to an unfilled secondary button and a tertiary button that has an underline.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1040&quot; height=&quot;404&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;An accessible button hierarchy.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;&lt;strong&gt;Use secondary or tertiary buttons when presenting the user with several options.&lt;/strong&gt; Reserve the
primary button for the most important option, as it for multiple options can be visually
overwhelming. Hence, it’s better to reduce the visual weight of each option by using the secondary
or tertiary buttons.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Require buttons to be at least 48 by 48 pixels in size.&lt;/strong&gt; This ensures that buttons are easy to click.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In summary, &lt;em&gt;Practical UI&lt;/em&gt; introduced me to fundamentals of visual design that helped me redesign my website. This experience also sparked my deeper curiosity of UI design. I’m excited to elevate my design skills to create websites that are even more visually stunning.&lt;/p&gt;</content:encoded></item><item><title>2023: My Year in Review</title><link>https://bryananthonio.com/blog/2023-in-review/</link><guid isPermaLink="true">https://bryananthonio.com/blog/2023-in-review/</guid><description>Reviewing my highlights from 2023 and plans for 2024.</description><pubDate>Sun, 07 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Reflecting on 2023, I realize it was an exceptional year for me. I’m writing this post to recap highlights from the past year, reflect on my previous goals, and set new ones for the upcoming year.&lt;/p&gt;
&lt;p&gt;Here are previous yearly reviews that I’ve written:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://bryananthonio.com/blog/2022-in-review/&quot;&gt;2022: My Year in Review&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://bryananthonio.com/blog/2020-in-review/&quot;&gt;2020: My Year in Review&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#3-key-lessons-learned&quot;&gt;3 Key Lessons Learned&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#books-i-read&quot;&gt;Books I Read&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#sci-fi-fantasy&quot;&gt;Sci-Fi &amp;amp; Fantasy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#software-development-engineering&quot;&gt;Software Development &amp;amp; Engineering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#entrepreneurship&quot;&gt;Entrepreneurship&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#self-help&quot;&gt;Self-help&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#my-year-of-efficiency&quot;&gt;My Year of Efficiency&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#we-adopted-a-puppy&quot;&gt;We Adopted a Puppy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#travels&quot;&gt;Travels&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#switching-from-data-science-to-software-engineering&quot;&gt;Switching From Data Science to Software Engineering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#goals&quot;&gt;Goals&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#previous-goals&quot;&gt;Previous Goals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#goals-for-next-year&quot;&gt;Goals for Next Year&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#closing-thoughts&quot;&gt;Closing Thoughts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;3-key-lessons-learned&quot;&gt;3 Key Lessons Learned&lt;a href=&quot;#3-key-lessons-learned&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Avoid letting perfectionism hinder progress.&lt;/strong&gt; I’ve discovered that my perfectionism, despite its good intentions, hinders my growth. For example, I cited this as &lt;a href=&quot;https://bryananthonio.com/blog/just-write-more/&quot;&gt;one reason&lt;/a&gt; why I wasn’t publishing more blog posts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Don’t be overambitious with side projects.&lt;/strong&gt; I noticed that I’m too ambitious with my side projects, aiming to impress others. As a result, I prioritize longer-term projects that I eventually abandon once I start feeling overwhelmed. Finishing up smaller projects in a shorter period can also be rewarding. These can help me learn fundamental skills that will serve me well for more ambitious efforts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Embrace ambiguity and uncertainty.&lt;/strong&gt; For a long time, I’ve tried to avoid ambiguity and uncertainty as much as possible. For example, I’ve been anxious about determining the next steps of my career trajectory in light of recent AI developments. I’ve since understood that ambiguity and uncertainty can empower me to explore various paths, and uncover promising possibilities that I haven’t imagined.&lt;/p&gt;
&lt;h2 id=&quot;books-i-read&quot;&gt;Books I Read&lt;a href=&quot;#books-i-read&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;sci-fi-fantasy&quot;&gt;Sci-Fi &amp;amp; Fantasy&lt;a href=&quot;#sci-fi-fantasy&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/20518872-the-three-body-problem?from_search=true&amp;amp;from_srp=true&amp;amp;qid=OyFb7gVmtE&amp;amp;rank=1&quot;&gt;The Three-Body Problem&lt;/a&gt;&lt;/em&gt; by Liu Cixin. I heard so much hype about this book but it didn’t impress me. The concepts and the setting were interesting. However, the poor character development made this one fall flat for me.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/61237044-infinity-gate&quot;&gt;&lt;em&gt;Infinity Gate&lt;/em&gt;&lt;/a&gt; by M.R. Carey. I liked the multi-verse concept that this book presents. But, the author was too ambitious. There were a few different subplots that each could have been separate books altogether.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/12287209-wool&quot;&gt;&lt;em&gt;Wool&lt;/em&gt;&lt;/a&gt; and &lt;a href=&quot;https://www.goodreads.com/book/show/17306293-shift&quot;&gt;&lt;em&gt;Shift&lt;/em&gt;&lt;/a&gt; by Hugh Howey. I started reading this series after watching &lt;em&gt;Silo&lt;/em&gt; on Apple TV. These books were fun to read.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;software-development-engineering&quot;&gt;Software Development &amp;amp; Engineering&lt;a href=&quot;#software-development-engineering&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/57281043-programming-phoenix-liveview&quot;&gt;&lt;em&gt;Programming Phoenix LiveView&lt;/em&gt;&lt;/a&gt; by Bruce A. Tate and Sophie DeBenedetto. I’ve found this book to be the most helpful introduction to Phoenix and LiveView. It clarified so much of my confusion regarding Phoenix and gave me confidence to &lt;a href=&quot;https://bryananthonio.com/blog/creating-simple-rest-api-elixir-phoenix/&quot;&gt;build a simple project&lt;/a&gt;. The book is in beta and contains a few typos at the time of this writing.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/25666050-algorithms-to-live-by&quot;&gt;&lt;em&gt;Algorithms to Live By&lt;/em&gt;&lt;/a&gt; by Brian Christian and Tom Griffiths. I loved this book. It was a remarkable exploration of how to apply computer science concepts to everyday life.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/75519891-practical-ui&quot;&gt;&lt;em&gt;Practical UI&lt;/em&gt;&lt;/a&gt; by Adam Dannaway. This was a practical primer on good user interface (UI) design practices for non-experts. It sparked my interest in visual design.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/57271519-the-missing-readme&quot;&gt;&lt;em&gt;The Missing README&lt;/em&gt;&lt;/a&gt; by Chris Riccomini and Dmitry Ryaboy. This book contained lots of practical tips on getting into software engineering. However, I didn’t get many new insights that I hadn’t previously learned from other sources.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;entrepreneurship&quot;&gt;Entrepreneurship&lt;a href=&quot;#entrepreneurship&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/10127019-the-lean-startup&quot;&gt;&lt;em&gt;Lean Startup&lt;/em&gt;&lt;/a&gt; by Eric Reis. One of my favorite books this year. It helped me understand the importance of shipping my work more often, instead of constantly aiming for perfection.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;self-help&quot;&gt;Self-help&lt;a href=&quot;#self-help&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/39863499-how-to-think-like-a-roman-emperor&quot;&gt;&lt;em&gt;How to Think Like a Roman Emperor&lt;/em&gt;&lt;/a&gt; by Donald Robertson. I was interested in reading this because I’ve read through &lt;em&gt;Meditations&lt;/em&gt; by Marcus Aurelius many times. However, I found this book too repetitive.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/en/book/show/15014&quot;&gt;&lt;em&gt;Crucial Conversations&lt;/em&gt;&lt;/a&gt; by Kerry Patterson et al. This was an effective guide for navigating tough conversations.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/25238425-the-effective-engineer&quot;&gt;&lt;em&gt;The Effective Engineer&lt;/em&gt;&lt;/a&gt; by Edmond Lau. This book taught me key lessons regarding the importance of prioritization, automating simple tasks, and learning new technical skills. It prompted me to refine my task management workflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;my-year-of-efficiency&quot;&gt;My Year of Efficiency&lt;a href=&quot;#my-year-of-efficiency&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’m proud I devised a task management workflow that I’ve stuck with for a long period. This includes routines for writing periodic reviews to reflect on my progress towards my goals and identify opportunities for improvement.&lt;/p&gt;
&lt;p&gt;I achieved this by adopting these tools:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://obsidian.md&quot;&gt;Obsidian&lt;/a&gt;: I’ve used Obsidian to manage my notes and orchestrate my periodic reviews, which occur weekly, monthly, and yearly.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://culturedcode.com/things/&quot;&gt;Things 3&lt;/a&gt;: I previously experimented with Things 3 but finally got into the groove of using it. I wrote about this in a &lt;a href=&quot;https://bryananthonio.com/blog/getting-things-done-with-things-3/&quot;&gt;recent blog post&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://ia.net/writer&quot;&gt;iA Writer&lt;/a&gt;: I started using iA Writer more for writing blog posts because of its minimalist interface and other built-in features, like its focus modes, that mesh well with my writing process.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;we-adopted-a-puppy&quot;&gt;We Adopted a Puppy&lt;a href=&quot;#we-adopted-a-puppy&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My partner and I adopted a 2-month-old golden retriever. We named her Hana, after the city in Maui.&lt;/p&gt;
&lt;p&gt;Taking care of Hana was so much work, especially when she was much younger. I even had the &lt;a href=&quot;https://manypets.com/us/blog/what-are-the-puppy-blues-and-how-long-do-they-last/&quot;&gt;puppy blues&lt;/a&gt; from time to time and wondered whether we made a mistake of getting a puppy!&lt;/p&gt;
&lt;p&gt;Fortunately, things have gotten better. Hana is now 9 months old and her temperament has mellowed.&lt;/p&gt;
&lt;h2 id=&quot;travels&quot;&gt;Travels&lt;a href=&quot;#travels&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;These are the new destinations I visited in 2023:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hawaii:&lt;/strong&gt; My partner and I spent a week exploring Maui in April. Here, we discovered our love for snorkeling. We also saw the sunrise at Haleakala which was a highlight of the trip. I was sad to learn that many of the places we visited &lt;a href=&quot;https://en.wikipedia.org/wiki/2023_Hawaii_wildfires&quot;&gt;burned down in August due to wildfires&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oregon:&lt;/strong&gt; This was our first travel adventure with our puppy! We took a road trip to the Oregon coast. I was surprised to find out that Oregon has sand dunes!&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thailand:&lt;/strong&gt; This was our big international trip of the year. We visited Bangkok, Krabi, and Chiang Mai. I enjoyed exploring the beautiful islands around Krabi, loved the down-to-earth feel of Chiang Mai, and had fun exploring the enormous shopping malls in Bangkok.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;switching-from-data-science-to-software-engineering&quot;&gt;Switching From Data Science to Software Engineering&lt;a href=&quot;#switching-from-data-science-to-software-engineering&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Last year, I noted my &lt;a href=&quot;2022-in-review&quot;&gt;interest in improving my software development skills&lt;/a&gt;. This eventually blossomed into a desire to transition into a software engineering career.&lt;/p&gt;
&lt;p&gt;Why? I didn’t enjoy doing data science work as much as I thought I would. The roles and responsibilities of the data science profession don’t align with my long-term interests.&lt;/p&gt;
&lt;p&gt;Lately, I’ve been exploring full-stack web development, reading various software engineering blogs, and experimenting with new AI technologies. Hence, moving into software engineering was an obvious choice. I’m enjoying my new role and I feel confident that I’m moving in the right direction regarding my career trajectory.&lt;/p&gt;
&lt;h2 id=&quot;goals&quot;&gt;Goals&lt;a href=&quot;#goals&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;previous-goals&quot;&gt;Previous Goals&lt;a href=&quot;#previous-goals&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;These were my &lt;a href=&quot;https://bryananthonio.com/blog/2022-in-review/#goals-for-2023&quot;&gt;goals for 2023&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Reach B2 level in Spanish:&lt;/strong&gt; Unfortunately, I’m still at an elementary level because I didn’t consistently practice. I’ll work towards improving my Spanish fluency in the coming year.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Complete &lt;a href=&quot;https://www.clondon.me/blog/52-2023&quot;&gt;Chelsea London’s 52-week photography challenge&lt;/a&gt;:&lt;/strong&gt; I never got started on this goal as I realized I don’t enjoy structured photography prompts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write 12 blog posts:&lt;/strong&gt; I wrote 10 blog posts in 2023. Interestingly enough, I published 4 of these in December alone. The impetus for this came after I read &lt;em&gt;Lean Startup&lt;/em&gt;, which gave me a new perspective regarding my creative work. I realized that it’s better to ship more often rather than endlessly trying to reach perfection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read 12 books:&lt;/strong&gt; I’m happy to say that I achieved this goal!&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Finish an algorithms course:&lt;/strong&gt; I started learning algorithms last year but fell off track. I may resume learning algorithms later this year now that I’m a software developer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learn more about systems design:&lt;/strong&gt; I wanted to learn how to create scalable systems, which is one reason I &lt;a href=&quot;https://bryananthonio.com/blog/first-elixir-program/&quot;&gt;started learning Elixir&lt;/a&gt;. I’ve explored the basics of systems design. But, it’s not yet clear to me right now how deep I’ll explore this topic in the coming year.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read books on software development:&lt;/strong&gt; I put together a list of software engineering books to read. However, I never got around to reading them, as my priorities had shifted.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;goals-for-next-year&quot;&gt;Goals for Next Year&lt;a href=&quot;#goals-for-next-year&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Learn about AI &amp;amp; computing.&lt;/strong&gt; I’ve long desired to understand how computers work. In addition, &lt;a href=&quot;https://twitter.com/karpathy/status/1707437820045062561?lang=en&quot;&gt;this observation by Andrej Karpathy&lt;/a&gt; piqued my interest in the subject again. I feel more compelled to understand the mechanics of computing and explore emerging AI technologies.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Develop UI Design Expertise&lt;/strong&gt;. In 2023, I got exposure to UI design after reading &lt;em&gt;Practical UI&lt;/em&gt; by Adham Dannaway. I have knowledge gaps on creating a design system that I’d like to resolve. I plan to apply what I’ve learned here to my projects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Read 12+ books.&lt;/strong&gt; Each year, I set this goal to maintain a habit of reading books somewhat regularly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ship 3 Projects.&lt;/strong&gt; This year, I want to focus on shipping my projects more often. I’ll achieve this sooner if I prioritize smaller projects to start before moving on to more complex projects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Publish 12+ blog posts.&lt;/strong&gt; I gained momentum with my writing last month as I published 4 blog posts. Hence, I’m confident that I’ll surpass this goal.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Gain Conversational Fluency in Spanish.&lt;/strong&gt; I want to resume learning Spanish. This year, I’ll aim to reach A2 proficiency.&lt;/p&gt;
&lt;h2 id=&quot;closing-thoughts&quot;&gt;Closing Thoughts&lt;a href=&quot;#closing-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All in all, I’m happy with the progress I made in 2023. I uncovered important life lessons, read a variety of books, adopted a puppy, and enjoyed amazing travel adventures. Additionally, I took an important step in my career by transitioning from data science to software engineering.&lt;/p&gt;
&lt;p&gt;My new goals for 2024 focus on further self-improvement. I intend to dive deeper into AI and computing, improve my UI design skills, ship my projects more often, and embrace the challenge of becoming fluent in Spanish. As 2024 begins, I look forward to exploring new possibilities and seeing my goals unfold.&lt;/p&gt;</content:encoded></item><item><title>Creating a Simple REST API Using Elixir and Phoenix</title><link>https://bryananthonio.com/blog/creating-simple-rest-api-elixir-phoenix/</link><guid isPermaLink="true">https://bryananthonio.com/blog/creating-simple-rest-api-elixir-phoenix/</guid><description>A step-by-step guide on creating a REST API with Phoenix, covering everything from database setup to endpoint implementation.</description><pubDate>Sat, 30 Dec 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When I first dove into the &lt;a href=&quot;https://www.phoenixframework.org&quot;&gt;Phoenix&lt;/a&gt; framework, I felt intimidated. It quickly became clear that I needed to understand a few key concepts before I could build my first app.&lt;/p&gt;
&lt;p&gt;However, reading through the book &lt;a href=&quot;https://pragprog.com/titles/liveview/programming-phoenix-liveview/&quot;&gt;Programming Phoenix LiveView&lt;/a&gt; clarified many of the aspects that confused me. The book gave me enough confidence to build a simple REST API that serves quotes from &lt;a href=&quot;https://en.wikipedia.org/wiki/Stoicism&quot;&gt;Stoic&lt;/a&gt; philosophers, to apply what I learned.&lt;/p&gt;
&lt;p&gt;In this post, I’ll outline the step-by-step process of creating this API using Phoenix. The API provides these endpoints:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/api/quotes/&lt;/code&gt; to list all quotes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/api/quotes/random&lt;/code&gt; to fetch a random quote&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#getting-started&quot;&gt;Getting Started&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#what-is-phoenix&quot;&gt;What is Phoenix?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#prerequisites&quot;&gt;Prerequisites&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#kickstarting-your-phoenix-project&quot;&gt;Kickstarting Your Phoenix Project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#setting-up-a-quotes-database&quot;&gt;Setting Up a Quotes Database&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#schema-setup-and-migration&quot;&gt;Schema Setup and Migration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#preventing-duplicate-quotes-in-the-database&quot;&gt;Preventing Duplicate Quotes in the Database&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#add-data-to-the-database&quot;&gt;Add Data to the Database&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#building-api-endpoints&quot;&gt;Building API Endpoints&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#define-endpoints&quot;&gt;Define Endpoints&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#create-controllers&quot;&gt;Create Controllers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#make-view-templates&quot;&gt;Make View Templates&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#testing-the-api&quot;&gt;Testing the API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;a href=&quot;#getting-started&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;what-is-phoenix&quot;&gt;What is Phoenix?&lt;a href=&quot;#what-is-phoenix&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Phoenix is a web development framework built using the &lt;a href=&quot;https://elixir-lang.org&quot;&gt;Elixir programming language&lt;/a&gt;. It’s akin to &lt;a href=&quot;https://rubyonrails.org&quot;&gt;Ruby on Rails&lt;/a&gt; and &lt;a href=&quot;https://www.djangoproject.com&quot;&gt;Django&lt;/a&gt; in the Elixir ecosystem as it implements the server-side &lt;a href=&quot;https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&quot;&gt;Model-View-Controller&lt;/a&gt; design pattern. This makes Phoenix suitable for crafting backend APIs, full-stack web apps, and other applications.&lt;/p&gt;
&lt;p&gt;A key advantage of Phoenix is its robust support for &lt;a href=&quot;https://hexdocs.pm/phoenix/channels.html&quot;&gt;real-time functionalities via websockets&lt;/a&gt;, making it ideal for building chat rooms, multiplayer games, and other apps requiring live updates.&lt;/p&gt;
&lt;h3 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;a href=&quot;#prerequisites&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In this tutorial, I’ll assume the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You are familiar with Elixir syntax.&lt;/li&gt;
&lt;li&gt;You have installed &lt;a href=&quot;https://elixir-lang.org/install.html&quot;&gt;Elixir&lt;/a&gt; and &lt;a href=&quot;https://hexdocs.pm/phoenix/installation.html&quot;&gt;Phoenix (along with PostgreSQL)&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;kickstarting-your-phoenix-project&quot;&gt;Kickstarting Your Phoenix Project&lt;a href=&quot;#kickstarting-your-phoenix-project&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Every Phoenix project begins with the command &lt;code&gt;mix phx.new&lt;/code&gt;. Run the commands below in your terminal to get started:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mix&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; phx.new&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; stoic_quotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; stoic_quotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mix&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; ecto.create&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will create a new directory called &lt;code&gt;stoic_quotes/&lt;/code&gt; with all the necessary files and folders and set up a database for the app.&lt;/p&gt;
&lt;h2 id=&quot;setting-up-a-quotes-database&quot;&gt;Setting Up a Quotes Database&lt;a href=&quot;#setting-up-a-quotes-database&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Next, we’ll create a quotes database. This will involve:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Setting up and running a migration for the &lt;code&gt;quotes&lt;/code&gt; table schema.&lt;/li&gt;
&lt;li&gt;Implementing validations in the database to prevent duplicate quotes.&lt;/li&gt;
&lt;li&gt;Writing a script to insert quotes from a JSON file into the database.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;schema-setup-and-migration&quot;&gt;Schema Setup and Migration&lt;a href=&quot;#schema-setup-and-migration&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;First, we’ll create a database table named &lt;code&gt;quotes&lt;/code&gt; to store quotes. This table will have these fields:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;quote&lt;/code&gt;: the quote itself&lt;/li&gt;
&lt;li&gt;&lt;code&gt;author&lt;/code&gt;: author of the quote&lt;/li&gt;
&lt;li&gt;&lt;code&gt;source&lt;/code&gt;: the literary work (and chapter) containing the quote&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Phoenix provides &lt;a href=&quot;https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.html#module-crud-related-generators&quot;&gt;generators&lt;/a&gt; that we can leverage for this. Here, we’ll use the &lt;code&gt;phx.gen.context&lt;/code&gt; generator.&lt;/p&gt;
&lt;p&gt;This generator executes three tasks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Creates a schema module, which includes the schema for items in the &lt;code&gt;quotes&lt;/code&gt; table and a &lt;code&gt;changeset&lt;/code&gt; function used for validating new entries.&lt;/li&gt;
&lt;li&gt;Provides a context module that offers functions for creating, reading, updating, and deleting items in the &lt;code&gt;quotes&lt;/code&gt; table.&lt;/li&gt;
&lt;li&gt;Generates a Migration file, containing instructions for creating the quotes table in our database.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Furthermore, &lt;code&gt;phx.gen.context&lt;/code&gt; requires at least 3 arguments: the context module name, the singular and plural names of the generated resources, and any attributes to include in the schema, specifying the data type for each attribute.&lt;/p&gt;
&lt;p&gt;Run the following command in your terminal to generate the necessary files for the &lt;code&gt;quotes&lt;/code&gt; table:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mix&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; phx.gen.context&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Quotes&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; Quote&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; quotes&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; quote:text&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; author:string&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; source:string&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;preventing-duplicate-quotes-in-the-database&quot;&gt;Preventing Duplicate Quotes in the Database&lt;a href=&quot;#preventing-duplicate-quotes-in-the-database&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;We need to implement a constraint to prevent users from inserting duplicate quotes into the database. For this, we’ll edit both the migration file and the schema module.&lt;/p&gt;
&lt;p&gt;Navigate to the &lt;code&gt;/priv/repo/migrations&lt;/code&gt; directory and find a timestamped file labeled &lt;code&gt;create_quotes&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This file contains instructions on creating the quotes table with the fields we specified earlier. The &lt;code&gt;timestamps()&lt;/code&gt; macro adds the &lt;code&gt;inserted_at&lt;/code&gt; and &lt;code&gt;updated_at&lt;/code&gt; fields, which store timestamps at which an entry was inserted and updated, respectively.&lt;/p&gt;
&lt;p&gt;Now, edit the migration file to look like this:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defmodule&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; StoicQuotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;Repo&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;Migrations&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;CreateQuotes&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	use&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Ecto&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Migration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; change&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		create &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			add &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			add &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			add &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;			timestamps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;		# add this line below to enforce unique quotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		create &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;unique_index&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, [&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;], name&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;index_for_duplicate_quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The new line we’ve added to the migration file creates a unique index, ensuring each quote is distinct.&lt;/p&gt;
&lt;p&gt;After making these edits, execute the command &lt;code&gt;mix ecto.migrate&lt;/code&gt; in the terminal to create the &lt;code&gt;quotes&lt;/code&gt; table.&lt;/p&gt;
&lt;p&gt;The schema module, located in &lt;code&gt;lib/stoic_quotes/quotes/quote.ex&lt;/code&gt;, defines the schema and includes a &lt;a href=&quot;https://hexdocs.pm/ecto/Ecto.Changeset.html&quot;&gt;changeset function&lt;/a&gt; used for validating new entries before their insertion into the &lt;code&gt;quotes&lt;/code&gt; table.&lt;/p&gt;
&lt;p&gt;These changeset functions are used by methods in the generated context module &lt;code&gt;StoicQuotes.Quotes&lt;/code&gt;, found in &lt;code&gt;lib/stoic_quotes/quotes.ex&lt;/code&gt;. For example, the context module provides this function for creating quotes:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; create_quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(attrs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;\\&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; %{}) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	%&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;changeset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(attrs)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Repo&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code&gt;create_quote&lt;/code&gt; takes a struct containing the fields of the new quote entry. It applies the &lt;code&gt;Quote.changeset&lt;/code&gt; to this input before passing it to &lt;code&gt;Repo.insert()&lt;/code&gt; function. Entries that violate any constraints won’t be inserted.&lt;/p&gt;
&lt;p&gt;By default, the changeset function in the &lt;code&gt;Quote&lt;/code&gt; schema module checks that the necessary fields– quote, author, and source– are provided for each new entry. Let’s add another line to implement the unique constraint:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defmodule&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; StoicQuotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;Quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	use&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Ecto&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	import&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Ecto&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Changeset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	schema &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;quotes&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		field &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		field &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		field &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;		timestamps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;	@doc false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; changeset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, attrs) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		quote&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; cast&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(attrs, [&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; validate_required&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; unique_constraint&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, name&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;index_for_duplicate_quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;#Add this new line&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The new line we’ve added leverages the index we defined in the migration file to prevent the insertion of duplicate quotes and provides an error message when this occurs.&lt;/p&gt;
&lt;h3 id=&quot;add-data-to-the-database&quot;&gt;Add Data to the Database&lt;a href=&quot;#add-data-to-the-database&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Now, let’s add quotes to the database. Create a JSON file named &lt;code&gt;quotes.json&lt;/code&gt; in the &lt;code&gt;priv/repo/&lt;/code&gt; directory. We’ll populate the JSON file with 3 quotes to start:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Seldom are any found unhappy from not observing what is in the minds of others. But such as observe not well the stirrings of their own souls must of necessity be unhappy.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Marcus Aurelius&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Book II, Meditations&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Consider whence each thing came, of what it was compounded, into what it will be changed, how it will be with it when changed, and that it will suffer no evil.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Marcus Aurelius&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Book XI, Meditations&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Accustom yourself as much as possible, when any one takes any action, to consider only: To what end is he working? But begin at home; and examine yourself first of all.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Marcus Aurelius&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Book X, Meditations&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, we’ll write a script to insert these quotes into the database. Create a script called &lt;code&gt;seeds.exs&lt;/code&gt; in &lt;code&gt;priv/repo/&lt;/code&gt; with the following contents:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;alias&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; StoicQuotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;# Read quotes from the JSON file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;quotes_path &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;priv/repo/quotes.json&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;quotes_path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;read!&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Jason&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;decode!&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;each&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; attrs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;	# Construct a quote struct and attempt to insert it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; %{quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; attrs[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;quote&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;], author&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; attrs[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;author&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;], source&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; attrs[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;source&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	case&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;create_quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		{&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;ok&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;_quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		{&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;_changeset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;duplicate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This script uses the &lt;code&gt;Quotes&lt;/code&gt; context module to insert each quote into the database.&lt;/p&gt;
&lt;p&gt;Run the following command to execute the script:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mix&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; priv/repo/seeds.exs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To verify the quotes in the database:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Execute &lt;code&gt;iex -S mix&lt;/code&gt; in the terminal to open an Elixir shell.&lt;/li&gt;
&lt;li&gt;Enter &lt;code&gt;alias StoicQuotes.Quotes&lt;/code&gt; to alias the &lt;code&gt;Quotes&lt;/code&gt; module.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;Quotes.list_quotes()&lt;/code&gt; to list all quotes in the database.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;building-api-endpoints&quot;&gt;Building API Endpoints&lt;a href=&quot;#building-api-endpoints&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Now that the database is set up, we need to establish endpoints for our REST API. These include the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/api/quotes/&lt;/code&gt; to list all quotes&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/api/quotes/random&lt;/code&gt; to fetch a random quote&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;define-endpoints&quot;&gt;Define Endpoints&lt;a href=&quot;#define-endpoints&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In this project, routes are defined in &lt;code&gt;lib/stoic_quotes_web/router.ex&lt;/code&gt;. We can implement the REST API routes by adding the following declaration into the body of this file:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;scope &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/api&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;StoicQuotesWeb&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	pipe_through &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;api&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	get &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/quotes&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;QuotesController&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	get &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/quotes/random&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;QuotesController&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;show&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This snippet sets up the &lt;code&gt;/api/&lt;/code&gt; routes to use the pipeline defined by &lt;code&gt;pipe_through :api&lt;/code&gt;. Requests to these routes will be processed by the &lt;code&gt;:api&lt;/code&gt; pipeline, which is defined in the file as&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;pipeline &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;api&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	plug &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;accepts&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;json&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Routes using this pipeline will accept JSON requests.&lt;/p&gt;
&lt;p&gt;Furthermore, the defined routes specify that &lt;code&gt;GET&lt;/code&gt; requests to the &lt;code&gt;/api/quotes/&lt;/code&gt; and &lt;code&gt;/api/quotes/random&lt;/code&gt; will be handled by the &lt;code&gt;:index&lt;/code&gt; and &lt;code&gt;:show&lt;/code&gt; actions in the &lt;code&gt;QuotesController&lt;/code&gt; module, respectively.&lt;/p&gt;
&lt;p&gt;In the following section, we’ll learn how to create these controller actions.&lt;/p&gt;
&lt;h3 id=&quot;create-controllers&quot;&gt;Create Controllers&lt;a href=&quot;#create-controllers&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Phoenix will look for controller definitions in the directory &lt;code&gt;lib/stoic_quotes_web/controllers/&lt;/code&gt;. Let’s create a file named &lt;code&gt;quotes_controller.ex&lt;/code&gt; here to define our API’s behavior:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defmodule&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; StoicQuotesWeb&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;QuotesController&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	use&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Phoenix&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Controller&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, formats&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	alias&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; StoicQuotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(conn, &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;_params&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		quotes &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; %{quotes&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;list_quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;		render&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(conn, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, quotes)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; show&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(conn, &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;_params&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;		quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; %{quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get_random_quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;		render&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(conn, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;show&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;show&lt;/code&gt; functions implement the logic for the &lt;code&gt;:index&lt;/code&gt; and &lt;code&gt;:show&lt;/code&gt; actions defined in the router, respectively. These require two arguments: the &lt;a href=&quot;https://hexdocs.pm/plug/Plug.Conn.html&quot;&gt;connection struct&lt;/a&gt; &lt;code&gt;conn&lt;/code&gt; and a set of parameters &lt;code&gt;params&lt;/code&gt;, which we won’t use (for more info, read about &lt;a href=&quot;https://hexdocs.pm/phoenix/controllers.html#actions&quot;&gt;controller actions in Phoenix&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Outputs of these functions are dictated by the &lt;code&gt;render/3&lt;/code&gt; function, which &lt;a href=&quot;https://hexdocs.pm/phoenix/Phoenix.Controller.html#render/3&quot;&gt;generates the views&lt;/a&gt; for these actions. Here, &lt;code&gt;render&lt;/code&gt; takes a connection struct, an atom corresponding to the view template and data to pass to the template.&lt;/p&gt;
&lt;p&gt;In addition, the macro &lt;code&gt;use Phoenix.Controller, formats: [:json]&lt;/code&gt; specifies that this controller &lt;a href=&quot;https://hexdocs.pm/phoenix/Phoenix.Controller.html#module-rendering-and-layouts&quot;&gt;renders JSON responses&lt;/a&gt;. Phoenix will look for the view templates in the file &lt;code&gt;quotes_json.ex&lt;/code&gt;, which we’ll define in the next section.&lt;/p&gt;
&lt;p&gt;For the &lt;code&gt;show&lt;/code&gt; action, we’ll need to define the &lt;code&gt;get_random_quote&lt;/code&gt; in the &lt;code&gt;StoicQuotes.Quotes&lt;/code&gt; module located in &lt;code&gt;lib/stoic_quotes/quotes.ex&lt;/code&gt;. Here, we can define this function as shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_random_quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	query &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		from q &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		order_by&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; fragment&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;RANDOM()&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		limit&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;	Repo&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;one&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(query)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;make-view-templates&quot;&gt;Make View Templates&lt;a href=&quot;#make-view-templates&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In the &lt;code&gt;lib/stoic_quotes_web/controllers/&lt;/code&gt; directory, let’s create the &lt;code&gt;quotes_json.ex&lt;/code&gt; file to format data for the API responses:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defmodule&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; StoicQuotesWeb&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;QuotesJSON&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	alias&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; StoicQuotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quotes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quote&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(%{quotes&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; quotes}) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		%{data&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; quotes, do&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; show&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(%{quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		%{data&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	defp&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(%&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;Quote&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; datum) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		%{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			quote&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; datum.quote,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			author&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; datum.author,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			source&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; datum.source&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;	end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These functions define the view templates for the &lt;code&gt;index&lt;/code&gt; and the &lt;code&gt;show&lt;/code&gt; actions. Both functions return a struct with a &lt;code&gt;data&lt;/code&gt; field, which Phoenix automatically converts into appropriate JSON response formats.&lt;/p&gt;
&lt;h2 id=&quot;testing-the-api&quot;&gt;Testing the API&lt;a href=&quot;#testing-the-api&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Now, let’s test this API. An easy way to do this is to use the &lt;code&gt;curl&lt;/code&gt; command, available in terminals, that transfers data from or to a server.&lt;/p&gt;
&lt;p&gt;First, run &lt;code&gt;mix phx.server&lt;/code&gt; to start the server. After that, test the endpoint routes using these commands:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -i&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; localhost:4000/api/quotes/&lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt; #get all quotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; -i&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; localhost:4000/api/quotes/random&lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt; #get random quote&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this blog post, we covered the necessary steps to build a simple REST API using the Phoenix Framework. First, we set up a database, populated it with quote entries, and implemented the endpoints for our REST API. Finally, we created controllers and corresponding views to generate JSON responses.&lt;/p&gt;</content:encoded></item><item><title>Creating a Sitemap in SvelteKit</title><link>https://bryananthonio.com/blog/creating-sveltekit-sitemap/</link><guid isPermaLink="true">https://bryananthonio.com/blog/creating-sveltekit-sitemap/</guid><description>How to create a sitemap in SvelteKit for search engine optimization (SEO).</description><pubDate>Mon, 11 Dec 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A well-structured sitemap can be a game changer for search engine optimization (SEO). If you’ve built your blog using &lt;a href=&quot;https://kit.svelte.dev&quot;&gt;SvelteKit&lt;/a&gt;, you may wonder how to create one using this framework. While the &lt;a href=&quot;https://kit.svelte.dev/docs/seo#manual-setup-sitemaps&quot;&gt;official documentation&lt;/a&gt; briefly introduces sitemap generation, it doesn’t provide some key details. In this post, I will describe how to generate a sitemap for a blog site.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#what-is-a-sitemap&quot;&gt;What is a Sitemap?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#steps-to-generate-a-sitemap&quot;&gt;Steps to Generate a Sitemap&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#prerequisites&quot;&gt;Prerequisites&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#create-the-sitemap-route&quot;&gt;Create the Sitemap Route&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#gather-blog-post-links&quot;&gt;Gather Blog Post Links&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#generate-sitemap-contents&quot;&gt;Generate Sitemap Contents&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-is-a-sitemap&quot;&gt;What is a Sitemap?&lt;a href=&quot;#what-is-a-sitemap&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A sitemap is a map to all the pages, videos, and other content on your website. Sitemaps are beneficial for SEO because web crawlers use them to efficiently identify and index content on websites. &lt;a href=&quot;https://developers.google.com/search/docs/crawling-indexing/sitemaps/overview&quot;&gt;Google&lt;/a&gt; recommends generating one if you meet one othese criteria:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Your website has 500+ pages.&lt;/li&gt;
&lt;li&gt;Your website is new and hasn’t been indexed.&lt;/li&gt;
&lt;li&gt;Your website contains a lot of rich media content, such as video and images, or is featured in Google News.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Even if none of these match your use case, providing a sitemap can get your website indexed faster, especially if you &lt;a href=&quot;https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap#addsitemap&quot;&gt;submit it to the Google Search Console&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;steps-to-generate-a-sitemap&quot;&gt;Steps to Generate a Sitemap&lt;a href=&quot;#steps-to-generate-a-sitemap&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;a href=&quot;#prerequisites&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In this tutorial, I’ll assume you have a basic understanding of how &lt;a href=&quot;https://kit.svelte.dev/docs/routing&quot;&gt;routing&lt;/a&gt; works in SvelteKit.&lt;/p&gt;
&lt;h3 id=&quot;create-the-sitemap-route&quot;&gt;Create the Sitemap Route&lt;a href=&quot;#create-the-sitemap-route&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;First, let’s create the route &lt;code&gt;/sitemap.xml&lt;/code&gt;. We can do this by making a new directory at the path &lt;code&gt;routes/sitemap.xml/&lt;/code&gt; in your project’s &lt;code&gt;src/&lt;/code&gt; folder.&lt;/p&gt;
&lt;p&gt;In this directory, create a &lt;code&gt;+server.js&lt;/code&gt; file. Here, we’ll define a server-side action to generate the sitemap. This can be done by implementing the &lt;code&gt;GET&lt;/code&gt; function shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// src/routes/sitemap.xml/+server.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; fs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;fs&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; path &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;path&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; prerender &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// needed only if using SvelteKit&amp;#39;s static adapter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; GET&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; navHeaderLinks &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/about&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/projects&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; blogPostLinks &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; getBlogPostLinks&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; linkItems &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;navHeaderLinks&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;blogPostLinks&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sitemap &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; generateSitemap&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;linkItems&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Response&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;sitemap&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    headers&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Content-Type&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;application/xml&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;navHeaderLinks&lt;/code&gt; and &lt;code&gt;blogPostLinks&lt;/code&gt; represent arrays of strings, where each string corresponds to an internal link on the website.&lt;/p&gt;
&lt;p&gt;In this example, the &lt;code&gt;navHeaderLinks&lt;/code&gt; array corresponds to links that appear in the navigation menu. These include the home, blog, about, and projects pages.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;blogPostLinks&lt;/code&gt; array contains internal links for blog posts. For instance, if there’s a blog post at the route &lt;code&gt;/blog/blog-post-example&lt;/code&gt;, the &lt;code&gt;blogPostLinks&lt;/code&gt; array will contain the string &lt;code&gt;&amp;quot;/blog/blog-post-example&amp;quot;&lt;/code&gt;. Because the routes for blog posts are typically generated dynamically, we’ll gather these separately.&lt;/p&gt;
&lt;aside class=&quot;callout callout--tip&quot; data-astro-cid-q2ml7llr&gt;&lt;div class=&quot;callout__header&quot; data-astro-cid-q2ml7llr&gt;&lt;svg fill=&quot;none&quot; viewBox=&quot;0 0 24 24&quot; stroke-width=&quot;1.5&quot; stroke=&quot;currentColor&quot; class=&quot;callout__icon&quot; data-astro-cid-q2ml7llr=&quot;true&quot;&gt;
  &lt;path stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; d=&quot;M12 18v-5.25m0 0a6.01 6.01 0 0 0 1.5-.189m-1.5.189a6.01 6.01 0 0 1-1.5-.189m3.75 7.478a12.06 12.06 0 0 1-4.5 0m3.75 2.383a14.406 14.406 0 0 1-3 0M14.25 18v-.192c0-.983.658-1.823 1.508-2.316a7.5 7.5 0 1 0-7.517 0c.85.493 1.509 1.333 1.509 2.316V18&quot; /&gt;
&lt;/svg&gt;&lt;strong class=&quot;callout__title&quot; data-astro-cid-q2ml7llr&gt;Tip&lt;/strong&gt;&lt;/div&gt;&lt;div class=&quot;callout__content&quot; data-astro-cid-q2ml7llr&gt;&lt;p&gt;If you’re using &lt;a href=&quot;https://kit.svelte.dev/docs/adapter-static&quot;&gt;SvelteKit as a static site generator&lt;/a&gt;, include the expression &lt;code&gt; export const prerender = true&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/aside&gt;
&lt;h3 id=&quot;gather-blog-post-links&quot;&gt;Gather Blog Post Links&lt;a href=&quot;#gather-blog-post-links&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To gather the blog post links, we need the Node functions &lt;code&gt;fs.readdirSync&lt;/code&gt; and &lt;code&gt;path&lt;/code&gt;. These will be used to read the directory containing the blog posts.&lt;/p&gt;
&lt;p&gt;Let’s assume that the directory &lt;code&gt;src/lib/posts&lt;/code&gt; contains markdown files for each post and that the names of these markdown files correspond to the blog post slugs. We can leverage the &lt;code&gt;getBlogPostLinks&lt;/code&gt; function defined below to extract the slugs for each of the blog posts:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// src/routes/sitemap.xml/+server.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; getBlogPostLinks&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; postsDirectory &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; path&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;process&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;cwd&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(), &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;src/lib/posts&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; filenames &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; fs&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;readdirSync&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;postsDirectory&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; blogPostLinks &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; filenames&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;((file) &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; slug &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;\.&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;md$&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; `/blog/&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; blogPostLinks&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;generate-sitemap-contents&quot;&gt;Generate Sitemap Contents&lt;a href=&quot;#generate-sitemap-contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Finally, let’s define a function to generate the sitemap using the gathered links. While there are &lt;a href=&quot;https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap&quot;&gt;three different sitemap formats&lt;/a&gt; to chose from, we’ll focus on creating a XML sitemap. The function below does this:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// src/routes/sitemap.xml/+server.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; generateSitemap&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(linkItems) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; siteURL &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;https://example-domain-name.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sitemapEntries &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; linkItems&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;((link) &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; `&amp;lt;url&amp;gt;&amp;lt;loc&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;siteURL&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}${&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;link&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;lt;/loc&amp;gt;&amp;lt;/url&amp;gt;`&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;trim&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  let&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; sitemap&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; `&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;	&amp;lt;urlset xmlns=&amp;quot;http://www.sitemaps.org/schemas/sitemap/0.9&amp;quot;&amp;gt; &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;sitemapEntries&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;	&amp;lt;/urlset&amp;gt;`&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;  sitemap&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; sitemap&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;//remove newline characters&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; sitemap&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;aside class=&quot;callout callout--info&quot; data-astro-cid-q2ml7llr&gt;&lt;div class=&quot;callout__header&quot; data-astro-cid-q2ml7llr&gt;&lt;svg fill=&quot;none&quot; viewBox=&quot;0 0 24 24&quot; stroke-width=&quot;1.5&quot; stroke=&quot;currentColor&quot; class=&quot;callout__icon&quot; data-astro-cid-q2ml7llr=&quot;true&quot;&gt;
  &lt;path stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; d=&quot;m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z&quot; /&gt;
&lt;/svg&gt;&lt;strong class=&quot;callout__title&quot; data-astro-cid-q2ml7llr&gt;Info&lt;/strong&gt;&lt;/div&gt;&lt;div class=&quot;callout__content&quot; data-astro-cid-q2ml7llr&gt;&lt;p&gt;You can also add &lt;code&gt;priority&lt;/code&gt; and &lt;code&gt;changefreq&lt;/code&gt; values to each URL declaration. But, Google says &lt;a href=&quot;https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap#additional-notes-about-xml-sitemaps&quot;&gt; it ignores these &lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/aside&gt;
&lt;p&gt;This function assumes that the website is hosted at the domain given by &lt;code&gt;siteURL&lt;/code&gt;. Now, if you navigate to the route &lt;code&gt;/sitemap.xml&lt;/code&gt;, you should see the contents of the sitemap.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In summary, generating a sitemap is a key part of SEO. This guide has outlined how to create one for a SvelteKit-based blog site. By following this process, you will set the stage to improve your site’s online visibility.&lt;/p&gt;</content:encoded></item><item><title>Redesign: Version 3.0</title><link>https://bryananthonio.com/blog/redesign-3/</link><guid isPermaLink="true">https://bryananthonio.com/blog/redesign-3/</guid><description>Documenting the latest update for my website design, which includes a new design system and a new focus on presenting my writing and my side projects.</description><pubDate>Fri, 08 Dec 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://bryananthonio.com/blog/redesign-2/&quot;&gt;It’s been a year since I redesigned my website.&lt;/a&gt; I’m excited because I’ve learned more about visual design since then! These learnings have culminated in a new design for my website, the details of which I’m going to share in this blog post.&lt;/p&gt;
&lt;h2 id=&quot;general-changes&quot;&gt;General Changes&lt;a href=&quot;#general-changes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;implemented-a-design-system&quot;&gt;Implemented a Design System&lt;a href=&quot;#implemented-a-design-system&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In this update, I devised a design system composed of a color palette, spacings, and font sizes. Here are the highlights:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;My design uses Inter font for headings and body text.&lt;/strong&gt; Previously, I used the default sans-serif fonts provided by the browser. I made this change to provide a more consistent experience across users.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;I implemented a typographic scale.&lt;/strong&gt; For this, I picked a scale ratio of 1.313 to create 7 font sizes. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;I devised a color palette for headers and body text, buttons, and various UI elements.&lt;/strong&gt; These include: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(40, 92%, 70%); color: hsl(0, 0%, 10%); margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Primary&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(220, 100%, 50%); color: white; margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Link&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(0, 0%, 10%); color: white; margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Gray 1&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(0, 0%, 30%); color: white; margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Gray 2&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(0, 0%, 59%); color: hsl(0, 0%, 10%); margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Gray 3&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(0, 0%, 90%); color: hsl(0, 0%, 10%); margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Gray 4&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(0, 0%, 95%); color: hsl(0, 0%, 10%); margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Gray 5&lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style=&quot;background-color: hsl(0, 0%, 98%); color: hsl(0, 0%, 10%); margin-left: 4px; border: 1px solid black; display: inline-block; width: 5rem; height: 1.5em; text-align: center; vertical-align: middle; line-height: 1.5em;&quot;&gt;Gray 6&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The new design implements a fixed set of spacings.&lt;/strong&gt; These include: 4, 8, 16, 32, 48, and 80 pixels, and their equivalents in rem units.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;removed-photography-content&quot;&gt;Removed Photography Content&lt;a href=&quot;#removed-photography-content&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I removed the photos page. When I first created this website, I intended for it to be a one-stop shop for my creative work, including my photography. But, I’ve since realized that this may not suit me.&lt;/p&gt;
&lt;p&gt;I have grander visions for showcasing my photography online. These include crafting photo essays and galleries. In the future, I may create a new website for this or add a redesigned photography page to this website.&lt;/p&gt;
&lt;p&gt;I haven’t yet determined which path to take. Until then, I’m dedicating this website to share my writings and my side projects.&lt;/p&gt;
&lt;h3 id=&quot;added-projects-page&quot;&gt;Added Projects Page&lt;a href=&quot;#added-projects-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To that effect, I added a new page to showcase my side projects. There’s not much content on this page right now. But that will change in the future.&lt;/p&gt;
&lt;h2 id=&quot;page-layouts&quot;&gt;Page Layouts&lt;a href=&quot;#page-layouts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here are some screenshots of the latest design.&lt;/p&gt;
&lt;h3 id=&quot;landing-page&quot;&gt;Landing Page&lt;a href=&quot;#landing-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/landing_page_4COor.png&quot; alt=&quot;New design of the home page&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;blog-page&quot;&gt;Blog page&lt;a href=&quot;#blog-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog_page_Z25OhjP.png&quot; alt=&quot;New design of the blog page&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;article-page&quot;&gt;Article page&lt;a href=&quot;#article-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/blog_post_page_26eQ30.png&quot; alt=&quot;New design of the blog post layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;about-me&quot;&gt;About Me&lt;a href=&quot;#about-me&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/about_page_1Ei369.png&quot; alt=&quot;New design of the about page layout&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h3 id=&quot;projects&quot;&gt;Projects&lt;a href=&quot;#projects&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/projects_page_Z1D8OMm.png&quot; alt=&quot;New projects page&quot; sizes=&quot;auto&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3248&quot; height=&quot;2112&quot;&gt;
&lt;h2 id=&quot;design-inspiration&quot;&gt;Design Inspiration&lt;a href=&quot;#design-inspiration&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As I worked on redesigning the layouts of each page, I drew inspiration from these websites:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://v4.brittanychiang.com&quot;&gt;Brittany Chiang&lt;/a&gt; - This website provided suggestions on structuring landing page headers.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://www.taniarascia.com/redesign-version-6/&quot;&gt;Tania Rascia&lt;/a&gt; - I liked the simplicity of the Tania’s layouts for the Blog and Project pages.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://joshcollinsworth.com/about-me&quot;&gt;Josh Collinsworth&lt;/a&gt; - Here, I got the idea to add links to other pages on the landing page. I also took some pointers here on structuring the layout of blog posts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://www.joshwcomeau.com&quot;&gt;Josh Comeau&lt;/a&gt; - I thought the animated arrows on Josh’s page were a nice touch so I added these to my landing page.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;honorable-mentions&quot;&gt;Honorable Mentions&lt;a href=&quot;#honorable-mentions&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I like &lt;a href=&quot;https://stefanzweifel.dev/reading&quot;&gt;Stefan Zweifel’s Reading page&lt;/a&gt;. Initially, I planned to add my own Reading page in this update, but opted to include it in the future.&lt;/p&gt;
&lt;p&gt;I’m also a huge fan of &lt;a href=&quot;https://maggieappleton.com&quot;&gt;Maggie Appleton’s website&lt;/a&gt;. The typography and animations impress me! I need to learn more about typography and CSS to come up with something equally clever.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I had a lot of fun putting together this redesign. Indeed, this work marked a significant evolution in my design skills and my vision for my website. I’m only just getting started in my design journey and look forward to seeing what comes next.&lt;/p&gt;</content:encoded></item><item><title>Just Write More</title><link>https://bryananthonio.com/blog/just-write-more/</link><guid isPermaLink="true">https://bryananthonio.com/blog/just-write-more/</guid><description>Exploring my struggles with perfectionism in my writing, and my commitment to overcome it through consistent practice.</description><pubDate>Sun, 03 Dec 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;What’s the best way to improve your writing? Just write more often. This advice makes sense and yet, I’ve struggled to follow it.&lt;/p&gt;
&lt;p&gt;I’ve been too ambitious whenever I write a new post for my blog. I constantly aim for each post to be written well, perfectly structured, and full of original ideas. As a result, I write less often because of the unnecessary pressure I put on myself. I’m now aiming to publish more regularly and worry less about writing perfect blog posts.&lt;/p&gt;
&lt;p&gt;This experience reminds me of my struggles when &lt;a href=&quot;https://bryananthonio.com/blog/my-first-steps-in-photography/&quot;&gt;I first started photography.&lt;/a&gt; In those days, my photos didn’t impress me. This is because I didn’t yet understand techniques for composing images and hadn’t identified a genre of photography to focus on. I felt aimless.&lt;/p&gt;
&lt;p&gt;Finding clarity required effort. I took lots of pictures and experimented with different styles of photography. Soon, I realized my love for landscape photography and developed my photographic eye. I’m going to take a similar approach to writing.&lt;/p&gt;
&lt;p&gt;Writing and publishing more frequently will help me grow as a writer. With every new article I publish, I hope to come closer to identifying what I enjoy writing about most and developing my writing voice. I don’t want this journey to be about achieving perfection. In embracing this process, I hope to connect more authentically with my thoughts and my readers.&lt;/p&gt;</content:encoded></item><item><title>Getting Things Done with Things 3</title><link>https://bryananthonio.com/blog/getting-things-done-with-things-3/</link><guid isPermaLink="true">https://bryananthonio.com/blog/getting-things-done-with-things-3/</guid><description>Sharing my streamlined task management using Things 3, emphasizing setup, routines, and preferences.</description><pubDate>Sun, 15 Oct 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For the longest time, I sought a sustainable workflow for managing my tasks. Despite experimenting with many different to-do list apps I often found myself over-engineering my workflows. After much reflection, I identified a simple setup that met my needs. In this post, I’ll share my current task management workflow using &lt;a href=&quot;https://culturedcode.com/things/&quot;&gt;Things 3&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#why-things-3&quot;&gt;Why Things 3?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#my-setup&quot;&gt;My Setup&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#areas&quot;&gt;Areas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#tags&quot;&gt;Tags&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#due-dates&quot;&gt;Due Dates&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#routines&quot;&gt;Routines&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#monthly-planning&quot;&gt;Monthly Planning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#weekly-planning&quot;&gt;Weekly Planning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#processing-the-inbox&quot;&gt;Processing the Inbox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#daily-planning&quot;&gt;Daily Planning&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;why-things-3&quot;&gt;Why Things 3?&lt;a href=&quot;#why-things-3&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I opted to use Things 3 for these reasons:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I previously purchased the app for all of my Apple devices.&lt;/li&gt;
&lt;li&gt;Things 3 does not require a monthly or annual subscription. Many of the other to-do list apps in the App Store require a subscription for all features.&lt;/li&gt;
&lt;li&gt;I enjoy the minimalist design that Things 3 offers. The app’s simplicity and ease of navigation suit my preferences perfectly.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;my-setup&quot;&gt;My Setup&lt;a href=&quot;#my-setup&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;areas&quot;&gt;Areas&lt;a href=&quot;#areas&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In my Things 3 setup, I’ve created five areas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🌱 &lt;strong&gt;Personal Growth:&lt;/strong&gt; Tasks and projects relevant to my personal growth such as books I’m reading and periodic projects for daily, weekly, and monthly reviews.&lt;/li&gt;
&lt;li&gt;🎨 &lt;strong&gt;Creative Projects:&lt;/strong&gt; This area encompasses my web development side projects, photo editing, and blog writing.&lt;/li&gt;
&lt;li&gt;💵 &lt;strong&gt;Finances:&lt;/strong&gt; I use this area for organizing my personal finance tasks.&lt;/li&gt;
&lt;li&gt;🏠 &lt;strong&gt;Responsibilities &amp;amp; Errands:&lt;/strong&gt; Here, I organize one-off errands and weekly chores.&lt;/li&gt;
&lt;li&gt;🌎 &lt;strong&gt;Social &amp;amp; Experiences:&lt;/strong&gt; This is for planning travel, fun experiences, and other social events.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s worth noting that I don’t use this setup for work-related tasks, hence the absence of professional areas.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/things-3-inbox_ZDOg7i.webp&quot; alt=&quot;A screenshot of my Things 3 setup.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2540&quot; height=&quot;1780&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;Overview of my Things 3 setup.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;tags&quot;&gt;Tags&lt;a href=&quot;#tags&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I don’t use tags very much. The only tag in my system is &lt;code&gt;⚡️Quick&lt;/code&gt; (&lt;a href=&quot;https://productivewithapurpose.com/2019/05/21/the-fu-master-productivity-checklist-using-things3/&quot;&gt;inspired by this old blog post&lt;/a&gt;), which I use for tasks that I can complete in 5 minutes.&lt;/p&gt;
&lt;h3 id=&quot;due-dates&quot;&gt;Due Dates&lt;a href=&quot;#due-dates&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I only include due dates on tasks that have critical consequences if I don’t complete them by a certain date. This suits me because most of my tasks don’t meet this criteria.&lt;/p&gt;
&lt;h2 id=&quot;routines&quot;&gt;Routines&lt;a href=&quot;#routines&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Things 3 offers these different views of tasks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inbox:&lt;/strong&gt; Collection of potential tasks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Today:&lt;/strong&gt; Tasks scheduled for today.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Upcoming:&lt;/strong&gt; Scheduled tasks for the current week.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Anytime:&lt;/strong&gt; Tasks and projects that can be completed anytime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Someday:&lt;/strong&gt; Tasks and projects to consider working on in the future.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Below, I’ll explain how I use each of these views in my monthly, weekly, and daily planning.&lt;/p&gt;
&lt;h3 id=&quot;monthly-planning&quot;&gt;Monthly Planning&lt;a href=&quot;#monthly-planning&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;At the start of each month, I review tasks in the &lt;code&gt;Someday&lt;/code&gt; category and move them to &lt;code&gt;Anytime&lt;/code&gt; if I expect I’ll be able to complete them by the end of that month. Tasks that no longer fit this criteria are moved into the &lt;code&gt;Someday&lt;/code&gt; category.&lt;/p&gt;
&lt;p&gt;I’ve found this strategy helpful because in the past I found myself overwhelmed with having a long list of tasks to complete in the &lt;code&gt;Anytime&lt;/code&gt; view. Adding this constraint allows me to better prioritize my tasks.&lt;/p&gt;
&lt;p&gt;To help with this, I also write a monthly review of the previous month to reflect on how things went in the previous month and identify opportunities for improvement. This helps me figure out what tasks or projects to prioritize for the upcoming month.&lt;/p&gt;
&lt;h3 id=&quot;weekly-planning&quot;&gt;Weekly Planning&lt;a href=&quot;#weekly-planning&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I do a weekly review every Sunday. Here, I review items in the &lt;code&gt;Anytime&lt;/code&gt; category and schedule those on different days in the upcoming week. If I’m ever completely done with weekly items (which rarely happens!) I look at items in the &lt;code&gt;Anytime&lt;/code&gt; category and work on those.&lt;/p&gt;
&lt;p&gt;Much like my monthly planning process, I also write a weekly review of the previous week. I use the learnings from this review to guide how I plan for the upcoming week.&lt;/p&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/weekly-review_V62BK.webp&quot; alt=&quot;My weekly review project template.&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1354&quot; height=&quot;1780&quot; class=&quot;screenshot__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-l2qtljp6&gt;My weekly review project template.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;processing-the-inbox&quot;&gt;Processing the Inbox&lt;a href=&quot;#processing-the-inbox&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;During the week I capture ideas of prospective things I want to do in the inbox. I’ve found the &lt;a href=&quot;https://culturedcode.com/things/support/articles/2803561/&quot;&gt;integration with the Reminders app&lt;/a&gt; helpful for this as well.&lt;/p&gt;
&lt;p&gt;At the end of each week I review these tasks and either schedule them, place them in the &lt;code&gt;Anytime&lt;/code&gt; category, or move them to the &lt;code&gt;Someday&lt;/code&gt; category if the task isn’t a priority for this month.&lt;/p&gt;
&lt;h3 id=&quot;daily-planning&quot;&gt;Daily Planning&lt;a href=&quot;#daily-planning&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Each morning, I review tasks in the &lt;code&gt;Today&lt;/code&gt; category. If I’m not able to complete a task by the end of the day I either plan to complete it the next day or reschedule it.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this blog post, I’ve gone over the details of my Things 3 setup and the routines I use to maintain it. Having used this setup for two months, I appreciate its simplicity and believe it will serve me sustainably long-term.&lt;/p&gt;</content:encoded></item><item><title>Simplifying My Website&apos;s CSS Using Tailwind</title><link>https://bryananthonio.com/blog/simplifying-my-website-css-using-tailwind/</link><guid isPermaLink="true">https://bryananthonio.com/blog/simplifying-my-website-css-using-tailwind/</guid><description>Discussing my transition from using a centralized CSS style sheet to adopting a component-based approach with Tailwind CSS.</description><pubDate>Sat, 05 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Have you ever struggled to organize your CSS styles, especially as your project scales? Indeed, my own experiences have taught me that learning best practices for maintaining CSS for larger projects isn’t straightforward. It was only after working through Josh Comeau’s &lt;a href=&quot;https://www.joyofreact.com/&quot;&gt;Joy of React course&lt;/a&gt; that I stumbled upon some insights regarding this.&lt;/p&gt;
&lt;p&gt;Despite the course’s focus on creating React apps, it presented strategies for maintaining CSS applicable across component-based frameworks like React or Svelte, the latter of which I’ve used to build my website. One game-changing lesson was the practice of scoping CSS styles to individual components instead of applying these styles globally.&lt;/p&gt;
&lt;p&gt;This got me to reconsider how I’ve implemented CSS for my website. Initially, I defined custom CSS classes used throughout the website in just one file. However, I eventually shifted towards making better use of the utility classes provided by the &lt;a href=&quot;https://tailwindcss.com/&quot;&gt;Tailwind CSS&lt;/a&gt; framework.&lt;/p&gt;
&lt;p&gt;In this post, I’ll go over some of the pitfalls of my previous approach, describe some solutions I could have adopted, and reveal why I decided to stick with using Tailwind.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#the-pitfalls-of-centralizing-all-css-styles&quot;&gt;The Pitfalls of Centralizing All CSS Styles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#scoped-css-a-component-based-approach&quot;&gt;Scoped CSS: A Component-Based Approach&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#tailwind-css-the-game-changer-in-my-css-workflow&quot;&gt;Tailwind CSS: The Game Changer in my CSS Workflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#refactoring-my-websites-css&quot;&gt;Refactoring My Website’s CSS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-pitfalls-of-centralizing-all-css-styles&quot;&gt;The Pitfalls of Centralizing All CSS Styles&lt;a href=&quot;#the-pitfalls-of-centralizing-all-css-styles&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When I created my first website using &lt;a href=&quot;https://www.11ty.dev&quot;&gt;Eleventy&lt;/a&gt;, I kept all of the CSS styles in a single stylesheet. At the time, this seemed reasonable for a small project. I even continued with this practice after rebuilding my website using SvelteKit.&lt;/p&gt;
&lt;p&gt;However, after working through the first module of the Joy of React course (specifically the section titled &lt;em&gt;Styling in React&lt;/em&gt;) I began to recognize a drawback to this approach.&lt;/p&gt;
&lt;p&gt;As a project gets larger, it can become cumbersome to keep all styles within a single stylesheet. For instance, you might have several different types of buttons, each of which may require a &lt;code&gt;.button&lt;/code&gt; class. But over time, you may need to add more classes to style different types of buttons such as &lt;code&gt;.button-primary&lt;/code&gt;, &lt;code&gt;.button-secondary&lt;/code&gt;, and &lt;code&gt;.button-submit&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Sometimes, refactoring old CSS rules may not be feasible. This can lead you to adopt the practice of applying multiple CSS classes to style one element. For example, you may end up having to use both the &lt;code&gt;.button&lt;/code&gt; and &lt;code&gt;.primary-button&lt;/code&gt; styles to style a primary button. This will solve short-term problems with styling. But it may add more complexity to your code in the long run as you add new features.&lt;/p&gt;
&lt;p&gt;The bigger problem that can occur is that your stylesheet will eventually become bloated with a lot of CSS classes. If you need to create a new class you would have to look over your existing CSS to make sure you haven’t already defined this class elsewhere for a different purpose. This adds unnecessary friction to defining new style classes, hampering your productivity.&lt;/p&gt;
&lt;p&gt;So, how can we mitigate this issue? Are there strategies to manage the chaos of clashing CSS classes and the complexity of multiple classes for a single element? The good news is that component-based frameworks have tools to address this challenge.&lt;/p&gt;
&lt;h2 id=&quot;scoped-css-a-component-based-approach&quot;&gt;Scoped CSS: A Component-Based Approach&lt;a href=&quot;#scoped-css-a-component-based-approach&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the Joy of React course, Josh, the instructor, talked about components as abstractions that encapsulate three things: HTML markup, JavaScript logic, and CSS styles. Here, locally scoping CSS styles to components mitigates the problem of making sure you don’t have duplicate CSS classes defined throughout the project. Modern frontend frameworks provide easy ways to achieve this.&lt;/p&gt;
&lt;p&gt;React external libraries such &lt;a href=&quot;https://github.com/css-modules/css-modules&quot;&gt;CSS Modules&lt;/a&gt; and &lt;a href=&quot;https://github.com/emotion-js/emotion&quot;&gt;Emotion&lt;/a&gt; scope style definitions to components. In contrast, Svelte allows you to &lt;a href=&quot;https://svelte.dev/docs/svelte-components#style&quot;&gt;define locally-scoped style rules directly within the component definition&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I learned about this feature early on as I rebuilt my website using Svelte. But I opted not to use it. However, after working through Josh’s course I decided to reconsider this choice.&lt;/p&gt;
&lt;h2 id=&quot;tailwind-css-the-game-changer-in-my-css-workflow&quot;&gt;Tailwind CSS: The Game Changer in my CSS Workflow&lt;a href=&quot;#tailwind-css-the-game-changer-in-my-css-workflow&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There’s one curious aspect regarding how I defined used to define my CSS rules. I had been using &lt;a href=&quot;https://tailwindcss.com/&quot;&gt;Tailwind CSS&lt;/a&gt; but never to its full potential. I liberally incorporated the framework’s utility classes into my custom CSS classes using the &lt;a href=&quot;https://tailwindcss.com/docs/functions-and-directives#apply&quot;&gt;&lt;code&gt;@apply&lt;/code&gt; directive&lt;/a&gt; instead of directly using the provided utility classes to style my components.&lt;/p&gt;
&lt;p&gt;Given the &lt;a href=&quot;https://tailwindcss.com/docs/reusing-styles#avoiding-premature-abstraction&quot;&gt;official documentation’s&lt;/a&gt; stance on this, I realized this was a mistake. After reading the documentation, along with &lt;a href=&quot;https://dev.to/srmagura/why-were-breaking-up-wiht-css-in-js-4g9b&quot;&gt;this article&lt;/a&gt;, and &lt;a href=&quot;https://news.ycombinator.com/item?id=35353182&quot;&gt;other discussions on Hacker News&lt;/a&gt; I soon recognized that Tailwind CSS solves two major problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Eliminating the need for unique descriptive names for style classes&lt;/li&gt;
&lt;li&gt;Facilitating locally scoped styles for components&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Interestingly enough, when styling components the issue of how to name the style classes remains. The challenge is not so much to do with how to name them to avoid duplicating style class definitions. Instead, the issue now has to do with how to name classes such that their intended purpose is clear. This challenge arises especially in cases where you have multiple nested &lt;code&gt;div&lt;/code&gt; tags in a component.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://tailwindcss.com/docs/utility-first&quot;&gt;official Tailwind CSS documentation illustrates this&lt;/a&gt;, using a chat notification component as an example. This component requires multiple classes to style nested elements such as the title of the notification, the notification message, a container for both, and more. At a certain point, coming up with unique descriptive names becomes tedious, especially when a class needs only one style.&lt;/p&gt;
&lt;p&gt;A common workaround is to use inline styles. But in most cases I would prefer keeping CSS styles defined in one place unless it’s necessary.&lt;/p&gt;
&lt;p&gt;The utility classes provided by Tailwind CSS provide a more convenient alternative. In combination with component-based frameworks, modifying styling becomes as easy as adding or removing classes from relevant components, or creating entirely new components to implement new features.&lt;/p&gt;
&lt;h2 id=&quot;refactoring-my-websites-css&quot;&gt;Refactoring My Website’s CSS&lt;a href=&quot;#refactoring-my-websites-css&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I refactored my CSS by styling components using Tailwind’s utility classes as often as possible. Despite how easy this process was, it wasn’t a perfect solution in all situations.&lt;/p&gt;
&lt;p&gt;For instance, I had to use &lt;code&gt;@apply&lt;/code&gt; directive in my &lt;code&gt;ArticleBodyText&lt;/code&gt; Svelte component that I use as a container for my blog posts. This component wraps text content, from markdown files, within &lt;code&gt;section&lt;/code&gt; tags. Within this component, the style tag included rules such as these two below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;html&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;style&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; lang=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;postcss&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;  section&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    @apply&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; text-gray-600 pb-12;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;  section&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; :global(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    @apply&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; font-normal text-base;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    @apply&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; leading-8 mb-5;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;style&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I needed to apply these specific styles to the text content inside my articles. To achieve this, I used the &lt;a href=&quot;https://svelte.dev/docs#component-format-style&quot;&gt;&lt;code&gt;:global(...)&lt;/code&gt; modifier&lt;/a&gt;. This modifier is necessary because my blog posts are written in markdown and pre-processed using &lt;a href=&quot;https://github.com/pngwn/MDsveX&quot;&gt;mdsvex&lt;/a&gt; before the content is passed into the &lt;code&gt;ArticleBodyText&lt;/code&gt; component slot. The &lt;code&gt;:global(...)&lt;/code&gt; modifier ensures that these styles are applied correctly to this pre-processed content.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Learning to manage CSS effectively has been a journey of discovery. As I continued to develop my website, it became evident that maintaining a single CSS file was an impractical approach. Resources like the Joy of React course and practical experiences guided me towards embracing component-scoped CSS, a strategy applicable across different frameworks.&lt;/p&gt;
&lt;p&gt;Tailwind CSS greatly simplified this transition. Its utility-first ideology proved to be both flexible and efficient, helping me to keep styles encapsulated and well-organized.&lt;/p&gt;</content:encoded></item><item><title>A Beginner&apos;s Intro to Algorithm Analysis</title><link>https://bryananthonio.com/blog/intro-to-algorithm-analysis/</link><guid isPermaLink="true">https://bryananthonio.com/blog/intro-to-algorithm-analysis/</guid><description>Exploring fundamentals of algorithm analysis by breaking down Big O notation, time complexity classes, and performance scenarios.</description><pubDate>Sat, 01 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When I first read Chapter 2 of the Algorithm Design Manual by Steven Skiena I was surprised to learn that there are algorithms so inefficient that it could take a supercomputer even longer than the age of the universe to run them.&lt;/p&gt;
&lt;p&gt;This insight alone helped me understand the relevance of algorithm analysis and Big O notation. These provide frameworks for evaluating and comparing the efficiency of algorithms. In this blog post, I’ll distill what I’ve learned about Big O Notation, delve into a few different classes of time complexity, and discuss analyzing algorithms under best-, worst-, and average-case scenarios.&lt;/p&gt;
&lt;h2 id=&quot;what-is-big-o-notation&quot;&gt;What is Big O Notation?&lt;a href=&quot;#what-is-big-o-notation&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Big O notation expresses how the algorithm’s runtime increases as the size of its input increases. In other words, as we give our algorithm more work (by increasing the input size), the time it takes to complete this work may also increase.&lt;/p&gt;
&lt;p&gt;Not all algorithms handle large inputs as efficiently as others. Therefore, comparing the performance of different algorithms requires examining how the number of computations an algorithm performs grows with the input size &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. We can represent this relationship as some function &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;f(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Now, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;f(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; may include several terms and constants. But when using Big O notation, it’s more important to focus on the term that grows the fastest as &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; gets very large, known as the dominant term.&lt;/p&gt;
&lt;p&gt;Why only the dominant term? That’s because Big O notation aims to capture the rate at which the runtime grows with &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, not the precise number of computations. For this reason, we also typically ignore constants as they don’t change with &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Let’s illustrate this with an example.&lt;/p&gt;
&lt;p&gt;Suppose we’ve observed that the function &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;f(n) = 6n^2 + n + 5&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.8974em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6667em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;5&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; describes an algorithm’s runtime. This function has three terms but the one that grows the fastest as &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; gets larger is &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;6n^2&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.8141em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Thus, we can say that &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;f(n) = O(n^2)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1.0641em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Notice that we’ve dropped the constant ‘6’ to emphasize how our function grows with &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; instead of the exact number of computations the algorithm performs.&lt;/p&gt;
&lt;p&gt;The expression &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;f(n) = O(g(n))&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0359em&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; means that there’s a function &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;g(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0359em&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; that satisfies the condition &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;≥&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;c \cdot g(n) \geq f(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4445em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;⋅&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0359em&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;≥&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;&amp;gt;&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n &amp;gt; n_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5782em;vertical-align:-0.0391em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3011em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; where &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;c&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;c&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3011em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; are constants. This &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3011em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is a point beyond which &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;g(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0359em&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; (scaled by the constant &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;c&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;c&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) always surpasses &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;f(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1076em&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. In our example, we found that &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;g(n) = n^2&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0359em&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.8141em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;c=6&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;6&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Although we haven’t specified &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3011em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, it’s implied that this inequality holds for sufficiently large &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;h2 id=&quot;exploring-time-complexity-classes&quot;&gt;Exploring Time Complexity Classes&lt;a href=&quot;#exploring-time-complexity-classes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Building on the concept of Big O notation we explored in the previous section, we’ll now focus on understanding time complexity classes, a key concept in algorithm analysis.&lt;/p&gt;
&lt;p&gt;These classes represented using Big O notation, provide a shorthand to describe how an algorithm’s runtime scales with input size. For example, when we say that an algorithm is &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(n^2)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1.0641em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; this means that its time complexity is quadratic, a particular class of time complexity.&lt;/p&gt;
&lt;p&gt;In this section, we’ll explore four key time complexity classes: Constant, Logarithmic, Linear, and Quadratic. Understanding these classes will provide insights into how different algorithms perform as the size of their inputs grow.&lt;/p&gt;
&lt;h3 id=&quot;constant&quot;&gt;Constant&lt;a href=&quot;#constant&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Constant time, given as &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, represents the simplest time complexity class. This corresponds to one basic unit of computation, including operations such as adding two numbers together or creating a new variable.&lt;/p&gt;
&lt;h3 id=&quot;logarithmic&quot;&gt;Logarithmic&lt;a href=&quot;#logarithmic&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Algorithms with a logarithmic runtime obey &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(\log(n))&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. These are typically binary search algorithms. Binary search is used in situations where you need to retrieve an item from a sorted list. The steps for this retrieval are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Look at the center-most item. If it’s the item we seek then return this.&lt;/li&gt;
&lt;li&gt;If the item of interest isn’t the center-most item then determine whether the item of interest appears before or after the center-most item depending on the sorting of the list&lt;/li&gt;
&lt;li&gt;Repeat this procedure with either the left or right partition of the list&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When I first learned about logarithmic time I was confused about where the &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\log(n)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; comes from. This is easy to explain.&lt;/p&gt;
&lt;p&gt;Let’s say we have &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; items which can be expressed as &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; = &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;2^s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6644em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.6644em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. The variable &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; tells us how many times we can divide &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in half. Now, take the logarithm of both sides to get &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;log(n) = s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0197em&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0359em&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Here, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; tells us the number of times we may have to divide this number of items, in the worst case, until we get what we’re looking for.&lt;/p&gt;
&lt;h3 id=&quot;linear&quot;&gt;Linear&lt;a href=&quot;#linear&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Linear runtimes correspond to cases where for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; items we need to inspect each item once.&lt;/p&gt;
&lt;p&gt;For a practical example let’s say we’re looking for the cheapest item in a shop. If don’t have a list of the items sorted by price we may have to look at each one to identify the cheapest item.&lt;/p&gt;
&lt;h3 id=&quot;quadratic&quot;&gt;Quadratic&lt;a href=&quot;#quadratic&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Quadratic runtimes occur in cases where for &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; items we need to inspect all unique pairs of the items in the set.&lt;/p&gt;
&lt;p&gt;Sorting algorithms such as bubble sort and insertion sort are &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(n^2)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1.0641em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. In the worst case, you have to sort a list that has been sorted in reverse order. This will require &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;n^2&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.8141em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; comparisons to sort the list.&lt;/p&gt;
&lt;p&gt;For example, let’s say you’re organizing a party and are creating a list of invitees. You may want to inspect all possible pairs of invitees to ensure you don’t invite two people who don’t get along. This would require a runtime of &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;O(n^2)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1.0641em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0278em&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;h2 id=&quot;understanding-algorithm-performance-best-average-and-worst-case-scenarios&quot;&gt;Understanding Algorithm Performance: Best, Average, and Worst-Case Scenarios&lt;a href=&quot;#understanding-algorithm-performance-best-average-and-worst-case-scenarios&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Algorithm analysis typically involves assessing an algorithm’s performance in three scenarios: the best, worst, and average cases.&lt;/p&gt;
&lt;p&gt;The best-case scenario represents instances where the algorithm completes its work in the shortest amount of time. In contrast, the worst case measures the upper bound of the algorithm’s runtime. Finally, the average case falls between these extremes.&lt;/p&gt;
&lt;p&gt;To illustrate, consider an algorithm tasked with sorting items in a list. In the best-case scenario, the list is already sorted and the algorithm doesn’t need to make any changes. The worst-case scenario occurs when the list is in reverse order, requiring the algorithm to rearrange every item. The average-case scenario assumes the list is in random order as only a subset of the items may need to be rearranged.&lt;/p&gt;
&lt;p&gt;You might think that the average-case scenario would be the most representative, but it’s typically a difficult case to analyze. The average case depends on the specific distribution of inputs, which we can’t always determine.&lt;/p&gt;
&lt;p&gt;For instance, for sorting items in a list could we identify the most probable inputs that the algorithm would have to sort? Most likely not. In contrast, it’s much easier to identify the types of inputs that correspond to the worst case.&lt;/p&gt;
&lt;p&gt;It’s often the worst case that should interest us the most for a few reasons. First, the worst case is generally simpler to analyze. Understanding it well may expose opportunities to optimize an algorithm. Finally, It also guarantees an upper limit on the algorithm’s runtime, regardless of the specific input. This is particularly important for software systems where predictability is key.&lt;/p&gt;
&lt;p&gt;Consider a real-time system that requires the completion of tasks within specific time constraints. Knowing the worst-case scenario allows us to make guarantees about the system’s ability to perform these tasks within an acceptable time frame even under unfavorable conditions.&lt;/p&gt;
&lt;p&gt;One example of a real-time system is a social media platform. Here, it may be favorable to establish guarantees of how long it takes for new posts and comments by users to become available to other users on the platform. If we find that these guarantees don’t hold, then it would be necessary to optimize the worst-case runtime.&lt;/p&gt;
&lt;p&gt;Lastly, while the best-case scenario may seem desirable, it’s the least informative about an algorithm’s performance. In most real-world scenarios inputs may not always resemble those of the best case. Hence, analyzing an algorithm’s best-case performance may provide an overly optimistic view that doesn’t reflect its practical utility.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Algorithm analysis is a fundamental skill in computer science. Understanding the basics of Big O notation is necessary to recognize the runtime of your algorithms, and hence, the speed and scalability of your code.&lt;/p&gt;
&lt;p&gt;In this blog post, we’ve examined the different classes of time complexity from constant to quadratic and their implications on an algorithm’s runtime. Despite this, there’s no ‘one-size-fits-all’ solution when choosing the most efficient algorithm. The best algorithm for a given task depends on many factors, including the input size and specific constraints of the problem.&lt;/p&gt;
&lt;p&gt;These foundational concepts can help you evaluate algorithms under best-, worst- and average-case scenarios. But remember that algorithm analysis is a broad field with so much more to explore. Other topics including space complexity, trade-offs between time and space, more complex time complexities, and the use of data structures for optimizing algorithms are all worthy of further study.&lt;/p&gt;</content:encoded></item><item><title>Writing My First Elixir Program</title><link>https://bryananthonio.com/blog/first-elixir-program/</link><guid isPermaLink="true">https://bryananthonio.com/blog/first-elixir-program/</guid><description>Sharing the process of writing my first Elixir program, which focuses on handling image processing tasks.</description><pubDate>Sun, 30 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I recently started learning &lt;a href=&quot;https://elixir-lang.org&quot;&gt;Elixir&lt;/a&gt; to learn backend development and to take on the challenge of learning a new programming language.&lt;/p&gt;
&lt;p&gt;Elixir’s scalability and fault-tolerant capabilities, ideal for handling concurrent tasks like image processing, drew me to learn it. I was also excited that Elixir is a functional programming language.&lt;/p&gt;
&lt;p&gt;The last time I worked with a functional programming language was when, as an undergraduate student at Cornell, I took the &lt;a href=&quot;https://cornellcswiki.gitlab.io/classes/CS3110.html&quot;&gt;CS 3110&lt;/a&gt; course on functional programming using &lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;During that period, I realized that utilizing concepts such as pattern matching and the pipe operator enhanced the readability of my code. Hence, I was looking forward to revisiting these concepts once more.&lt;/p&gt;
&lt;p&gt;In this post, I will share my experience of putting together my first Elixir program. The objective for this program is to provide a pipeline for preprocessing photographs I’ve taken to share in the photography section of my website.&lt;/p&gt;
&lt;p&gt;To provide some context, the photo files in question are 24-megapixel JPEG files that need to be resized to be displayed optimally on the web by using &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images#how_do_you_create_responsive_images&quot;&gt;responsive features&lt;/a&gt; of the &lt;code&gt;img&lt;/code&gt; tag in HTML. I’ll also need to create &lt;a href=&quot;https://developers.google.com/speed/webp&quot;&gt;webp&lt;/a&gt; equivalents as these tend to be more lightweight than their jpeg counterparts.&lt;/p&gt;
&lt;p&gt;You can find the code for this project &lt;a href=&quot;https://github.com/photon-collider/imag_proc&quot;&gt;in this Github repository&lt;/a&gt;. As I go over my code, I will assume you are a beginner to programming in Elixir but have experience with other programming languages such as JavaScript or Python.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#setting-up-elixir&quot;&gt;Setting Up Elixir&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#project-dependencies&quot;&gt;Project Dependencies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#handling-input-and-output-operations-for-images&quot;&gt;Handling Input and Output Operations for Images&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#loading-images&quot;&gt;Loading Images&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#function-naming-conventions-pattern-matching-and-no-return-keyword&quot;&gt;Function Naming Conventions, Pattern Matching, and no return Keyword&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#retrieving-image-file-paths&quot;&gt;Retrieving Image File Paths&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#generating-resized-images&quot;&gt;Generating Resized Images&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#main-loop-for-iterating-over-image-files-and-desired-output-file&quot;&gt;Main Loop for Iterating Over Image Files and Desired Output File&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#generating-resized-images-step-by-step&quot;&gt;Generating Resized Images Step-by-Step&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#running-the-script&quot;&gt;Running the Script&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#next-steps&quot;&gt;Next Steps&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;setting-up-elixir&quot;&gt;Setting Up Elixir&lt;a href=&quot;#setting-up-elixir&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can install the Elixir on macOS using &lt;a href=&quot;https://brew.sh&quot;&gt;homebrew&lt;/a&gt; by entering &lt;code&gt;brew install elixir&lt;/code&gt; in the terminal. For instructions on installing Elixir on other platforms visit &lt;a href=&quot;https://elixir-lang.org/install.html#distributions&quot;&gt;the official website here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;With Elixir installed you should be able to run Elixir’s interactive shell in the terminal by running the &lt;code&gt;iex&lt;/code&gt; command in a command-line terminal.&lt;/p&gt;
&lt;p&gt;Elixir uses the build tool &lt;code&gt;mix&lt;/code&gt; to handle tasks such as generating new projects with some boilerplate, apply code formatting, and handle package management for project dependencies.&lt;/p&gt;
&lt;p&gt;We’ll use mix to create the project by running this command in the terminal&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mix&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; image_proc&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will create a new directory named &lt;code&gt;image_proc&lt;/code&gt; containing some additional files and folders. These include files and directories such as&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/lib&lt;/code&gt; - directory where the elixir code for the project will be stored&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/test&lt;/code&gt; - directory for storing unit tests that you can run using the testing library &lt;a href=&quot;https://hexdocs.pm/ex_unit/ExUnit.html&quot;&gt;ExUnit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.formatter.exs&lt;/code&gt; - config file for the formatting tool provided by &lt;code&gt;mix&lt;/code&gt; which can be invoked by running &lt;code&gt;mix format&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mix.exs&lt;/code&gt; and &lt;code&gt;mix.lock&lt;/code&gt; - used by &lt;code&gt;mix&lt;/code&gt; for managing project builds and dependencies&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;More information regarding these details can be found &lt;a href=&quot;https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html&quot;&gt;here in the official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;project-dependencies&quot;&gt;Project Dependencies&lt;a href=&quot;#project-dependencies&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This project requires the &lt;a href=&quot;https://hexdocs.pm/image/readme.html&quot;&gt;&lt;code&gt;Image&lt;/code&gt;&lt;/a&gt; library which will be used as a dependency for generating resized images and converting them to other image formats.&lt;/p&gt;
&lt;p&gt;Add &lt;code&gt;{:image, &amp;quot;~&amp;gt; 0.25.1&amp;quot;}&lt;/code&gt; to your list of dependencies in the &lt;code&gt;mix.exs&lt;/code&gt; file like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defp&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; deps&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    {&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;~&amp;gt; 0.25.1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After that you’ll have to run the following command in the terminal to install this dependency:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mix&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; deps.get&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;handling-input-and-output-operations-for-images&quot;&gt;Handling Input and Output Operations for Images&lt;a href=&quot;#handling-input-and-output-operations-for-images&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;loading-images&quot;&gt;Loading Images&lt;a href=&quot;#loading-images&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;First, we’ll create the first module dedicated to handling input and output operations for image files. Create the file &lt;code&gt;image_io.ex&lt;/code&gt; inside the &lt;code&gt;image_proc/lib&lt;/code&gt; directory.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defmodule&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ImageIO&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All functions in this module go in the body of the module between the &lt;code&gt;do&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; keywords. The first function to define here will be used to load images from a given file path:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load_image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image_path) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  image_path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;open!&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;minimize_metadata!&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Functions and other clauses are also wrapped using &lt;code&gt;do&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; keywords. This syntax differs from the curly braces used in languages such as JavaScript or the whitespaces used by Python.&lt;/p&gt;
&lt;p&gt;As I’ve alluded to before, I’ve missed having access to the piping operator, given as &lt;code&gt;|&amp;gt;&lt;/code&gt; in Elixir, in other imperative languages that I use regularly, such as Python.&lt;/p&gt;
&lt;p&gt;Piping makes it easier to compose a sequence of computations in an easy-to-read manner. Doing the equivalent in a language like Python would look something like&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load_image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image_path):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Image.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;image_path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; Image.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;minimize_metadata&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While this isn’t too bad to read. When defining more complex functions this type of syntax can get a bit hairy. I also find it easier to read the Elixir equivalent better.&lt;/p&gt;
&lt;h3 id=&quot;function-naming-conventions-pattern-matching-and-no-return-keyword&quot;&gt;Function Naming Conventions, Pattern Matching, and no &lt;code&gt;return&lt;/code&gt; Keyword&lt;a href=&quot;#function-naming-conventions-pattern-matching-and-no-return-keyword&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One thing I’ve found interesting about Elixir is its conventions for naming functions.&lt;/p&gt;
&lt;p&gt;In the &lt;code&gt;load_image&lt;/code&gt; definition I’ve used the &lt;code&gt;open!&lt;/code&gt; and &lt;code&gt;minimize_metadata!&lt;/code&gt; functions from the &lt;code&gt;Image&lt;/code&gt; library. These functions load the image from the provided file path and remove unneeded metadata from the image’s EXIF data, respectively.&lt;/p&gt;
&lt;p&gt;In the event that an error occurs when performing these operations, an exception is raised that can be handled using the &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt;, and &lt;code&gt;rescue&lt;/code&gt; operators. These work similarly to their equivelants in other languages such as Python’s &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt; operators.&lt;/p&gt;
&lt;p&gt;The alternative is to use the &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;minimize_metadata&lt;/code&gt; functions (named without an exclamation point). These functions will return a tuple regardless of whether they encounter an error or not.&lt;/p&gt;
&lt;p&gt;For instance, the &lt;code&gt;open&lt;/code&gt; function will return the tuple &lt;code&gt;{:ok, image}&lt;/code&gt; if no error occurs or the tuple &lt;code&gt;{:error, message}&lt;/code&gt; when one does occur. This provides an opportunity to pattern match on the outputs for error handling.&lt;/p&gt;
&lt;p&gt;It’s important to point out that the outputs &lt;code&gt;:ok&lt;/code&gt; and &lt;code&gt;:error&lt;/code&gt;, defined using a semicolon at the start of their definition, are known as &lt;a href=&quot;https://hexdocs.pm/elixir/1.12/Atom.html&quot;&gt;atoms&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;These are one of the primitive types supported by Elixir. They are distinct because they are constants whose value is the same as their name. They are very useful for implementing pattern matching structures in Elixir.&lt;/p&gt;
&lt;p&gt;With pattern matching we could handle errors by using a &lt;code&gt;case&lt;/code&gt; operator as shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;case&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	{&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;ok&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, image} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;		# DO something with loaded image here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	{&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, message} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;		# DO something here with error and the provided message&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The main reason to avoid doing this would be that it prevents us from using the piping operator to make code concise. You would also have to write a similar block of code to handle errors coming from &lt;code&gt;Image.minimize_metadata&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another thing to notice here is that Elixir doesn’t have a &lt;code&gt;return&lt;/code&gt; keyword to break out of functions. Instead, the value of the last evaluated expression is provided as the return value for functions.&lt;/p&gt;
&lt;h3 id=&quot;retrieving-image-file-paths&quot;&gt;Retrieving Image File Paths&lt;a href=&quot;#retrieving-image-file-paths&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Now that we have some code written to load images we need to figure out how to retrieve the file paths to images.&lt;/p&gt;
&lt;p&gt;Elixir’s standard library includes several packages including &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Enum&lt;/code&gt;, &lt;code&gt;Path&lt;/code&gt; that we can use for this purpose. These modules contain functions for handling strings, enumerated variables such as lists and maps, and file paths, respectively.&lt;/p&gt;
&lt;p&gt;One convenient thing about &lt;code&gt;iex&lt;/code&gt; is that you can use it to check the functions contained in any of these modules and access documentation pertaining to their use. For instance, you can open &lt;code&gt;iex&lt;/code&gt; and run&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;iex&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; h &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;File&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;to see the functions in the &lt;code&gt;File&lt;/code&gt; module which contains operations for dealing with files. If you wanted to see what &lt;code&gt;File.ls&lt;/code&gt; does, you would just have to run &lt;code&gt;h File.ls&lt;/code&gt; to get a summary of what it does and an example usecase.&lt;/p&gt;
&lt;p&gt;We’ll define the function &lt;code&gt;get_image_paths&lt;/code&gt; in the &lt;code&gt;Image_IO&lt;/code&gt; module:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_image_paths&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_input_image_folder) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  path_input_image_folder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ls!&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&amp;amp;is_image?&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; file &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;      Path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_input_image_folder, file)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    end&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First, it uses &lt;code&gt;File.ls!&lt;/code&gt; to list all files in the provided directory path. Next, the &lt;code&gt;Enum.filter/2&lt;/code&gt; function takes two arguments: a list of items and a function.&lt;/p&gt;
&lt;p&gt;The provided function is executed on each item in the list and should return either a truthy or a falsy boolean value. If a truthy value is returned, the item is kept in the list. If a falsy value is returned, the item is removed from the list.&lt;/p&gt;
&lt;p&gt;The paths returned by &lt;code&gt;File.ls!&lt;/code&gt; aren’t absolute paths. Hence, this function uses the &lt;code&gt;Enum.map&lt;/code&gt;
function, which takes an anonymous function that uses the &lt;code&gt;Path.join&lt;/code&gt; function to join the paths for path of the directory given by &lt;code&gt;path_input_image_folder&lt;/code&gt; in front of the the names of each the individual image files.&lt;/p&gt;
&lt;p&gt;Next, we need to define the &lt;code&gt;&amp;amp;is_image?/1&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Before we get to that you may wonder what the ampersand and the &lt;code&gt;/1&lt;/code&gt; label means. In Elixir, the &lt;code&gt;/1&lt;/code&gt; refers to the function’s arity which otherwise represents the number of arguments the function accepts.&lt;/p&gt;
&lt;p&gt;The ampersand symbol &lt;code&gt;&amp;amp;&lt;/code&gt; in &lt;code&gt;&amp;amp;is_image&lt;/code&gt; represents the &lt;a href=&quot;https://hexdocs.pm/elixir/1.12/Function.html#module-the-capture-operator&quot;&gt;capture operator&lt;/a&gt; in Elixir. Here, it passes the function &lt;code&gt;is_image?&lt;/code&gt; into the second argument of &lt;code&gt;Enum.filter/2&lt;/code&gt;. As an alternative you could also insert the &lt;code&gt;is_image?&lt;/code&gt; function by wrapping it using the &lt;code&gt;fn param -&amp;gt;&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; syntax like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  path_input_image_folder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ls!&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; path_image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; is_image?&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_image) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; file &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;      Path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_input_image_folder, file)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    end&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The easiest way to check whether a given file is an image is to check it’s file extension to see if it corresponds to an image file format. This is precisely what the &lt;code&gt;is_image?&lt;/code&gt; function defined below does:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;image_filetypes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;.jpg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;.jpeg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; is_image?&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(filename) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  file_extension &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; filename&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;extname&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;downcase&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    Enum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;member?&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(@&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;image_filetypes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, file_extension)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function checks whether a file is an image using the &lt;code&gt;@image_filetypes&lt;/code&gt; &lt;a href=&quot;https://elixir-lang.org/getting-started/module-attributes.html&quot;&gt;module attribute&lt;/a&gt;. This module attribute holds a list of image file formats, specifically &lt;code&gt;.jpg&lt;/code&gt; and &lt;code&gt;.jpeg&lt;/code&gt;, which helps identify whether a given file is an image that needs processing.&lt;/p&gt;
&lt;p&gt;Module attributes are limited to the scope within the module they are defined in, allowing us to use &lt;code&gt;@image_filetypes&lt;/code&gt; in any of the functions defined in the &lt;code&gt;ImageIO&lt;/code&gt; module. Using module attributes prevents us from having to hardcode parameters in all places that they will be used.&lt;/p&gt;
&lt;p&gt;Finally, we’ll define a function for generating the file name of the saved image, which takes three arguments including the image name, the image’s filetype, and a prefix for the image’s name:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_output_filename&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(imagename, image_filetype, imagename_prefix) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; imagename_prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    nil&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{imagename}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{image_filetype}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;    _&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{imagename_prefix}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{imagename}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{image_filetype}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function pattern matches the variable &lt;code&gt;imagename_prefix&lt;/code&gt; to determine its return value. When no value is provided for this argument, the first case handles such instances by generating a string using the string interpolation syntax given by &lt;code&gt;#{}&lt;/code&gt; to insert the values of the variables &lt;code&gt;imagename&lt;/code&gt; and &lt;code&gt;image_filetype&lt;/code&gt; into the returned formatted string. In the catch all case given by &lt;code&gt;_ -&amp;gt;&lt;/code&gt; the generated string incorporates all three input arguments.&lt;/p&gt;
&lt;h2 id=&quot;generating-resized-images&quot;&gt;Generating Resized Images&lt;a href=&quot;#generating-resized-images&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The next step will focus on implementing functions for processing loaded images. Create a new file called &lt;code&gt;image_proc.ex&lt;/code&gt; inside &lt;code&gt;image_proc/lib&lt;/code&gt; directory. This module, called &lt;code&gt;ImageProc&lt;/code&gt;, will contain the image processing functions.&lt;/p&gt;
&lt;p&gt;This file should start off with this code&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;defmodule&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ImageProc&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Like before, any function definitions will go in the space between the &lt;code&gt;do&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; keywords.&lt;/p&gt;
&lt;h3 id=&quot;main-loop-for-iterating-over-image-files-and-desired-output-file&quot;&gt;Main Loop for Iterating Over Image Files and Desired Output File&lt;a href=&quot;#main-loop-for-iterating-over-image-files-and-desired-output-file&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;First, we need to define a function that takes a path to an input folder that contains images and another path to a directory where the resized images will be saved.&lt;/p&gt;
&lt;p&gt;If the output directory doesn’t already exist, it will be generated. Moreover, the function should iterate over the image paths and the output image filetypes to be generated. Thie &lt;code&gt;process_images&lt;/code&gt; functions defined below achieves this:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;imagename_prefix&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;output_image_filetypes&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;jpeg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;webp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; process_images&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_input_folder, path_output_folder) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  lst_paths_image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ImageIO&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get_image_paths&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_input_folder)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;exists?&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_output_folder) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mkdir_p&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_output_folder)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; path_image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; lst_paths_image, image_filetype &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; @&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;output_image_filetypes&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    imagename &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_imagename&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_image)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    ImageIO&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;load_image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_image)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; generate_resized_images&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        image_filetype,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        imagename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        path_output_folder,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        @&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;imagename_prefix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I appreciate the for-loop syntax in Elixir because it’s very easy to specify the conditions for a nested for-loop that loops over both the list of image paths and the list of output image filetypes, all in just one line code!&lt;/p&gt;
&lt;p&gt;In this loop the function &lt;code&gt;get_imagename&lt;/code&gt; gets the name of the image file, stripping away its absolute path information and the image filetype extension, removes any whitespace, and lowercases each of the characters:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_imagename&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(path_image) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  path_image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;rootname&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;basename&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;downcase&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;generating-resized-images-step-by-step&quot;&gt;Generating Resized Images Step-by-Step&lt;a href=&quot;#generating-resized-images-step-by-step&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Next, once the image has been loaded the &lt;code&gt;generate_resized_images&lt;/code&gt; function is invoked:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; generate_resized_images&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    image,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    image_filetype,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    imagename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    output_folder_path,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    imagename_prefix &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;\\&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  ) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  save_folder_path &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Path&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(output_folder_path, imagename)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;exists?&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(save_folder_path) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    File&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(save_folder_path)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  image_aspect &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;aspect&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  long_edge_final_map &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_resized_long_edge_map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image_aspect)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  image_long_edge_len &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_long_edge_len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image, image_aspect)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; {sizename, long_edge_final} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; long_edge_final_map &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    scale_factor &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; long_edge_final &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image_long_edge_len&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    {&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;ok&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, resized_image} &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;resize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(scale_factor)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    resized_imagename &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{imagename}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{sizename}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    resized_image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    |&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ImageIO&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;save_image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	    resized_imagename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	    save_folder_path,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	    image_filetype,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	    imagename_prefix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    IO&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;puts&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Writing image &lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{resized_imagename}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{image_filetype}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; to &lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;#{save_folder_path}&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There’s quite a bit to unpack here. But these are the key steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If one doesn’t already exist, generate an appropriate folder path where the resized images will be saved&lt;/li&gt;
&lt;li&gt;Determine the aspect ratio of the image. Use this to select a map of key-value pairs where they keys correspond to sizes &lt;code&gt;sm&lt;/code&gt;, &lt;code&gt;md&lt;/code&gt;, &lt;code&gt;lg&lt;/code&gt; and the values correspond to the length of the image’s long edge in pixels for each size once the image has been resized&lt;/li&gt;
&lt;li&gt;For each of the sizes in the map, calculate a scale factor needed to resize the image to the desired size.&lt;/li&gt;
&lt;li&gt;Save the image to the output directory&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In step 2, the function &lt;code&gt;Image.aspect&lt;/code&gt; is used. It returns one of the following atoms depending on the input image’s aspect ratio:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:landscape&lt;/code&gt; for images with a landscape aspect ratio&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:portrait&lt;/code&gt; for images with a portrait aspect ratio&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:square&lt;/code&gt; for images with a square aspect ratio&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The aspect ratio is first used in the function &lt;code&gt;get_resized_long_edge_map&lt;/code&gt; to select the appropriate map depending on whether or not the image has a square aspect ratio:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_resized_long_edge_map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image_aspect) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image_aspect &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    :&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;square&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; @&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;resized_long_edge_map_square&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;    _&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; @&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;resized_long_edge_map&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the image has a square aspect ratio, the &lt;code&gt;@resized_long_edge_map_square&lt;/code&gt; mapping is used, otherwise the &lt;code&gt;@resized_long_edge_map&lt;/code&gt; is used.&lt;/p&gt;
&lt;p&gt;In addition, these are the definitions of the two maps used by this function:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;resized_long_edge_map_square&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; %{&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sm&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 334&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;md&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 668&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;lg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1334&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;resized_long_edge_map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; %{&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sm&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;md&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;lg&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 2000&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For a square aspect ratio image, the script generates small, medium, and large size images with dimensions of 334x334 , 668x668, and 1334x1334, respectively.&lt;/p&gt;
&lt;p&gt;Similarly, portrait or landscape images are resized such that their long edges are 500, 1000, and 2000 pixels long for the small, medium, and large sizes, respectively.&lt;/p&gt;
&lt;p&gt;The only remaining helper function to define is &lt;code&gt;get_long_edge_len&lt;/code&gt;. This function returns the appropriate length corresponding to the image’s long edge, depending on its aspect ratio:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_long_edge_len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(image, image_aspect) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image_aspect &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    :&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;portrait&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;height&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;    _&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; image &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;|&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; Image&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;width&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is all that is needed for this script!&lt;/p&gt;
&lt;h2 id=&quot;running-the-script&quot;&gt;Running the Script&lt;a href=&quot;#running-the-script&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;To run the script, first enter the interactive Elixir shell by executing &lt;code&gt;iex -S mix&lt;/code&gt; in your terminal.&lt;/p&gt;
&lt;p&gt;You can then run the &lt;code&gt;process_images&lt;/code&gt; function by providing the path to the input image directory and the path to the output image directory as arguments:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;elixir&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;iex&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; ImageProc&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;process_images&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;input&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;directory&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;output&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;image&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;directory&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This command will initiate the image processing workflow, resizing all images in the input directory and saving the resized versions to the specified output directory.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In this blog post, we’ve explored how to create a simple Elixir script for resizing images using the &lt;code&gt;Image&lt;/code&gt; library. We’ve covered the basics of image I/O, file manipulation, and aspect ratio-based resizing to generate different sizes of images. This script can be easily adapted and expanded upon to meet any other specific needs for image processing tasks.&lt;/p&gt;
&lt;p&gt;To recap, we’ve learned how to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Load and save images using the &lt;code&gt;Image&lt;/code&gt; library&lt;/li&gt;
&lt;li&gt;Retrieve image file paths and filter them based on their file extensions&lt;/li&gt;
&lt;li&gt;Generate resized images based on aspect ratios and predefined dimensions&lt;/li&gt;
&lt;li&gt;Save the resized images to an output folder with an appropriate naming convention&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Whether you’re working on a website, a photo management tool, or a content delivery system, this script may serve as a helpful starting point.&lt;/p&gt;
&lt;h2 id=&quot;next-steps&quot;&gt;Next Steps&lt;a href=&quot;#next-steps&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The image processing pipeline described above generates resized images and stores them locally on your computer. But what if you wanted to directly upload them to a cloud service like Amazon S3? To achieve this, we need to incorporate functionality that uses an S3 client to upload the resized images to a storage bucket.&lt;/p&gt;
&lt;p&gt;In addition, our script doesn’t take advantage of &lt;a href=&quot;https://elixir-lang.org/getting-started/processes.html&quot;&gt;Elixir’s concurrency features&lt;/a&gt;. In future improvements to the script, we could explore using these features, such as &lt;a href=&quot;https://hexdocs.pm/elixir/Task.html&quot;&gt;Tasks&lt;/a&gt;, to process and resize multiple images simultaneously. These improvements can significantly speed up the image resizing process, especially when working with a large number of images.&lt;/p&gt;
&lt;p&gt;Finally, it would be beneficial to develop a user-friendly frontend, enabling users to drag and drop images for resizing, upload them to an S3 bucket, and manage S3 bucket contents. Our ultimate goal is to construct this interface using the &lt;a href=&quot;https://www.phoenixframework.org/&quot;&gt;Phoenix&lt;/a&gt; framework.&lt;/p&gt;
&lt;p&gt;By implementing these improvements, we can optimize our image processing workflow, making it more efficient and user-friendly.&lt;/p&gt;</content:encoded></item><item><title>How Slug-Based Dynamic Routing Works in Sveltekit</title><link>https://bryananthonio.com/blog/how-sveltekit-dynamic-routes-work/</link><guid isPermaLink="true">https://bryananthonio.com/blog/how-sveltekit-dynamic-routes-work/</guid><description>How to set up your SvelteKit project to handle routing for dynamic routes of varying complexity.</description><pubDate>Wed, 08 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When I started learning SvelteKit I was confused by how this framework handles dynamic routing. I’m referring to instances where you link to a blog post with a slug at a URL given as &lt;code&gt;/blog/[postSlug]&lt;/code&gt; where &lt;code&gt;[postSlug]&lt;/code&gt; is the slug that corresponds with a given blog post. Some websites, such as those &lt;a href=&quot;https://wordpress.com/support/permalinks-and-slugs/&quot;&gt;created using WordPress&lt;/a&gt;, also include the dates in the URL slugs such that they appear something like &lt;code&gt;/blog/[year]/[month]/[day]/[postSlug]&lt;/code&gt;. In this post, I’ll write about how to handle these dynamic routes when using SvelteKit.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#prerequisites&quot;&gt;Prerequisites&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#sveltekit-routing-basics&quot;&gt;Sveltekit Routing Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#simple-dynamic-routes&quot;&gt;Simple Dynamic Routes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#handling-more-complex-dynamic-routes&quot;&gt;Handling More Complex Dynamic Routes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;a href=&quot;#prerequisites&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this post, I’ll assume you know the basics of &lt;a href=&quot;https://svelte.dev/&quot;&gt;Svelte&lt;/a&gt;. Otherwise, the &lt;a href=&quot;https://svelte.dev/tutorial/basics&quot;&gt;official tutorial&lt;/a&gt; is a good place to learn the fundamentals. Regarding SvelteKit, I’ll assume you understand how loading data works using &lt;code&gt;+page.js&lt;/code&gt; files &lt;a href=&quot;https://kit.svelte.dev/docs/load&quot;&gt;as described here&lt;/a&gt;. However, I will go over these concepts very briefly.&lt;/p&gt;
&lt;h2 id=&quot;sveltekit-routing-basics&quot;&gt;Sveltekit Routing Basics&lt;a href=&quot;#sveltekit-routing-basics&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;SvelteKit uses a file-based routing system. In other words, routes are created based on the structure of the directory in the &lt;code&gt;src/routes/&lt;/code&gt; folder within your project. As a simple example, creating the content for the route &lt;code&gt;/about&lt;/code&gt; for an “About Me” page would require creating this directory structure in your project:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;fs&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;src&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;└── routes&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	└── about&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The markup for the “About Me” page would live inside the &lt;code&gt;+page.svelte&lt;/code&gt; component like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;svelte&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;// +page.svelte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;About Me&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;Hello, world!&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Upon navigating to the route &lt;code&gt;/about&lt;/code&gt;, you will see the rendered markup defined in this component.&lt;/p&gt;
&lt;p&gt;Now, what if you wanted to define a page at the route &lt;code&gt;/blog&lt;/code&gt; containing a list of your blog posts? You would need to create the directory &lt;code&gt;src/routes/blog&lt;/code&gt; that contains the files &lt;code&gt;+page.js&lt;/code&gt; and &lt;code&gt;+page.svelte&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;fs&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;src&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;└── routes&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	├── about&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	│ └── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.svelte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	└── blog&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		├── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;page.js&lt;/code&gt; file will contain a load function that you can use to fetch a list of blog posts. This function passes its output to the &lt;code&gt;data&lt;/code&gt; prop inside the corresponding &lt;code&gt;+page.svelte&lt;/code&gt; component. Here’s an example of how this works:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// assuming you have a fetchPosts function defined in lib/utils.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;fetchPosts&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; }&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; from&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;$lib/utils&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// src/blog/+page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; posts &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; fetchPosts&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;posts&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;html&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;// src/blog/+page.svelte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  export&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; let&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;Blog Posts&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;h1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  {#each data.posts as post}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; href=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;{post.url}&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;{post.title}&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;a&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  {/each}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As an aside, if your load function should run on the server side rather than on the client side, you should rename &lt;code&gt;+page.js&lt;/code&gt; to &lt;code&gt;+page.server.js&lt;/code&gt;. The &lt;a href=&quot;https://kit.svelte.dev/docs/load&quot;&gt;official SvelteKit documentation&lt;/a&gt; provides more info on this.&lt;/p&gt;
&lt;h2 id=&quot;simple-dynamic-routes&quot;&gt;Simple Dynamic Routes&lt;a href=&quot;#simple-dynamic-routes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you have blog posts defined at the routes &lt;code&gt;/blog/hello-world&lt;/code&gt;, &lt;code&gt;/blog/second-post&lt;/code&gt;, and &lt;code&gt;/blog/third-post&lt;/code&gt;, it would be cumbersome to have to create the directories &lt;code&gt;src/routes/blog/hello-world&lt;/code&gt;, &lt;code&gt;src/routes/blog/second-post&lt;/code&gt; and &lt;code&gt;src/routes/blog/third-post&lt;/code&gt;, respectively and create corresponding &lt;code&gt;+page.svelte&lt;/code&gt; and &lt;code&gt;+page.js&lt;/code&gt; files for each of these routes.&lt;/p&gt;
&lt;p&gt;Fortunately, you don’t have to do that in SvelteKit! In this case, you’ll instead need to create the directory &lt;code&gt;src/blog/[slug]&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;fs&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;src&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;└── routes&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	└── blog&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		├── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		├── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.svelte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			├── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code&gt;[slug]&lt;/code&gt; will be a parameter used to handle the logic needed for each route mentioned earlier. Using this design, when accessing the route &lt;code&gt;/blog/hello-world&lt;/code&gt; the params object for the load function defined in &lt;code&gt;[slug]/+page.js&lt;/code&gt; will have a corresponding &lt;code&gt;slug&lt;/code&gt; property:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;slug&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;hello-world&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can access this property in the load function to fetch the appropriate blog post based on the &lt;code&gt;slug&lt;/code&gt; and pass it to the corresponding &lt;code&gt;+page.svelte&lt;/code&gt; this way:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;// [slug]/+page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; async&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({ params }) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; postData &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; getPost&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;params&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;slug);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;postData&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;handling-more-complex-dynamic-routes&quot;&gt;Handling More Complex Dynamic Routes&lt;a href=&quot;#handling-more-complex-dynamic-routes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Now, what what about the route &lt;code&gt;/blog/[year]/[month]/[day]/[postSlug]&lt;/code&gt;? You can of course create the nested folder structure &lt;code&gt;src/routes/blog/[year]/[month]/[day]/[post]&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;src/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;└── routes/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;	└── blog/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		└── [year]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			└── [month]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				└── [day]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;					└── [post]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;						├── +page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;						└── +page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Accessing the route &lt;code&gt;/blog/2023/01/01/hello-world&lt;/code&gt;, the load function defined in &lt;code&gt;[post]/+page.js&lt;/code&gt; directory will have access to the params objects with these properties:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;year&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;2023&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;month&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;01&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;01&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;hello-world&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But this might be cumbersome if you don’t intend to load any content in routes such as &lt;code&gt;/blog/[year]&lt;/code&gt; or &lt;code&gt;/blog/[year]/[month]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The key insight on how to do this more elegantly is defined &lt;a href=&quot;https://kit.svelte.dev/docs/load#using-url-data-params&quot;&gt;here in the official documentation&lt;/a&gt;. If you want to keep the year slug but condense the others, you could instead create the following directory structure:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;src/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;└── routes/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;	└── blog/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		└── [year]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			└── [...postSlug]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				├── +page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;				└── +page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this instance, accessing the route &lt;code&gt;/blog/2023/01/01/hello-world&lt;/code&gt; the &lt;code&gt;params&lt;/code&gt; object obtained by the &lt;code&gt;[...postSlug]/+page.js&lt;/code&gt; would contain the following:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;year&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;2023&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;postSlug&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;01/01/hello-world&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively, you could capture the date separately from the post’s title slug with this directory structure:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;fs&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;src&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;└── routes&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	└── blog&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;		└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;[...&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;			└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;titleSlug&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;				├── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;				└── &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;params&lt;/code&gt; object accessible in the load function at &lt;code&gt;[titleSlug]/+page.js&lt;/code&gt; now looks like&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;2023/01/01&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;titleSlug&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;hello-world&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Better yet, you can create the folder &lt;code&gt;src/routes/blog/[...postSlug]&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;src/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;└── routes/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;	└── blog/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		└── [...postSlug]/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			├── +page.js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;			└── +page.svelte&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When accessing the route &lt;code&gt;/blog/2023/01/01/hello-world&lt;/code&gt;, the &lt;code&gt;params&lt;/code&gt; object in the load function defined &lt;code&gt;[..postSlug]/page.js&lt;/code&gt; will look like&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;postSlug&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;2023/01/01/hello-world&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From here, you could extract the year, month, day, and post slugs in the load function like so:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; async&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;params&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;	const&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; postSlug &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; params&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;postSlug&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;	[&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;year&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;month&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;post&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt; postSlug&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;	//add code here to determine what content to fetch based on slug info&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One important thing I didn’t cover here is how to ensure that slugs have the correct types. For instance, the year slug should always be a number. You may want to direct the user to an &lt;code&gt;Error 404&lt;/code&gt; page if this happens. SvelteKit allows you to define a custom &lt;code&gt;match&lt;/code&gt; function, &lt;a href=&quot;https://kit.svelte.dev/docs/advanced-routing#matching&quot;&gt;as shown here&lt;/a&gt;, for handling these edge cases.&lt;/p&gt;</content:encoded></item><item><title>2022: My Year in Review</title><link>https://bryananthonio.com/blog/2022-in-review/</link><guid isPermaLink="true">https://bryananthonio.com/blog/2022-in-review/</guid><description>Reviewing my personal highlights from 2022 and my plans for 2023.</description><pubDate>Mon, 16 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;So much has happened this past year. Through it all I didn’t get a chance to post a review of 2021. Nonetheless, here’s my roundup for the past year.&lt;/p&gt;
&lt;h2 id=&quot;moving-to-california&quot;&gt;Moving to California&lt;a href=&quot;#moving-to-california&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the fall of 2021, I quit my Ph.D. and accepted an offer to work at Apple as a Data Scientist. I started working full-time remotely in Texas and made plans to relocate to California in January. Moving was very stressful as I had to pack and ship several boxes to a temporary residence in California. I also had to complete a cross-country road trip from Houston to the Bay Area.&lt;/p&gt;
&lt;p&gt;I was nervous about the road trip but I enjoyed the experience. I drove through Texas, New Mexico, and Arizona. There weren’t many interesting sites to see until I arrived in California. If I had the option I would have driven through Colorado and Utah as those states are more scenic. Unfortunately, I was only going to receive compensation for taking the most efficient route rather than the most scenic one.&lt;/p&gt;
&lt;p&gt;After I arrived in California, I moved into a temporary residence. Shortly after, I had to look for an apartment to move into long-term. During about two weeks I toured several apartment complexes on weekends before I finally signed a lease. Getting furniture was difficult due to supply chain issues. It took at least a month to get a mattress and a bed frame!&lt;/p&gt;
&lt;p&gt;Nonetheless, I have enjoyed living in the Bay Area. It’s never been hard for me to find new places to explore and there’s still so much to see.&lt;/p&gt;
&lt;h2 id=&quot;switch-to-the-fujifilm-camera-system&quot;&gt;Switch to the Fujifilm Camera System&lt;a href=&quot;#switch-to-the-fujifilm-camera-system&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I started my hobby in photography with the &lt;a href=&quot;https://bryananthonio.com/blog/my-first-steps-in-photography/&quot;&gt;Nikon D3300 camera&lt;/a&gt;. After working with this camera for two years, I decided to buy a Fujifilm XT4 and a few lenses. I considered getting a Sony full-frame camera. But given that the lenses for Sony cameras tend to be very expensive and that I wasn’t going to make use of the advantages of a full-frame sensor I opted to get another APSC camera.&lt;/p&gt;
&lt;p&gt;I enjoy the tactile feel of the Fujifilm XT4. Unlike my previous camera, it’s much easier to change various settings like ISO, shutter speed, and aperture with the physical dials. Given that this is a mirrorless camera I like the added convenience of seeing the exposure of images directly in the electronic viewfinder as I frame a shot.&lt;/p&gt;
&lt;p&gt;There’s still so much to learn about how to use my Fujifilm camera to its full potential. I’ve found &lt;a href=&quot;https://www.youtube.com/@pal2tech/about&quot;&gt; pal2tech’s YouTube channel&lt;/a&gt; to be very helpful for learning the ins and outs of the Fujifilm ecosystem.&lt;/p&gt;
&lt;p&gt;For instance, I recently discovered that you can create film simulation recipes. I was also unaware of &lt;a href=&quot;https://fujifilm-x.com/en-us/support/download/software/x-raw-studio/&quot;&gt;Fujifilm X Raw Studio&lt;/a&gt;, a free software tool, that allows you to process images using your custom-created film simulations. I may use this in some situations to get around manually post-process images in Capture One and Photoshop.&lt;/p&gt;
&lt;h2 id=&quot;travels&quot;&gt;Travels&lt;a href=&quot;#travels&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I went on several trips this year to various places. (I’ll add pictures to this post later)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Crater Lake&lt;/li&gt;
&lt;li&gt;Big Sur&lt;/li&gt;
&lt;li&gt;Lassen National Park&lt;/li&gt;
&lt;li&gt;Sequoia National Park&lt;/li&gt;
&lt;li&gt;Lake Tahoe&lt;/li&gt;
&lt;li&gt;Copenhagen&lt;/li&gt;
&lt;li&gt;Paris&lt;/li&gt;
&lt;li&gt;Rome&lt;/li&gt;
&lt;li&gt;Yosemite National Park&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;learning-spanish&quot;&gt;Learning Spanish&lt;a href=&quot;#learning-spanish&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In 2021, I put a lot of effort into learning French after quitting my Ph.D. This involved finding pen pals to practice speaking and writing in French. These efforts began to subside after I moved to California as I got very busy with getting settled into my new residence and adapting to the demands of my new job.&lt;/p&gt;
&lt;p&gt;My interest in continuing to learn French waned after realizing that French isn’t the easiest language to maintain in the US. It’s not very often that I come across French speakers where I live. My partner speaks Spanish so I’ve opted to learn Spanish in the coming year.&lt;/p&gt;
&lt;p&gt;Learning Spanish so far has been challenging. I realized I’ve forgotten how difficult it can be to learn and memorize verb conjugations. It has made me appreciate my knowledge of French as I’ve gotten to a stage where I don’t have to think much about conjugations.&lt;/p&gt;
&lt;p&gt;In the meantime, I’m maintaining my knowledge of French by listening to podcasts and occassionally reading books and articles in French.&lt;/p&gt;
&lt;h2 id=&quot;reading&quot;&gt;Reading&lt;a href=&quot;#reading&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Each year I strive to read at least 12 books. This time I managed to read 6 books which include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Le Manuel d’Epictete&lt;/em&gt; by Epictetus&lt;/strong&gt; - The French translation of the Manual of Epictetus.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Breakfast with Seneca&lt;/em&gt; by David Fiedler&lt;/strong&gt; - Goes into some of the themes of Stoic philosophy as described by Seneca&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Dune&lt;/em&gt; by Frank Herbert&lt;/strong&gt; - A sci-fi classic. I greatly enjoyed the world-building.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Becoming Steve Jobs&lt;/em&gt; by Brent Schlender&lt;/strong&gt; - I read this book to get another perspective on Steve Jobs’s life.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;After Steve&lt;/em&gt;&lt;/strong&gt; by Tripp Mickle - After reading &lt;em&gt;Becoming Steve Jobs&lt;/em&gt; I became curious about how Apple has changed since he passed away.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;The Making of a Manager&lt;/em&gt; by Julie Zhuo&lt;/strong&gt; - This book helped me better understand the role and duties of a manager.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;thoughts-on-my-career-trajectory&quot;&gt;Thoughts on my Career Trajectory&lt;a href=&quot;#thoughts-on-my-career-trajectory&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;pivoting-away-from-deep-learning&quot;&gt;Pivoting Away from Deep Learning&lt;a href=&quot;#pivoting-away-from-deep-learning&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In my previous &lt;a href=&quot;https://bryananthonio.com/blog/2020-in-review/&quot;&gt;review for 2020&lt;/a&gt;, I stated my desire to become an expert in machine learning. I have since then changed my mind. After experimenting with deep learning in my graduate studies I no longer found the subject very engaging. My experiences taught me that deep learning models still function as black boxes. It can be hard to figure out why they do and do not work. I’ve found this to be very unsatisfying.&lt;/p&gt;
&lt;p&gt;In addition, training models for me felt very repetitive. I would tweak a small set of parameters, wait for the model to train, and then examine a set of metrics to determine if this new change of parameters made any difference. After talking to a few professional practitioners about their own experiences I learned that these experiences of mine weren’t uncommon.&lt;/p&gt;
&lt;p&gt;I had fun learning about some of the mathematics underlying the backpropagation algorithm and other aspects of neural networks. But this was not enough to sustain my interest in the field.&lt;/p&gt;
&lt;p&gt;Notably, this past year has been a breakthrough for machine learning given the success of generative models such as Stable Diffusion and Chat GPT. Because so much money and resources are needed to train such models from scratch, I envision it would be better to learn how to use these tools rather than learn how to build them in the future.&lt;/p&gt;
&lt;h3 id=&quot;interest-in-software-engineering&quot;&gt;Interest in Software Engineering&lt;a href=&quot;#interest-in-software-engineering&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I’ve identified that I would like to improve my software development skills. I love learning to build things with code. I also enjoy solving small logic puzzles on my way to completing a project. I’m aiming to learn how to write better maintainable code, and collaborate with other developers on large, complex projects. I also want to learn about algorithms and system design.&lt;/p&gt;
&lt;h2 id=&quot;goals-for-2023&quot;&gt;Goals for 2023&lt;a href=&quot;#goals-for-2023&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Here are some of my goals for the coming year:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reach B2 level in Spanish&lt;/li&gt;
&lt;li&gt;Complete &lt;a href=&quot;https://www.clondon.me/blog/52-2023&quot;&gt;Chelsea London’s 52-week photography challenge&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Write 12 blog posts&lt;/li&gt;
&lt;li&gt;Read 12 books&lt;/li&gt;
&lt;li&gt;Finish an algorithms course&lt;/li&gt;
&lt;li&gt;Learn more about systems design&lt;/li&gt;
&lt;li&gt;Read these books on software development
&lt;ul&gt;
&lt;li&gt;A Philosophy of Software Design&lt;/li&gt;
&lt;li&gt;Pragmatic Programmer&lt;/li&gt;
&lt;li&gt;Designing Data-Intensive Applications&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title>Thinking About the Value of Writing</title><link>https://bryananthonio.com/blog/thinking-about-the-value-of-writing/</link><guid isPermaLink="true">https://bryananthonio.com/blog/thinking-about-the-value-of-writing/</guid><description>The value of writing and how it relates to learning in public</description><pubDate>Sun, 13 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;How does writing give value to readers? I don’t think I thought much about this question when I first learned how to write. However, I pondered this more seriously after watching a &lt;a href=&quot;https://www.youtube.com/watch?v=aFwVf5a3pZM&quot;&gt;lecture by Larry McEnerney&lt;/a&gt;, the director of the writing program at the University of Chicago.&lt;/p&gt;
&lt;p&gt;McEnerney remarked that despite several years of practice and instruction, many students haven’t developed adequate writing skills. He explained that this occurs because students haven’t learned how to produce writing that offers value to readers.&lt;/p&gt;
&lt;p&gt;I found this very relatable. While I was a student, I often wrote at the behest of instructors and standardized exams. The purpose of these writings was often to answer a prompt regarding a book or an article. On rare occasions, I had the creative freedom to research a topic of interest and write about that.&lt;/p&gt;
&lt;p&gt;In these assignments, the purpose wasn’t to give value to readers. I wrote to demonstrate that I knew what I was talking about and that I could write well, according to a grading rubric. My audience included instructors tasked with giving me a grade.&lt;/p&gt;
&lt;h2 id=&quot;what-is-valuable-writing&quot;&gt;What is Valuable Writing?&lt;a href=&quot;#what-is-valuable-writing&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The value of a piece of writing depends on the audience. In classroom settings, instructors are typically the only audience members. As a result, students write for people who, in McEnenery’s words, are paid to care about their students’ writing. Outside the classroom, writers must convince readers to care about their writing.&lt;/p&gt;
&lt;p&gt;After all, good writing educates readers or changes their minds on a particular topic. McEnerney argued that most writing courses don’t emphasize this as students often write about topics that their instructors already know very well.&lt;/p&gt;
&lt;p&gt;He also noted that good writing possesses value, persuasiveness, organization, and clarity. Writing education curricula focus on the latter three. Perhaps this is because these are easier to teach and evaluate in a grading rubric. Much of this is relevant to academic writing. But where does writing for a personal blog fit in this discussion?&lt;/p&gt;
&lt;h2 id=&quot;the-value-of-writing-a-blog&quot;&gt;The Value of Writing a Blog&lt;a href=&quot;#the-value-of-writing-a-blog&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Personal blogs present an interesting case in that they function like a public diary. Thus, blogs can provide an avenue to practice writing for more diverse audiences beyond just course instructors. But one area where a blog surpasses a private diary is by sharing knowledge, not only with yourself as the author but also with your readers.&lt;/p&gt;
&lt;p&gt;A common adage states that one of the best ways to learn is by teaching. Thus, writing about what you’ve learned can help you better understand the material.&lt;/p&gt;
&lt;p&gt;This concept captures what it means to &lt;a href=&quot;https://www.swyx.io/learn-in-public/&quot;&gt;learn in public&lt;/a&gt;. By writing about your journey in learning new things, a blog post functions as a platform to teach others who were once in your shoes. Doing this can be as simple as &lt;a href=&quot;https://jvns.ca/blog/2021/05/24/blog-about-what-you-ve-struggled-with/&quot;&gt;writing about your struggles&lt;/a&gt; in learning new concepts or &lt;a href=&quot;https://simonwillison.net/2022/Nov/6/what-to-blog-about/&quot;&gt;writing short-form TIL posts&lt;/a&gt; about anything you recently learned. It can also provide an opportunity for people possessing more expert knowledge on your topic to offer feedback and further guidance.&lt;/p&gt;
&lt;p&gt;Learning in public is one of the most valuable aspects of writing for my blog. Indeed, there are many things I wish to learn. Documenting the new skills I’ve learned will not only benefit my future self but will also help others benefit from any insights I’ve gained along the way.&lt;/p&gt;</content:encoded></item><item><title>Redesign: Version 2.0</title><link>https://bryananthonio.com/blog/redesign-2/</link><guid isPermaLink="true">https://bryananthonio.com/blog/redesign-2/</guid><description>Documenting my first ever redesign of my personal website</description><pubDate>Sun, 06 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It’s been a long time since I posted here. A lot has happened in that time, some of which I’ll write about in future blog posts. During my hiatus, I’ve been working on this website redesign which I’m finally presenting today!&lt;/p&gt;
&lt;p&gt;I designed my very own minimalist theme for this site. In doing this, I took the time to rebuild this site using &lt;a href=&quot;https://kit.svelte.dev/&quot;&gt;Sveltekit&lt;/a&gt;. When I first deployed this website, I used the static site generator &lt;a href=&quot;https://www.11ty.dev/&quot;&gt;Eleventy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;My decision to pivot to using a different framework came about earlier this year when I learned how to use &lt;a href=&quot;https://reactjs.org/&quot;&gt;React&lt;/a&gt; for some front-end work at my job. I enjoyed the experience of setting up components to handle state logic so much that I opted to pursue rebuilding my website using a front-end framework.&lt;/p&gt;
&lt;p&gt;I could have continued using React as my front-end framework of choice, especially given that it has a very mature ecosystem. Instead, I opted to learn &lt;a href=&quot;https://svelte.dev/&quot;&gt;Svelte&lt;/a&gt; out of curiosity. It hasn’t been too hard for me to learn the basics. I’ve even had some fun learning the ins and outs of this framework.&lt;/p&gt;
&lt;p&gt;Here are some screenshots of the new design I’ve implemented alongside those of the original design. These are some notable design changes I’ve made:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Switch to a grayscale color theme for most links, buttons, and text to simplify the visual design&lt;/li&gt;
&lt;li&gt;Simplified blog post list layout by removing divider lines and other meta data (time to read, tags) for each blog post to cut down on visual clutter&lt;/li&gt;
&lt;li&gt;No social media links as I don’t use instagram or twitter anymore&lt;/li&gt;
&lt;li&gt;Moved blog post tags to the bottom of each blog post&lt;/li&gt;
&lt;li&gt;Added a short personal bio at the bottom of each blog post&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;new-layout&quot;&gt;New Layout&lt;a href=&quot;#new-layout&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;front-page&quot;&gt;Front page&lt;a href=&quot;#front-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/new-home_hdqAM.webp&quot; alt=&quot;New design of the home page&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1000&quot; height=&quot;1104&quot; class=&quot;screenshot__img&quot;&gt;&lt;/figure&gt;
&lt;h3 id=&quot;blog-page&quot;&gt;Blog page&lt;a href=&quot;#blog-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/new-blog_Z536GK.webp&quot; alt=&quot;New design of the blog page&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1000&quot; height=&quot;1101&quot; class=&quot;screenshot__img&quot;&gt;&lt;/figure&gt;
&lt;h3 id=&quot;photos-page&quot;&gt;Photos page&lt;a href=&quot;#photos-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/new-photos_Z1QkAXj.webp&quot; alt=&quot;New design of the photos page&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1000&quot; height=&quot;1101&quot; class=&quot;screenshot__img&quot;&gt;&lt;/figure&gt;
&lt;h3 id=&quot;about-page&quot;&gt;About page&lt;a href=&quot;#about-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/new-about_1XuORU.webp&quot; alt=&quot;New design of the about me page&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1000&quot; height=&quot;1101&quot; class=&quot;screenshot__img&quot;&gt;&lt;/figure&gt;
&lt;h3 id=&quot;blog-post-page&quot;&gt;Blog Post Page&lt;a href=&quot;#blog-post-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/new-post_Z2cpYHe.webp&quot; alt=&quot;New design of the blog post layout&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1000&quot; height=&quot;1101&quot; class=&quot;screenshot__img&quot;&gt;&lt;/figure&gt;
&lt;h2 id=&quot;old-layout&quot;&gt;Old Layout&lt;a href=&quot;#old-layout&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;front-page&quot;&gt;Front page&lt;a href=&quot;#front-page&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;figure class=&quot;figure screenshot screenshot--default&quot; data-astro-cid-l2qtljp6&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/old-home_1xUJm3.webp&quot; alt=&quot;Old design of the home page&quot; sizes=&quot;auto&quot; data-astro-cid-l2qtljp6=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1000&quot; height=&quot;1101&quot; class=&quot;screenshot__img&quot;&gt;&lt;/figure&gt;
&lt;p&gt;e imgBucket=“p9_redesign_2” imgName=“old-post.png” alt=“Old design of the blog post layout”/&amp;gt; */}&lt;/p&gt;
&lt;h2 id=&quot;items-on-my-wishlist&quot;&gt;Items on My Wishlist&lt;a href=&quot;#items-on-my-wishlist&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;These are some features that are in my roadmap for a future site update:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;del&gt;displaying a list of all tags on my blog page&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;&lt;del&gt;implement a button to make it possible to switch to dark mode&lt;/del&gt;&lt;/li&gt;
&lt;li&gt;RSS feed for my photo gallery&lt;/li&gt;
&lt;li&gt;add tags to photos contained within the photo gallery&lt;/li&gt;
&lt;li&gt;restyling equations and code blocks in blog posts to make these look more visually appealing&lt;/li&gt;
&lt;li&gt;adding a table of contents section for longer articles&lt;/li&gt;
&lt;li&gt;implement a better way to include photosets into my photo gallery&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I’ll cross items off this list as I get these done.&lt;/p&gt;</content:encoded></item><item><title>Sentiment Analysis with Naive Bayes</title><link>https://bryananthonio.com/blog/sentiment-analysis-with-naive-bayes/</link><guid isPermaLink="true">https://bryananthonio.com/blog/sentiment-analysis-with-naive-bayes/</guid><description>How to implement sentiment analysis using a naive Bayes classifier.</description><pubDate>Sat, 24 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Naive_Bayes_classifier&quot;&gt;Naive Bayes classifiers&lt;/a&gt; are used in various natural language processing tasks. These include &lt;a href=&quot;https://arxiv.org/abs/1610.09982&quot;&gt;sentiment analysis&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Naive_Bayes_spam_filtering&quot;&gt;spam filtering&lt;/a&gt;, and other types of document classifications. In this post, I will present Bayes’ theorem, the building block of naive Bayes classifiers, describe how naive Bayes classifiers work, and show how to implement these algorithms.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#what-is-bayes-theorem&quot;&gt;What is Bayes’ Theorem?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#the-naive-bayes-classifier&quot;&gt;The Naive Bayes Classifier&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#implementing-naive-bayes&quot;&gt;Implementing Naive Bayes&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#laplacian-smoothing&quot;&gt;Laplacian Smoothing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#computing-the-log-likelihood&quot;&gt;Computing the Log-Likelihood&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-is-bayes-theorem&quot;&gt;What is Bayes’ Theorem?&lt;a href=&quot;#what-is-bayes-theorem&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Naive Bayes classifier represents an application of Bayes’ theorem. Given two events &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;B&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, this theorem states that the conditional probability &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(A \ | \ B)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; that event &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occurs given that event &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;B&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; has occurred is expressed as&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;.&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt; \displaystyle P(A | B) = \frac{P(B | A) \times P(A)}{P(B)}.
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.363em;vertical-align:-0.936em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Here, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(A)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(B)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; are the probabilities that &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;A&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;B&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occur, respectively. Bayes’ theorem allows us to take an unknown quantity &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(A \ | \ B)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0502em&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and define it in terms of other relevant probabilities that we may already know.&lt;/p&gt;
&lt;p&gt;If reading this equation makes you feel like you’re reading heiroglyphics, try the following example:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(\textrm{fun year} \;|\; \textrm{no covid}) = \frac{P(\textrm{no covid} \; | \; \textrm{fun year}) \times P(\textrm{fun year})}{P(\textrm{no covid})}
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.363em;vertical-align:-0.936em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;In other words, the probability that 2020 would have been a fun year, given that the COVID-19 pandemic hadn’t occurred, can be computed if we have prior knowledge of the terms on the right-hand side. These include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{no covid} \ | \ \textrm{fun year})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: the probability that COVID-19 doesn’t occur in a fun year&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{fun year})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: the probability that any year is fun&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{no covid})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: the probability that COVID-19 doesn’t occur in any given year&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The term &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{no covid})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; can be written as the probability that COVID-19 doesn’t occur regardless of whether the year is fun or not. This is given as&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;bad year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;bad year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(\textrm{no covid}) = P(\textrm{no covid} \; | \; \textrm{fun year}) \times P(\textrm{fun year}) + P(\textrm{no covid} \; | \; \textrm{bad year}) \times P(\textrm{bad year})
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;bad year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;bad year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Now, lets assume we know the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{fun year}) = 0.8&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;bad year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.2&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{bad year}) = 1 - P(\textrm{fun year}) = 0.2&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;bad year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.7278em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.95&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{no covid} \; | \; \textrm{fun year}) = 0.95&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.95&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;bad year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.05&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{no covid} \; | \; \textrm{bad year}) = 1 - P(\textrm{no covid} \; | \; \textrm{fun year}) = 0.05&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;bad year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.7278em;vertical-align:-0.0833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.05&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These probabilities suggest that&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mtable rowspacing=&quot;0.25em&quot; columnalign=&quot;right left&quot; columnspacing=&quot;0em&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.95&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;0.05&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;0.2&lt;/mn&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.77&lt;/mn&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\begin{aligned} P(\textrm{no covid}) &amp;amp;=  0.95 \times 0.8 + 0.05 \times 0.2 \\ &amp;amp;= 0.77 \end{aligned}
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.7em;vertical-align:-1.1em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mtable&quot;&gt;&lt;span class=&quot;col-align-r&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6em&quot;&gt;&lt;span style=&quot;top:-3.76em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-2.26em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.1em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;col-align-l&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6em&quot;&gt;&lt;span style=&quot;top:-3.76em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.95&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-2.26em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.77&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.1em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;Using these results, we can determine that&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mtable rowspacing=&quot;0.25em&quot; columnalign=&quot;right left&quot; columnspacing=&quot;0em&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;fun year&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;no covid&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mn&gt;0.95&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;/mrow&gt;&lt;mn&gt;0.77&lt;/mn&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.99&lt;/mn&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\begin{aligned}\displaystyle P(\textrm{fun year} \;|\; \textrm{no covid}) &amp;amp;= \frac{0.95 \times 0.8}{0.77} \\ &amp;amp;= 0.99 \end{aligned}
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:3.5074em;vertical-align:-1.5037em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mtable&quot;&gt;&lt;span class=&quot;col-align-r&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:2.0037em&quot;&gt;&lt;span style=&quot;top:-4.0037em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.3214em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;fun year&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;no covid&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-2.1777em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.3214em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.5037em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;col-align-l&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:2.0037em&quot;&gt;&lt;span style=&quot;top:-4.0037em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.3214em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.3214em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.77&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.95&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.686em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-2.1777em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.3214em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.99&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.5037em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;In other words, the probability that any year is fun, given that the COVID-19 pandemic doesn’t occur during that same year, is about 99%. Next, I will describe how Bayes’ theorem relates to the Naive Bayes classifier.&lt;/p&gt;
&lt;h2 id=&quot;the-naive-bayes-classifier&quot;&gt;The Naive Bayes Classifier&lt;a href=&quot;#the-naive-bayes-classifier&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In sentiment analysis applications, the goal of the Naive Bayes classifier is to predict the most probable sentiment for a given text. This requires a training set of text samples that have been labeled as having either a positive or negative sentiment. Using Bayes’ theorem, the probability of interest is expressed as&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; text&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;text &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;text&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mtext&gt;pos&lt;/mtext&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;neg&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(s \ | \ \textrm{text}) = \frac{P(\textrm{text} \ | \ s) \times P(s)}{P(\textrm{text})} \quad s \in \{ \textrm{pos},\;\textrm{neg} \}
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.363em;vertical-align:-0.936em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:1em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;∈&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;where the sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; can correspond to either a positive (&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\mathrm{pos}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.625em;vertical-align:-0.1944em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) or negative (&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\mathrm{neg}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.625em;vertical-align:-0.1944em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) sentiment. The probability &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(s)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the fraction of text samples in the training set that have sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;It may not always be straightforward to determine &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;text&lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{text} | s)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;text&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\textrm{text})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. One way to simplify this is to assume that the words occurring in the text are independent of each other.&lt;/p&gt;
&lt;p&gt;In practice, this assumption is typically not correct which is why this algorithm is known as &lt;em&gt;naive&lt;/em&gt; Bayes. Regardless, this simplification allows us to define the conditional probability &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(s \ | \ w)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, the probability of a sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; given the word &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, as&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(s\;| \; w) = \frac{P(w \; | \; s) \times P(s)}{P(w)}
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.363em;vertical-align:-0.936em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;where &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(w)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents the probability that the word &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occurs in the training set. Now we can determine the probability that a piece of text has sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; as&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;x&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∏&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt; \displaystyle P(s\;| \; \mathrm{text}) = P(s) \times \prod_i \frac{P(w_i \; | \; s) }{P(w_i)},
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.7047em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.05em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;where &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents the &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6595em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;-th word in the text.&lt;/p&gt;
&lt;p&gt;Naive Bayes performs classifications by first computing &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;pos &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;x&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(\textrm{pos} \ | \ \mathrm{text})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;neg &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;x&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(\textrm{neg} \ | \ \mathrm{text})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. We can calculate the ratio of these two probabilities, known as the likelihood, to obtain&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;pos&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;x&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;neg&lt;/mtext&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;x&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;munder&gt;&lt;mo&gt;∏&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;menclose notation=&quot;updiagonalstrike&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/menclose&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;menclose notation=&quot;updiagonalstrike&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/menclose&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle \frac{P(\textrm{pos}\;| \; \mathrm{text})}{P(\textrm{neg}\;| \; \mathrm{text})} = \frac{P(\mathrm{pos})}{P(\mathrm{neg})} \prod_i \frac{P(w_i \; | \; \mathrm{pos})  \times \cancel{P(w_i)}}{P(w_i \; | \; \mathrm{neg})  \times \cancel{P(w_i)} }
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.363em;vertical-align:-0.936em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.7047em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.05em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord cancel-lap&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.75em&quot;&gt;&lt;span style=&quot;top:-3em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord cancel-pad&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;svg-align&quot; style=&quot;top:-2.75em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height:1em&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;100%&quot; height=&quot;1em&quot;&gt;&lt;line x1=&quot;0&quot; y1=&quot;100%&quot; x2=&quot;100%&quot; y2=&quot;0&quot; stroke-width=&quot;0.046em&quot;&gt;&lt;/line&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.25em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord cancel-lap&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.75em&quot;&gt;&lt;span style=&quot;top:-3em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord cancel-pad&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;svg-align&quot; style=&quot;top:-2.75em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span style=&quot;height:1em&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;100%&quot; height=&quot;1em&quot;&gt;&lt;line x1=&quot;0&quot; y1=&quot;100%&quot; x2=&quot;100%&quot; y2=&quot;0&quot; stroke-width=&quot;0.046em&quot;&gt;&lt;/line&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.25em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;If the likelihood is greater than 1, the model predicts a positive sentiment. But if the likelihood is less than 1, the model instead predicts a negative sentiment. One advantage of computing the likelihood function is that we can avoid calculating &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(w_i)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; as it occurs in the numerator and the denominator.&lt;/p&gt;
&lt;p&gt;The term &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;/&lt;/mi&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\mathrm{pos})/P(\mathrm{neg})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is known as a prior. It is relevant if there are more samples having one particular sentiment than the other. If the training set contains an equal number of positive and negative text samples, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.5&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\mathrm{pos}) = P(\mathrm{neg}) = 0.5&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.5&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and thus, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;/&lt;/mi&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\mathrm{pos})/P(\mathrm{neg}) = 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The conditional probability &lt;span&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(w \ | \ s)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt; that a word &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occurs in the presence of sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is given by the expression&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(w \;|\; s) = \frac{\mathrm{freq}(w, \; s)}{N_s}
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.263em;vertical-align:-0.836em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.109em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.836em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;The function &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle \mathrm{freq}(w, \ s)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents how often the word &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occurs with sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in the training set. Furthermore, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;N_s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.8333em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.109em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the total number of words in text samples that have sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;.&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s.&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;These are the building blocks needed to implement naive Bayes for sentiment classification. In the next section, I will describe how to implement a Naive Bayes classifier from scratch.&lt;/p&gt;
&lt;h2 id=&quot;implementing-naive-bayes&quot;&gt;Implementing Naive Bayes&lt;a href=&quot;#implementing-naive-bayes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let’s now implement this in some code. I’ll start by creating a small dataset:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;dataset &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;DataFrame&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;2020 was a fun year&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;Cats was a great movie&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;NLP is not fun&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;            &amp;quot;I hate tacos&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;        &amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                      &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                      &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                      &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The table below represents this dataset:&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;text&lt;/th&gt;&lt;th&gt;sentiment&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;0&lt;/th&gt;&lt;td&gt;2020 was a fun year&lt;/td&gt;&lt;td&gt;postive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;1&lt;/th&gt;&lt;td&gt;Cats was a great movie&lt;/td&gt;&lt;td&gt;postive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;2&lt;/th&gt;&lt;td&gt;NLP is not fun&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;3&lt;/th&gt;&lt;td&gt;I hate tacos&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Now, we need to determine how often each word in our dataset occurs for both sentiment labels. The function &lt;code class=&quot;language-text&quot;&gt;get_freqs&lt;/code&gt; defined below takes care of this. It uses the functions &lt;code class=&quot;language-text&quot;&gt;word_tokenize&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;FreqDist&lt;/code&gt; imported from the &lt;a href=&quot;https://www.nltk.org/&quot;&gt;NLTK&lt;/a&gt; library. The function &lt;code class=&quot;language-text&quot;&gt;word_tokenize&lt;/code&gt; splits a string into a list of words, also known as word tokens. Furthermore, the function &lt;code class=&quot;language-text&quot;&gt;FreqDist&lt;/code&gt; takes these word tokens to generate a dictionary showing how often each word occurs.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_tokenize&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; FreqDist&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset, sentiment):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    senti_dataset &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset.loc[dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    total_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; senti_dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to_list&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    total_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;total_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    words &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;total_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; FreqDist&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;words&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s how &lt;code class=&quot;language-text&quot;&gt;get_freqs&lt;/code&gt; works in practice. First, we can generate a frequency dictionary for the words in the text samples with a positive sentiment.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freqs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;FreqDist&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;was&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;a&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;2020&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;fun&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;year&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;Cats&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;great&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;movie&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similarly, we can compute a frequency dictionary for text samples with a negative sentiment.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; neg_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; neg_freqs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;FreqDist&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;NLP&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;is&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;not&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;fun&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;I&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;hate&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;tacos&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For convenience, let’s combine these dictionaries in a DataFrame. The function &lt;code class=&quot;language-text&quot;&gt;get_freq_table&lt;/code&gt; defined below achieves this. It first creates two dataframes using &lt;code class=&quot;language-text&quot;&gt;pos_freqs&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;neg_freqs&lt;/code&gt;, respectively. These are then merged using the &lt;code class=&quot;language-text&quot;&gt;&lt;a href=&quot;https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html&quot;&gt;DataFrame.merge&lt;/a&gt;&lt;/code&gt; function. In the final step, this implementation uses the &lt;code class=&quot;language-text&quot;&gt;&lt;a href=&quot;https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.fillna.html&quot;&gt;DataFrame.fillna&lt;/a&gt;&lt;/code&gt; function to replace any missing values with a &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.DataFrame.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_dict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; orient&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;index&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_freq_table.columns &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.DataFrame.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_dict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;neg_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; orient&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;index&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_freq_table.columns &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freq_table.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;merge&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;        neg_freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; how&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;outer&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; left_index&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; right_index&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;fillna&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can now compute a frequency table showing how often words in the training set occur with positive and negative sentiments.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;freq_table&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;positive&lt;/th&gt;&lt;th&gt;negative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;2020&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;Cats&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;I&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;NLP&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;a&lt;/th&gt;&lt;td&gt;2.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;fun&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;great&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;hate&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;is&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;movie&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;not&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;tacos&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;was&lt;/th&gt;&lt;td&gt;2.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;year&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Given this frequency table, we can now define a function &lt;code class=&quot;language-text&quot;&gt;prob&lt;/code&gt; to compute the conditional probability &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(w \ | \ s) = \mathrm{freq}(w, \ s)/N_s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.109em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; that a word &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occurs with a sentiment &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;s&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.4306em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(word, freq_table, sentiment):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    vocab &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table.index.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to_list&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; vocab:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        word_freq &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table.loc[word, sentiment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        word_freq &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    n_senti &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table[sentiment].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_freq&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;n_senti&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;s an example, we can use this function to compute the probability that the word “was” occurred given that the sentiment was positive. In this case, the probability is 20% since “was” occurred twice and the total number of words in the positive text samples is 10.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;was&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similarly, we can compute the probability that the word “was” occurred given that the sentiment was positive. The probability here is 0 because this word didn’t occur in the samples with a negative sentiment.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;was&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, let’s define a &lt;code class=&quot;language-text&quot;&gt;likelihood&lt;/code&gt; function that we can use to predict the sentiment of any given text:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; likelihood&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(text, freq_table, prior&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    word_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; prior&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_lst:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        pos_prob &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        neg_prob &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_prob&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;neg_prob&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; output&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the argument &lt;code class=&quot;language-text&quot;&gt;prior&lt;/code&gt; corresponds to the ratio &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;/&lt;/mi&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(\mathrm{pos})/P(\mathrm{neg})&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. If the training set contains more text samples having one sentiment than the other, this argument will have to be adjusted to account for this. We can compute the likelihood for the string &lt;code class=&quot;language-text&quot;&gt;“Cats was great”&lt;/code&gt; by running the following expression:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; likelihood&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Cats was great&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;RuntimeWarning&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;: divide by zero encountered &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; double_scalars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;  output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_prob&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;neg_prob&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Uh oh! What happened here? The problem with this implementation is that if &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(w_i \ | \ s) = 0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; for any word in the input text then &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;x&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(s \ | \ \mathrm{text}) = 0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;text&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. In this case, &lt;code class=&quot;language-text&quot;&gt;neg_prob&lt;/code&gt; is zero because none of the words in the string &lt;code class=&quot;language-text&quot;&gt;“Cats was great”&lt;/code&gt; ever occur with a negative sentiment in the training set. This can be addressed using a method known as Laplacian smoothing.&lt;/p&gt;
&lt;h3 id=&quot;laplacian-smoothing&quot;&gt;Laplacian Smoothing&lt;a href=&quot;#laplacian-smoothing&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When using Laplacian smoothing, the probability &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(w \ | \ s)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is revised to become&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;s&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mtext&gt;s&lt;/mtext&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mtext&gt;pos&lt;/mtext&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mtext&gt;neg&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;.&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle P(w \;|\; \textrm{s}) = \frac{\mathrm{freq}(w,\; \mathrm{s}) + 1}{N_\mathrm{s} + V} \quad \textrm{s} \in \{ \textrm{pos}, \;\textrm{neg} \}.
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.263em;vertical-align:-0.836em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.109em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathrm mtight&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.2222em&quot;&gt;V&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathrm&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.836em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:1em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;∈&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord textrm&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;In this expression, &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;V&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.2222em&quot;&gt;V&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents the number of unique words that occur in the training set. Now, if &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\mathrm{freq}(w, \ s) = 0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6444em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, then &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt; &lt;/mtext&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;/&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;P(w \ | \ s) = 1/(N_s + V)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1/&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.109em&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1514em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.109em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.2222em&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; instead of 0. Let’s now define a new probability function &lt;code class=&quot;language-text&quot;&gt;prob_lps&lt;/code&gt; to implement this:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(word, freq_table, sentiment):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    vocab &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table.index.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to_list&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    v &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;vocab&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; vocab:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        word_freq &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table.loc[word, sentiment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        word_freq &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    n_senti &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table[sentiment].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; (word_freq &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(n_senti &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; v)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s see how this works on the word “was.” Before, the probability was 20%. Now, it’s 12.5%.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;was&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.125&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For negative sentiments, we originally got a probability of 0. After including Laplacian smoothing, the probability is now about 4.76%.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;was&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.047619047619047616&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s now define a new likelihood function that uses Laplacian smoothing.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; likelihood_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(text, freq_table, prior&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    word_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; prior&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_lst:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        pos_prob &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        neg_prob &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_prob&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;neg_prob&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; output&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This time, we don’t run into any runtime errors when computing likelihoods&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; likelihood_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Cats was great&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;8.0390625&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How does the likelihood change if instead input the string &lt;code class=&quot;language-text&quot;&gt;“Cats was not great”&lt;/code&gt;?&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; likelihood_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Cats was not great&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;3.51708984375&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The likelihood drops by almost 50% in this case. Here, the model will still predict a positive sentiment. This is because the word “not” is the only word in the input text that appears with a negative sentiment in the training set.&lt;/p&gt;
&lt;h3 id=&quot;computing-the-log-likelihood&quot;&gt;Computing the Log-Likelihood&lt;a href=&quot;#computing-the-log-likelihood&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;It is best practice to compute log probabilities for numerical stability. As the number of words in the vocabulary &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;V&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6833em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.2222em&quot;&gt;V&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; increases, the probability of any word will become smaller and smaller.&lt;/p&gt;
&lt;p&gt;Thus, when computing the likelihood for a very long string of text, computing the product of small probabilities will yield a number so small there might not be enough numerical precision to properly represent this number accurately.&lt;/p&gt;
&lt;p&gt;This is known as &lt;a href=&quot;https://en.wikipedia.org/wiki/Arithmetic_underflow&quot;&gt;underflow&lt;/a&gt;. It represents one of the subtle nuances of performing arithmetic on decimal numbers using a computer. A more in-depth review of floating-point computations can be found in &lt;a href=&quot;https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html&quot;&gt;this article&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Computing the logarithm of probability will mitigate the presence of underflow errors. This is due to following the property of the logarithm function:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;munderover&gt;&lt;mo&gt;∏&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/munderover&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munderover&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/munderover&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;.&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle \log \left( \prod_{i}^{m}p_i \right)=  \sum_i^m \log(p_i).
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:3.0277em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;minner&quot;&gt;&lt;span class=&quot;mopen delimcenter&quot; style=&quot;top:0em&quot;&gt;&lt;span class=&quot;delimsizing size4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6514em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.3em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose delimcenter&quot; style=&quot;top:0em&quot;&gt;&lt;span class=&quot;delimsizing size4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.9291em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6514em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.3em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;In the event that we have have to multiply several small probabilities &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;p_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.625em;vertical-align:-0.1944em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, we can prevent underflow by taking the log of this product and instead computing the sums of the log probabilities &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\log(p_i)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:1em;vertical-align:-0.25em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:0em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Using this property, we can then define the log-likelhood as:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;true&quot;&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;munderover&gt;&lt;mo&gt;∏&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/munderover&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;munderover&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/munderover&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;p&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;∣&lt;/mi&gt;&lt;mtext&gt;  &lt;/mtext&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;.&lt;/mi&gt;&lt;/mstyle&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\displaystyle \log\left(\frac{P(\mathrm{pos})}{P(\mathrm{neg})} \times \prod_{i}^{m}\frac{P(w_i \;|\; \mathrm{pos})}{P(w_i \;|\; \mathrm{neg})} \right) = \log \frac{P(\mathrm{pos})}{P(\mathrm{neg})} + \sum_i^m \log \frac{P(w_i \;|\; \mathrm{pos})}{P(w_i \;|\; \mathrm{neg})}.
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:3.0277em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;minner&quot;&gt;&lt;span class=&quot;mopen delimcenter&quot; style=&quot;top:0em&quot;&gt;&lt;span class=&quot;delimsizing size4&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6514em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.3em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose delimcenter&quot; style=&quot;top:0em&quot;&gt;&lt;span class=&quot;delimsizing size4&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.363em;vertical-align:-0.936em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.9291em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.6514em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.3em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;lo&lt;span style=&quot;margin-right:0.0139em&quot;&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mopen nulldelimiter&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mfrac&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.427em&quot;&gt;&lt;span style=&quot;top:-2.314em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.23em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;frac-line&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.677em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.1389em&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;∣&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;pos&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.936em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose nulldelimiter&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;This is implemented in the function &lt;code class=&quot;language-text&quot;&gt;log_likelihood_lps&lt;/code&gt; shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; log_likelihood_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(text, freq_table, prior&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    word_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;prior&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_lst:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        numerator &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        denom &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; prob_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sentiment&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;numerator&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;denom&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; output&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, a positive sentiment is predicted when the likelihood is greater than zero. Otherwise, the model will predict a negative sentiment, as seen in the following examples:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; log_likelihood_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Cats are great&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.9857001832463227&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; log_likelihood_lps&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;I hate pineapple pizza&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;1.9204199316179813&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this post, I introduced Bayes’ theorem and showed how it’s used to build a simple classifier, known as naive Bayes, for sentiment analysis. I also showed how to implement Laplacian smoothing and compute log-likelihoods to make the classifier more numerically robust.&lt;/p&gt;</content:encoded></item><item><title>Visiting Chicago</title><link>https://bryananthonio.com/blog/my-first-visit-to-chicago/</link><guid isPermaLink="true">https://bryananthonio.com/blog/my-first-visit-to-chicago/</guid><description>Sharing some photos from my trip to the Windy City.</description><pubDate>Mon, 19 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Over the 4th of July weekend, I traveled to Chicago for my very first time. I had a lot of fun during my visit! This trip was also significant because I only recently started to ponder whether Chicago would be a place where I may want to live in the long-term future.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#arriving-in-chicago&quot;&gt;Arriving in Chicago&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#the-chicago-riverwalk&quot;&gt;The Chicago Riverwalk&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#admiring-the-architecture&quot;&gt;Admiring the Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#getting-around&quot;&gt;Getting Around&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#one-last-boat-tour&quot;&gt;One Last Boat Tour&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;arriving-in-chicago&quot;&gt;Arriving in Chicago&lt;a href=&quot;#arriving-in-chicago&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On Friday, July 2nd, I flew out of Houston early in the morning and arrived in Chicago at 8 am. From the airport, I took a taxi to my hotel by the lakefront.&lt;/p&gt;
&lt;p&gt;I promptly left my bags in my hotel room walked to the Navy Pier to do some sightseeing. The weather was windy and partly cloudy that morning. It also felt surreal to be in a city that I’ve never been to, especially since I’ve been stuck in Houston for the past year.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-chicago_skyline_2-lg_1PK4TY.jpg&quot; alt=&quot;A second view of the Chicago Skyline from the Navy Pier.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A second view of the Chicago Skyline from the Navy Pier.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;the-chicago-riverwalk&quot;&gt;The Chicago Riverwalk&lt;a href=&quot;#the-chicago-riverwalk&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One thing that captivated me was how life in the city revolves around the Chicago River. I noticed this after spending some time on the river walk. On both of the architecture tours that I attended, my tour guides emphasized this aspect of the city. The city has and &lt;a href=&quot;https://www.chicagotribune.com/news/breaking/ct-chicago-rivers-report-met-20160816-story.html&quot;&gt;continues to invest&lt;/a&gt; in making the river walk a place for people from all walks of life to gather.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-river_walk_1-lg_i6tLI.jpg&quot; alt=&quot;A view of boats in the Chicago riverwalk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-river_walk_2-lg_17a3mC.jpg&quot; alt=&quot;A view of boats in the Chicago riverwalk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-river_walk_3-lg_1VdBWw.jpg&quot; alt=&quot;A view of boats in the Chicago riverwalk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-river_walk_4-lg_Z2jTWgv.jpg&quot; alt=&quot;A view of boats in the Chicago riverwalk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-river_walk_5-lg_Z1uQnFB.jpg&quot; alt=&quot;A view of boats in the Chicago riverwalk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;/figure&gt;
&lt;h2 id=&quot;admiring-the-architecture&quot;&gt;Admiring the Architecture&lt;a href=&quot;#admiring-the-architecture&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The architecture of the city impressed me. In the architecture tours, I learned that the &lt;a href=&quot;https://en.wikipedia.org/wiki/Great_Chicago_Fire&quot;&gt;Great Chicago Fire&lt;/a&gt; in 1871, which destroyed many buildings, encouraged many architects to help with rebuilding the city. I was fascinated by the creativity in the design of some of the buildings. In particular, the &lt;a href=&quot;https://studiogang.com/project/aqua-tower&quot;&gt;Aqua tower&lt;/a&gt; has to be one of my most favorite buildings. I find the rippling facade of the building, combined with the spiral staircase at the bottom, to be innovative and unique.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--portrait&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-aqua_tower-lg_Z1dVjQY.jpg&quot; alt=&quot;The Aqua Tower.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;633&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A close-up of the Aqua Tower.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-spiral_staircase-lg_ZsVSlL.jpg&quot; alt=&quot;The spiral staircase at the bottom of the Aqua Tower.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The spiral staircase at the bottom of the Aqua Tower.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;getting-around&quot;&gt;Getting Around&lt;a href=&quot;#getting-around&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Despite how people say it’s easy to get around without a car, I, unfortunately, didn’t take advantage of the public transportation. Hence, I did a &lt;em&gt;lot&lt;/em&gt; of walking. My phone logged that I had walked 15 and 12 miles in my first and second days in the city, respectively. On the last two days of my visit, I decided to take more time to rest and not walk so much. I would typically hang out by the Bean or along the river walk to relax and people-watch from time to time.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-chicago_bean-lg_Z1rTMMa.jpg&quot; alt=&quot;The Chicago Bean.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The Chicago Bean.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-buckingham_fountain-lg_Z1kdTAn.jpg&quot; alt=&quot;The Buckingham Fountain.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The Buckingham Fountain.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h2 id=&quot;one-last-boat-tour&quot;&gt;One Last Boat Tour&lt;a href=&quot;#one-last-boat-tour&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;On July 3rd, I took another boat tour on the lakefront. Here, I got some views of the city skyline and the Navy Pier at sundown. There was supposed to be a &lt;a href=&quot;https://www.chicagotribune.com/entertainment/ct-ent-4th-july-fireworks-chicago-2021-20210702-cuma76vm5ffrbkwb7t66xyfyte-story.html&quot;&gt;“high-altitude” fireworks show&lt;/a&gt;. Unfortunately for me, I didn’t get to see it. It later became clear that the show wouldn’t be visible from my position on the lakefront, where I had eagerly awaited the start of the show. Alas, I hope I’ll be able to see a fireworks show in Chicago someday.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-navy_pier_1-lg_Z1sYpqV.jpg&quot; alt=&quot;The Navy Pier.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The Navy Pier.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-navy_pier_2-lg_ZDUPQ2.jpg&quot; alt=&quot;Boat ride at sundown&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Boat ride at sundown&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Overall, I had a good impression of Chicago. Right now, I’m curious about how the city changes when winter arrives. There are also other parts of the city that I wish I got to see. In particular, I wish I had taken the time to explore Chinatown. I also did not get to try Chicago-style pizza. Nonetheless, these are some of the experiences that I will look forward to on my next visit.&lt;/p&gt;
&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-china_town-lg_s0Y93.jpg&quot; alt=&quot;A view of Chinatown from the Chicago river.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A view of Chinatown from the Chicago river.&lt;/figcaption&gt;&lt;/figure&gt;</content:encoded></item><item><title>Building a Simple Sentiment Classifier</title><link>https://bryananthonio.com/blog/building-a-simple-sentiment-classifier/</link><guid isPermaLink="true">https://bryananthonio.com/blog/building-a-simple-sentiment-classifier/</guid><description>How to train a linear model to perform sentiment analysis.</description><pubDate>Sat, 13 Feb 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;How do customers feel about our product? What are the public sentiments regarding the latest news about COVID-19? How positive or negative are the prevailing impressions regarding a particular politician?&lt;/p&gt;
&lt;p&gt;Traditional approaches to answering these questions have involved conducting surveys and polls of relevant populations. Nowadays, these inquiries can also be addressed by examining online platforms where public sentiments are abundant.&lt;/p&gt;
&lt;p&gt;Analyzing public sentiments can be challenging due to the volume of data generated over time. On Twitter alone, users post &lt;a href=&quot;https://www.internetlivestats.com/one-second/#tweets-band&quot;&gt;thousands of tweets&lt;/a&gt; each second.&lt;/p&gt;
&lt;p&gt;Hence, it would be prohibitively time-consuming to inspect every tweet manually. This challenge has created a demand for methods, such as sentiment analysis algorithms, capable of mining these data in an automated fashion.&lt;/p&gt;
&lt;p&gt;Sentiment analysis elucidates the underlying emotions expressed in a given text. These can include positive, negative, or even neutral sentiments. In practice, it can be hard to train a model to identify emotions given their subjective nature.&lt;/p&gt;
&lt;p&gt;However, to meet this challenge, natural language processing techniques have been devised to extract numerical features from written text. These features are used to train machine learning models to identify sentiments.&lt;/p&gt;
&lt;p&gt;In this post, I will describe a simple way to train a binary classifier to perform sentiment analysis.&lt;/p&gt;
&lt;p&gt;I first learned this technique in the Natural Language Specialization course on &lt;a href=&quot;https://www.coursera.org/learn/classification-vector-spaces-in-nlp&quot;&gt;Coursera&lt;/a&gt;. I will test this approach on a dataset, found on &lt;a href=&quot;https://www.kaggle.com/bittlingmayer/amazonreviews&quot;&gt;Kaggle&lt;/a&gt;, containing customer reviews from Amazon. Here, I am going to outline the steps I took to prepare this data for machine learning.&lt;/p&gt;
&lt;p&gt;I will also examine the performance of a linear support vector machine (SVM) sentiment classifier. This model was trained to classify the sentiments of the reviews in this dataset and achieved a classification accuracy of 75% on my testing set.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#a-simple-technique-for-classifying-sentiment&quot;&gt;A Simple Technique For Classifying Sentiment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#performing-sentiment-analysis-on-a-dataset-of-amazon-product-reviews&quot;&gt;Performing Sentiment Analysis on a Dataset of Amazon Product Reviews&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#loading-the-dataset&quot;&gt;Loading the Dataset&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#preprocessing-the-inputs&quot;&gt;Preprocessing the Inputs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#training-a-linear-model-to-classify-sentiments&quot;&gt;Training a Linear Model to Classify Sentiments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#some-additional-considerations&quot;&gt;Some Additional Considerations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;a-simple-technique-for-classifying-sentiment&quot;&gt;A Simple Technique For Classifying Sentiment&lt;a href=&quot;#a-simple-technique-for-classifying-sentiment&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Carl Sagan noted that to make an apple pie from scratch, you must first invent the universe. Correspondingly, it goes without saying that to train a machine learning model you first need to acquire data, which unfortunately is not as impressive as inventing the universe.&lt;/p&gt;
&lt;p&gt;Start by preparing a dataset that contains text samples with corresponding labels denoting their sentiment. In my case, each customer review had either a positive or negative sentiment label.&lt;/p&gt;
&lt;p&gt;After preparing a dataset, partition the data into two parts: a training set used to train our model and a testing set used to evaluate our model’s performance. The main challenge now is to extract relevant features to train a sentiment classifier.&lt;/p&gt;
&lt;p&gt;Using the training set, we can construct two features. For a given sample of text, compute the sum of how often each word in the text appears in the text samples in the training set labeled as having a positive sentiment.&lt;/p&gt;
&lt;p&gt;This sum will serve as the first feature for our model. The second feature will be derived from a similar procedure. This time, compute the sum of how often each word in appears within the text samples in the training set that have a negative sentiment. The following expression defines these features:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;mtext&gt;pos_freq&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mtext&gt; , &lt;/mtext&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;g&lt;/mi&gt;&lt;mtext&gt;_&lt;/mtext&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;e&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\sum_{i} \text{pos\text{\textunderscore}freq}(w_i) \text{ , } \sum_{i} \mathrm{neg\text{\textunderscore}freq}(w_i) ,
&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:2.3277em;vertical-align:-1.2777em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.05em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;_&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt; , &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop op-limits&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.05em&quot;&gt;&lt;span style=&quot;top:-1.8723em;margin-left:0em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3.05em&quot;&gt;&lt;/span&gt;&lt;span&gt;&lt;span class=&quot;mop op-symbol large-op&quot;&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:1.2777em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot; style=&quot;margin-right:0.0139em&quot;&gt;neg&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;_&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord mathrm&quot;&gt;freq&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;where &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;w_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.5806em;vertical-align:-0.15em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0269em&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.3117em&quot;&gt;&lt;span style=&quot;top:-2.55em;margin-left:-0.0269em;margin-right:0.05em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;katex-sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mathnormal mtight&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.15em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents the &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;katex-base&quot;&gt;&lt;span class=&quot;katex-strut&quot; style=&quot;height:0.6595em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;-th word in the given sample text. The first component of this vector represents the sum of the positive frequencies of each word. The second component corresponds to the sum of the negative frequencies of each word.&lt;/p&gt;
&lt;p&gt;Let’s see how to implement this in some code. For this, I will use the &lt;a href=&quot;https://www.nltk.org/#natural-language-toolkit&quot;&gt;Natural Language Toolkit&lt;/a&gt; (NLTK), a well-established library for natural language processing. I’m going to import a few functions from this library:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;nltk.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;download&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;punkt&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk.tokenize &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_tokenize&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk.probability &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; FreqDist&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the function &lt;code class=&quot;language-text&quot;&gt;word_tokenize&lt;/code&gt; takes a string as input and generates a list of words in the given string. Each item in this list is known as a word token.&lt;/p&gt;
&lt;p&gt;Furthermore, the &lt;code class=&quot;language-text&quot;&gt;FreqDist&lt;/code&gt; function takes these word tokens and generates a dictionary where the keys consist of each unique token. The corresponding values are the frequencies of each token.&lt;/p&gt;
&lt;p&gt;Finally, the line containing &lt;code class=&quot;language-text&quot;&gt;nltk.download(‘punkt’)&lt;/code&gt; downloads the &lt;a href=&quot;http://www.nltk.org/api/nltk.tokenize.html?highlight=punkt#module-nltk.tokenize.punkt&quot;&gt;Punkt&lt;/a&gt; module which is the default tokenizer function used by &lt;code class=&quot;language-text&quot;&gt;word_tokenize&lt;/code&gt;. Here’s an example showing how these two functions can be used together:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;sample_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;the quick brown fox jumps over the lazy dog&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;FreqDist&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sample_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Running this code outputs the following dictionary showing how often each word occurs in &lt;code class=&quot;language-text&quot;&gt;sample_text&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;FreqDist({&amp;#39;the&amp;#39;: 2, &amp;#39;quick&amp;#39;: 1, &amp;#39;brown&amp;#39;: 1, &amp;#39;fox&amp;#39;: 1,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;		  &amp;#39;jumps&amp;#39;: 1, &amp;#39;over&amp;#39;: 1, &amp;#39;lazy&amp;#39;: 1, &amp;#39;dog&amp;#39;: 1})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This example illustrates one of the key steps needed to construct numerical features for sentiment analysis. Using the training set, we will have to perform this process to obtain two dictionaries showing how often a word occurs in the positive reviews and the negative reviews, respectively.&lt;/p&gt;
&lt;p&gt;I’ve defined the function &lt;code class=&quot;language-text&quot;&gt;get_freqdists&lt;/code&gt; to generate these two dictionaries containing the positive frequencies &lt;code class=&quot;language-text&quot;&gt;pos_freqs&lt;/code&gt;, and the negative frequencies &lt;code class=&quot;language-text&quot;&gt;neg_freqs&lt;/code&gt;. The input for this function is assumed to be a &lt;a href=&quot;https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html&quot;&gt;DataFrame&lt;/a&gt; with two columns. These include &lt;code class=&quot;language-text&quot;&gt;review_text&lt;/code&gt; which contains the text from each review, and &lt;code class=&quot;language-text&quot;&gt;sentiment&lt;/code&gt; which contains corresponding sentiment labels for each review.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqdists&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        total_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to_list&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        total_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;total_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        word_tokens &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;total_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; FreqDist&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word_tokens&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_dataset &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset.loc[dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_dataset &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset.loc[dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;neg_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freqs, neg_freqs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Once we have the dictionaries containing the positive and negative frequencies of the words in our training set, we can write another function to generate the features for a given text sample.&lt;/p&gt;
&lt;p&gt;I have defined the function &lt;code class=&quot;language-text&quot;&gt;get_features&lt;/code&gt; for this purpose. Using the &lt;code class=&quot;language-text&quot;&gt;input_text&lt;/code&gt;, it computes a feature vector containing the sums of the positive and negative frequencies of the words contained in the text.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_features&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(input_text, pos_freqs, neg_freqs):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_count &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_count &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    word_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;input_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_lst:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freqs:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            pos_count &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freqs[word]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; neg_freqs:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            neg_count &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; neg_freqs[word]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [pos_count, neg_count]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I will demonstrate how these functions work in the following example. Let’s start by defining a small dataset containing customer product reviews:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;dataset &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;DataFrame&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Best product ever!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                                        &amp;quot;Much excite. Such wow!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                                        &amp;quot;Not happy with this product.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                                        &amp;quot;Very bad, this product was.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                       &amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                                     &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                                     &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;                                     &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;dataset&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This dataset is represented by the table below:&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;review_text&lt;/th&gt;&lt;th&gt;sentiment&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;0&lt;/th&gt;&lt;td&gt;Best product ever!&lt;/td&gt;&lt;td&gt;postive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;1&lt;/th&gt;&lt;td&gt;Much excite. Such wow! &lt;/td&gt;&lt;td&gt;postive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;2&lt;/th&gt;&lt;td&gt;Not happy with this product.&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;3&lt;/th&gt;&lt;td&gt;Very bad, this product was.&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Before we begin, we will have to pre-process this text. The steps for this are:&lt;/p&gt;
&lt;ol&gt;&lt;li&gt;Remove all punctuation marks and stop words.&lt;/li&gt;&lt;li&gt;Convert all characters to lowercase.&lt;/li&gt;&lt;li&gt;Stem each word&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;We remove punctuation marks as these don’t directly correlate with the sentiment of a text. It is also typically recommended to remove stop words for this same reason. These include words such as “is”, “was”, “of”, “but”, “very”, and many others.&lt;/p&gt;
&lt;p&gt;Next, we lowercase each word so that capitalized words aren’t treated as being distinct from their lowercase counterparts.&lt;/p&gt;
&lt;p&gt;Finally, we convert each word to its corresponding stem, a process known as stemming. For example, the words “going”, “goes”, and “gone” are derived from the stem word “go.” Stemming ensures that we don’t count each of these words separately as they all carry the same lexical meaning.&lt;/p&gt;
&lt;p&gt;The function &lt;code class=&quot;language-text&quot;&gt;clean_text&lt;/code&gt; defined below implements these pre-processing steps. Here, I’ve imported some additional definitions from the &lt;code class=&quot;language-text&quot;&gt;nltk&lt;/code&gt; library. These include the &lt;code class=&quot;language-text&quot;&gt;&lt;a href=&quot;https://www.nltk.org/api/nltk.stem.html#module-nltk.stem.porter&quot;&gt;PorterStemmer&lt;/a&gt;&lt;/code&gt; class used for stemming each word, and &lt;code class=&quot;language-text&quot;&gt;stopwords&lt;/code&gt; which is a list of stop words. I also imported the &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; library as &lt;code class=&quot;language-text&quot;&gt;string.puncutation&lt;/code&gt; contains the punctuation marks we will need to remove from each text sample.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;nltk.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;download&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;stopwords&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk.stem &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; PorterStemmer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; nltk.corpus &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; stopwords&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; clean_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(text):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;maketrans&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;punctuation&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    new_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; text.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;translate&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    new_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; new_text.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lower&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    word_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; word_tokenize&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;new_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    output &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    stemmer &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; PorterStemmer&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word_lst:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; stopwords.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;words&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;english&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;            continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;        else&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            word &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; stemmer.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;stem&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;            output.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;word&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;join&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;output&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can clean each of the text entries by executing the following:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;dataset.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;apply&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;    lambda&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; x:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; clean_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;dataset&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After running this, the dataset now looks like:&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;review_text&lt;/th&gt;&lt;th&gt;sentiment&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;0&lt;/th&gt;&lt;td&gt;best product ever&lt;/td&gt;&lt;td&gt;postive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;1&lt;/th&gt;&lt;td&gt;much excit wow &lt;/td&gt;&lt;td&gt;postive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;2&lt;/th&gt;&lt;td&gt;happi product&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;3&lt;/th&gt;&lt;td&gt;bad product&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;There are a few things to note about the processed text.&lt;/p&gt;
&lt;p&gt;For instance, the words “happy” and “excite” have been modified to become “happi” and “excit,” respectively. These are the word stems identified by the PorterStemmer algorithm.&lt;/p&gt;
&lt;p&gt;Also, some of the original words in the reviews are missing because stop words were removed. In particular, the first negative review that was “not happy with this product” became “happi product.” Here, “not” is a stop word that negated the word “happy,” giving this review a negative sentiment.&lt;/p&gt;
&lt;p&gt;Because this stop word was deleted, the review now appears to have a positive sentiment. Nuances such as this represent one of the shortcomings of this approach as it ignores negations that are crucial to identifying the conveyed sentiment.&lt;/p&gt;
&lt;p&gt;Now, we can construct frequency dictionaries for the positive and negative reviews using the &lt;code class=&quot;language-text&quot;&gt;get_freqdists&lt;/code&gt; function:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;pos_freqs, neg_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqdists&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;pos_freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.DataFrame.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_dict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; orient&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;index&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;pos_freq_table.columns &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pos_freqs&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;neg_freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.DataFrame.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;from_dict&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;neg_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; orient&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;index&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;neg_freq_table.columns &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;neg_freqs&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_freq_table.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;merge&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    neg_freq_table&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; how&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;outer&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; left_index&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; right_index&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;freq_table &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; freq_table.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;fillna&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;freq_table&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The frequency dictionaries are represented in &lt;code class=&quot;language-text&quot;&gt;freq_table&lt;/code&gt;, shown in the table below:&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;pos_freqs&lt;/th&gt;&lt;th&gt;neg_freqs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;bad&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;best&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;ever&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;excit&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;happi&lt;/th&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;much&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;product&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;2.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;wow&lt;/th&gt;&lt;td&gt;1.0&lt;/td&gt;&lt;td&gt;0.0&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;This table lists each unique word in the dataset. It also shows how often each word appears in the positive reviews and the negative reviews. These are denoted as &lt;code class=&quot;language-text&quot;&gt;pos_freqs&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;neg_freqs&lt;/code&gt;, respectively. For instance, the word “product” appeared twice in the negative reviews and only appeared once in the positive reviews.&lt;/p&gt;
&lt;p&gt;Now, we can use the &lt;code class=&quot;language-text&quot;&gt;get_features&lt;/code&gt; function to derive a feature vector for any given review.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;very good product love it&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; clean_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;text, &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get_features&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; pos_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; neg_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(&amp;#39;good product love&amp;#39;, [1, 2])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This code shows the cleaned text along with its feature vector. The words “very” and “it” were removed because they are stop words. Furthermore, the word “product” is the only word in this review that appears in either the positive or negative reviews in the training set.&lt;/p&gt;
&lt;p&gt;This word had a frequency of 1 in the positive reviews and a frequency of 2 in the negative reviews. That explains why its feature vector has these frequencies as its components.&lt;/p&gt;
&lt;p&gt;In summary, construct numerical features for sentiment analysis using the method I’ve described involves the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Pre-process the text for each review&lt;/li&gt;
&lt;li&gt;Create positive and negative frequency dictionaries using reviews from the training set&lt;/li&gt;
&lt;li&gt;For a given review, sum over the positive frequencies of each word contained in the review. Then, compute a second sum over the negative frequencies of each word.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;performing-sentiment-analysis-on-a-dataset-of-amazon-product-reviews&quot;&gt;Performing Sentiment Analysis on a Dataset of Amazon Product Reviews&lt;a href=&quot;#performing-sentiment-analysis-on-a-dataset-of-amazon-product-reviews&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When I first learned about this approach to sentiment analysis, I was curious about how well it would work in practice. To address this curiosity, I found a dataset on &lt;a href=&quot;https://www.kaggle.com/bittlingmayer/amazonreviews&quot;&gt;Kaggle&lt;/a&gt; that contains a few million Amazon customer reviews.&lt;/p&gt;
&lt;p&gt;This dataset includes two files: &lt;code class=&quot;language-text&quot;&gt;train.ft.txt&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;test.ft.txt&lt;/code&gt; that correspond to the training and testing sets, respectively. Within these text files, each sample has the following format:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;__label__[X] [review text goes here]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In other words, each product review is preceded by a class label. This label could either be &lt;code class=&quot;language-text&quot;&gt;__label__1&lt;/code&gt; that represents 1- and 2-star reviews, or &lt;code class=&quot;language-text&quot;&gt;__label__2&lt;/code&gt; that represents 4- and 5-star reviews.&lt;/p&gt;
&lt;p&gt;According to the documentation, the dataset doesn’t include 3-star reviews. One additional detail is that some of the reviews are written in other languages such as Spanish, although most are in English.&lt;/p&gt;
&lt;h3 id=&quot;loading-the-dataset&quot;&gt;Loading the Dataset&lt;a href=&quot;#loading-the-dataset&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I began by opening the text file containing the training data. Because this file contains millions of reviews, I loaded 20 samples to briefly examine some of these.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;f &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;./train.ft.txt&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;r&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;num_samples &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lines_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;num_samples&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    lines_lst.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;readline&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lines_lst&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Executing the code above reads the file &lt;code class=&quot;language-text&quot;&gt;train.ft.txt&lt;/code&gt; and prints out the first 4 reviews which are shown below:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;#39;__label__2 Stuning even for the non-gamer: This sound track was beautiful! It paints the senery in your mind so well I would recomend it even to people who hate vid. game music! I have played the game Chrono Cross but out of all of the games I have ever played it has the best music! It backs away from crude keyboarding and takes a fresher step with grate guitars and soulful orchestras. It would impress anyone who cares to listen! ^_^\n&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;quot;__label__2 The best soundtrack ever to anything.: I&amp;#39;m reading a lot of reviews saying that this is the best &amp;#39;game soundtrack&amp;#39; and I figured that I&amp;#39;d write a review to disagree a bit. This in my opinino is Yasunori Mitsuda&amp;#39;s ultimate masterpiece. The music is timeless and I&amp;#39;m been listening to it for years now and its beauty simply refuses to fade.The price tag on this is pretty staggering I must say, but if you are going to buy any cd for this much money, this is the only one that I feel would be worth every penny.\n&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;__label__2 Amazing!: This soundtrack is my favorite music of all time, hands down. The intense sadness of &amp;quot;Prisoners of Fate&amp;quot; (which means all the more if you&amp;#39;ve played the game) and the hope in &amp;quot;A Distant Promise&amp;quot; and &amp;quot;Girl who Stole the Star&amp;quot; have been an important inspiration to me personally throughout my teen years. The higher energy tracks like &amp;quot;Chrono Cross ~ Time&amp;#39;s Scar~&amp;quot;, &amp;quot;Time of the Dreamwatch&amp;quot;, and &amp;quot;Chronomantique&amp;quot; (indefinably remeniscent of Chrono Trigger) are all absolutely superb as well.This soundtrack is amazing music, probably the best of this composer&amp;#39;s work (I haven&amp;#39;t heard the Xenogears soundtrack, so I can&amp;#39;t say for sure), and even if you&amp;#39;ve never played the game, it would be worth twice the price to buy it.I wish I could give it 6 stars.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;__label__2 Excellent Soundtrack: I truly like this soundtrack and I enjoy video game music. I have played this game and most of the music on here I enjoy and it&amp;#39;s truly relaxing and peaceful.On disk one. my favorites are Scars Of Time, Between Life and Death, Forest Of Illusion, Fortress of Ancient Dragons, Lost Fragment, and Drowned Valley.Disk Two: The Draggons, Galdorb - Home, Chronomantique, Prisoners of Fate, Gale, and my girlfriend likes ZelbessDisk Three: The best of the three. Garden Of God, Chronopolis, Fates, Jellyfish sea, Burning Orphange, Dragon&amp;#39;s Prayer, Tower Of Stars, Dragon God, and Radical Dreamers - Unstealable Jewel.Overall, this is a excellent soundtrack and should be brought by those that like video game music.Xander Cross&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Most of the initial reviews were for a soundtrack for the game Chrono Cross. I’ve never listened to this soundtrack before or played Chrono Cross but I must say I was intrigued by these reviews!&lt;/p&gt;
&lt;p&gt;After glancing through the reviews, I could tell that this dataset would need some cleaning. Some reviews had misspelled words.&lt;/p&gt;
&lt;p&gt;Here, I thought about whether to correct spelling mistakes although I opted not to implement this in the end. I also noticed that sometimes there wasn’t whitespace after certain punctuation marks like periods.&lt;/p&gt;
&lt;p&gt;This was important to note since my &lt;code class=&quot;language-text&quot;&gt;clean_text&lt;/code&gt; function defined earlier would convert a string such as &lt;code class=&quot;language-text&quot;&gt;“two.words”&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;twowords&lt;/code&gt; after removing punctuation marks. Here, I implicitly assumed that there would always be whitespace before or after each punctuation mark.&lt;/p&gt;
&lt;h3 id=&quot;preprocessing-the-inputs&quot;&gt;Preprocessing the Inputs&lt;a href=&quot;#preprocessing-the-inputs&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I wrote two functions to obtain the sentiment labels and the corresponding text from the file containing the data.&lt;/p&gt;
&lt;p&gt;Here, I noticed that the 10th character, indexed as &lt;code class=&quot;language-text&quot;&gt;line[9]&lt;/code&gt;, was the sentiment label that could be &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt; (1- and 2-star reviews) or &lt;code class=&quot;language-text&quot;&gt;2&lt;/code&gt; (4- and 5-star reviews).&lt;/p&gt;
&lt;p&gt;Furthermore, the 11th character was always whitespace. The characters after this were just the review text.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_labels&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(lines_lst):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [line[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; line &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; lines_lst]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_review_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(lines_lst):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [line[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; line &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; lines_lst]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I used the &lt;code class=&quot;language-text&quot;&gt;load_dataset&lt;/code&gt; function shown below to load a specified number of reviews from a given input file. Here, I modified the sentiment labels &lt;code class=&quot;language-text&quot;&gt;1&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;2&lt;/code&gt; to become &lt;code class=&quot;language-text&quot;&gt;negative&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;positive&lt;/code&gt;, respectively.&lt;/p&gt;
&lt;p&gt;I also added a regular expression to add a space after periods and commas. This procedure ensured that words separated by these punctuation marks wouldn’t become merged after cleaning the text.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; re&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(input_file, num_samples):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    f &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;input_file&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;r&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    lines_lst &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;num_samples&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        lines_lst.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;readline&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    labels &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_labels&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lines_lst&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    review_text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_review_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lines_lst&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    dataset &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;DataFrame&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; review_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;labels&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    dataset.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        {&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;2&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    dataset.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;        lambda&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; x:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; re&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sub&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;?&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;.,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])(&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;?=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;^\s&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    dataset.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;        lambda&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; x:&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; clean_text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I then defined another function &lt;code class=&quot;language-text&quot;&gt;get_ml_dataset&lt;/code&gt; to prepare the dataset for machine learning. This function takes the DataFrame &lt;code class=&quot;language-text&quot;&gt;dataset&lt;/code&gt; of the labeled reviews. It generates the matrix &lt;code class=&quot;language-text&quot;&gt;X&lt;/code&gt; containing the features of each review and the vector &lt;code class=&quot;language-text&quot;&gt;y&lt;/code&gt; that contains the corresponding sentiment labels for each review.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_ml_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset, pos_freqs, neg_freqs):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;review_text&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to_list&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    X &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get_features&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; pos_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; neg_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; text &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; train_text]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    X &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; np.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    y &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; dataset[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;to_list&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; X, y&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, I loaded the first 5000 reviews in the file containing the training data.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;train_data_file &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;./train.ft.txt&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;train_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;train_data_file&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 5000&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;train_data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These are some of the reviews in the training set:&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;review_text&lt;/th&gt;&lt;th&gt;sentiment&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;0&lt;/th&gt;&lt;td&gt;stune even nongam sound track beauti paint sen…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;1&lt;/th&gt;&lt;td&gt;best soundtrack ever anyth im read lot review …&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;2&lt;/th&gt;&lt;td&gt;amaz soundtrack favorit music time hand intens…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;3&lt;/th&gt;&lt;td&gt;excel soundtrack truli like soundtrack enjoy v…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4&lt;/th&gt;&lt;td&gt;rememb pull jaw floor hear youv play game know…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;…&lt;/th&gt;&lt;td&gt;…&lt;/td&gt;&lt;td&gt;…&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4995&lt;/th&gt;&lt;td&gt;must read anyon interest true crime rivet last…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4996&lt;/th&gt;&lt;td&gt;sheer mad meticul document ye review correct e…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4997&lt;/th&gt;&lt;td&gt;weirdest plot ever true rememb last year aroun…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4998&lt;/th&gt;&lt;td&gt;excit book ive ever read love thought book mag…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4999&lt;/th&gt;&lt;td&gt;book scare hell vincent bugliosi isnt kid writ…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;caption&gt;5000 rows × 2 columns&lt;/caption&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Then, I loaded some of reviews from the testing set.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;test_data_file &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;./test.ft.txt&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;test_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; load_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;test_data_file&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These are some of the reviews in the testing set:&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot;&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;review_text&lt;/th&gt;&lt;th&gt;sentiment&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;0&lt;/th&gt;&lt;td&gt;great cd love pat great voic gener listen cd y…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;1&lt;/th&gt;&lt;td&gt;best game music soundtrack game didnt realli p…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;2&lt;/th&gt;&lt;td&gt;batteri die within year bought charger jul 200…&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;3&lt;/th&gt;&lt;td&gt;work fine maha energi better check maha energi…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;4&lt;/th&gt;&lt;td&gt;great nonaudiophil review quit bit combo playe…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;…&lt;/th&gt;&lt;td&gt;…&lt;/td&gt;&lt;td&gt;…&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;995&lt;/th&gt;&lt;td&gt;borinmg dumb wast time glori old time movi tri…&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;996&lt;/th&gt;&lt;td&gt;best film year best film ever made god monster…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;997&lt;/th&gt;&lt;td&gt;see movi ian mckellen perform god monster supe…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;998&lt;/th&gt;&lt;td&gt;best screenplay stabil recent film anticip goo…&lt;/td&gt;&lt;td&gt;positive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;999&lt;/th&gt;&lt;td&gt;tree arriv bent poorli pack manufactur pack pr…&lt;/td&gt;&lt;td&gt;negative&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;caption&gt;1000 rows × 2 columns&lt;/caption&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;As a quick check, I wanted to see whether the classes in the training set were balanced so I wrote the following function for this purpose. Compared to the testing set, the training set was very imbalanced. It had 2308 positive reviews and 2692 negative reviews.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_label_count&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    num_pos &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;loc&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    num_neg &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;loc&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; num_pos, num_neg&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get_label_count&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;train_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;get_label_count&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;test_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2308&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;2692&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;502&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;498&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To address this, I wrote the following function to create a training set with equal numbers of positive and negative reviews:&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; balance_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dataset):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    pos_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; train_data.loc[train_data[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;    neg_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; train_data.loc[train_data[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;neg_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        pos_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pos_data.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sample&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;neg_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    elif&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;neg_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;        neg_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; neg_data.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;sample&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;concat&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;pos_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; neg_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I now created a new balanced dataset &lt;code class=&quot;language-text&quot;&gt;bal_train_data&lt;/code&gt; and used this to generate the positive and negative frequency dictionaries.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;bal_train_data &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; balance_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;train_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;pos_freqs, neg_freqs &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_freqdists&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;bal_train_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;training-a-linear-model-to-classify-sentiments&quot;&gt;Training a Linear Model to Classify Sentiments&lt;a href=&quot;#training-a-linear-model-to-classify-sentiments&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Now for the fun part! I first generated the matrices containing features for the training set and testing set along with vectors containing the sentiment labels for each review. I also used the &lt;code class=&quot;language-text&quot;&gt;&lt;a href=&quot;https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html&quot;&gt;StandardScaler&lt;/a&gt;&lt;/code&gt; class from the scikit-learn library to standardize each feature.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sklearn.preprocessing &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; StandardScaler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;scaler &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; StandardScaler&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;X_train, y_train &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_ml_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;bal_train_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; pos_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; neg_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;X_train &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; scaler.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;fit_transform&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;X_test, y_test &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; get_ml_dataset&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;test_data&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; pos_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; neg_freqs&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;X_test &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; scaler.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;transform&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_test&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, I plotted the features of each review in the training set. For this purpose, I used the library &lt;a href=&quot;https://seaborn.pydata.org/&quot;&gt;seaborn&lt;/a&gt;. The plotting functions defined in this library only take Dataframes. Hence, I created a new DataFrame containing the features from the training set reviews.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; seaborn &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;ml_train_data_df &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;DataFrame&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(data&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; columns&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pos_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;neg_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;ml_train_data_df.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; y_train&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;ml_train_data_df&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sns.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;jointplot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(data&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ml_train_data_df&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              x&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pos_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              y&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;neg_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              hue&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              palette&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;blue&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;red&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg.fig.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;suptitle&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Training Data&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; size&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg.fig.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;tight_layout&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg.fig.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;subplots_adjust&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(top&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After creating the plot shown below, I noted that the two features were correlated. This finding suggested that many words in the frequency dictionaries occurred at equal frequencies in the positive and in the negative reviews. It was also clear that it would be challenging to discriminate between positive and negative reviews using these features.&lt;/p&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/joint_plot_train_data_2e6VaN.svg&quot; alt=&quot;plot of the training data in feature space&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;576&quot; height=&quot;576&quot; class=&quot;plot-figure&quot;&gt;
&lt;p&gt;I also plotted the testing set. This plot bore some similarities with that of the training set. In particular, the same correlation between &lt;code class=&quot;language-text&quot;&gt;pos_freq&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;neg_freq&lt;/code&gt; was present.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;ml_test_data_df &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; pd.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;DataFrame&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(data&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_test&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; columns&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pos_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;neg_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;ml_test_data_df.loc[:, &lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; y_test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sns.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;jointplot&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(data&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;ml_test_data_df&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              x&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;pos_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              y&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;neg_freq&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              hue&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;sentiment&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;              palette&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;positive&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;blue&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;negative&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt; &amp;quot;red&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg.fig.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;suptitle&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Testing Data&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; size&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg.fig.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;tight_layout&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;jg.fig.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;subplots_adjust&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(top&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.9&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/joint_plot_test_data_1mtDr8.svg&quot; alt=&quot;plot of the testing data in feature space&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;576&quot; height=&quot;576&quot; class=&quot;plot-figure&quot;&gt;
&lt;p&gt;Next, I fitted a linear SVM classifier on the training dataset and scored its performance. The SVM achieved a classification accuracy of 77.9% on the training data.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sklearn.svm &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; LinearSVC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lin_svm &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; LinearSVC&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(dual&lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lin_svm.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;fit&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; y_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lin_svm.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;score&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; y_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#8695B7;font-style:italic&quot;&gt;#0.7796793760831889&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The real test now was to see how the model would perform on the testing set. Here, I observed a classification accuracy of 75% shown below.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;lin_svm.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;score&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;X_test&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; y_test&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#C3A6FF&quot;&gt;0.75&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I plotted two confusion matrices using the training and testing sets to examine the classification performance further.&lt;/p&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; sklearn.metrics &lt;/span&gt;&lt;span style=&quot;color:#FFAE57&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt; plot_confusion_matrix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;plot_confusion_matrix&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lin_svm&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; X_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; y_train&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;plt.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Confusion Matrix: Training Data&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/conf_mat_train_data_w8X0P.svg&quot; alt=&quot;confusion matrix plot of the model&apos;s predictions on the training data&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;576&quot; height=&quot;384&quot; class=&quot;plot-figure&quot;&gt;
&lt;div class=&quot;code-block-wrapper&quot;&gt;&lt;div class=&quot;code-header&quot;&gt;&lt;button class=&quot;copy&quot; aria-label=&quot;Copy code to clipboard&quot; title=&quot;Copy code to clipboard&quot; type=&quot;button&quot; onclick=&quot;
          navigator.clipboard.writeText(this.parentElement.nextElementSibling.querySelector(&apos;code&apos;).textContent);
          this.classList.add(&apos;copied&apos;);
          this.setAttribute(&apos;aria-label&apos;, &apos;Code copied to clipboard&apos;);
          this.title = &apos;Code copied to clipboard&apos;;
          setTimeout(() =&gt; {
            this.classList.remove(&apos;copied&apos;);
            this.setAttribute(&apos;aria-label&apos;, &apos;Copy code to clipboard&apos;);
            this.title = &apos;Copy code to clipboard&apos;;
          }, 2000);
        &quot;&gt;&lt;span class=&quot;ready&quot; aria-hidden=&quot;false&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M9 15C9 12.1716 9 10.7574 9.87868 9.87868C10.7574 9 12.1716 9 15 9L16 9C18.8284 9 20.2426 9 21.1213 9.87868C22 10.7574 22 12.1716 22 15V16C22 18.8284 22 20.2426 21.1213 21.1213C20.2426 22 18.8284 22 16 22H15C12.1716 22 10.7574 22 9.87868 21.1213C9 20.2426 9 18.8284 9 16L9 15Z&quot;&gt;&lt;/path&gt;&lt;path d=&quot;M16.9999 9C16.9975 6.04291 16.9528 4.51121 16.092 3.46243C15.9258 3.25989 15.7401 3.07418 15.5376 2.90796C14.4312 2 12.7875 2 9.5 2C6.21252 2 4.56878 2 3.46243 2.90796C3.25989 3.07417 3.07418 3.25989 2.90796 3.46243C2 4.56878 2 6.21252 2 9.5C2 12.7875 2 14.4312 2.90796 15.5376C3.07417 15.7401 3.25989 15.9258 3.46243 16.092C4.51121 16.9528 6.04291 16.9975 9 16.9999&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copy code&lt;/span&gt;&lt;span class=&quot;success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;32&quot; height=&quot;32&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.5&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; aria-hidden=&quot;true&quot;&gt;&lt;path d=&quot;M5 14L8.5 17.5L19 6.5&quot;&gt;&lt;/path&gt;&lt;/svg&gt;Copied&lt;/span&gt;&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;astro-code Halcyon&quot; style=&quot;background-color:#1d2433;color:#a2aabc;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;plot_confusion_matrix&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;lin_svm&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; X_test&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt; y_test&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;plt.&lt;/span&gt;&lt;span style=&quot;color:#FFD580&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;Confusion Matrix: Testing Data&lt;/span&gt;&lt;span style=&quot;color:#5CCFE6&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#BAE67E&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#A2AABC&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;img src=&quot;https://bryananthonio.com/_astro/conf_mat_test_data_4UTf.svg&quot; alt=&quot;confusion matrix plot of the model&apos;s predictions on the testing data&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;576&quot; height=&quot;384&quot; class=&quot;plot-figure&quot;&gt;
&lt;p&gt;The confusion matrices suggested that the model wasn’t biased toward predicting one sentiment more frequently than the other. Furthermore, the small gap between the model’s performance on the training and testing sets suggests that model isn’t overfitting to the training data.&lt;/p&gt;
&lt;h2 id=&quot;some-additional-considerations&quot;&gt;Some Additional Considerations&lt;a href=&quot;#some-additional-considerations&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There are more things I could have done to clean the data further.&lt;/p&gt;
&lt;p&gt;In cleaning the data, I didn’t correctly handle contracted words. Based on how I processed the text, the string &lt;code class=&quot;language-text&quot;&gt;“I’d”&lt;/code&gt; would become &lt;code class=&quot;language-text&quot;&gt;“id”&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I also noticed spelling mistakes in some of the reviews. For this, I initially used the library &lt;code class=&quot;language-text&quot;&gt;&lt;a href=&quot;https://github.com/barrust/pyspellchecker&quot;&gt;pyspellchecker&lt;/a&gt;&lt;/code&gt; to correct spelling mistakes. But I later decided not to use it as it added a significant amount of time to the pre-processing step.&lt;/p&gt;
&lt;p&gt;I also didn’t take into account that some reviews weren’t in English. Here, I could have used the library &lt;code class=&quot;language-text&quot;&gt;&lt;a href=&quot;https://polyglot.readthedocs.io/en/latest/index.html&quot;&gt;polyglot&lt;/a&gt;&lt;/code&gt; to detect the language of each review. This would ensure that my training and testing sets wouldn’t have reviews written in other languages.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this post, I described a simple method to generate two numerical features for sentiment analysis. This method requires a training set containing text samples labeled as having either a positive or negative sentiment.&lt;/p&gt;
&lt;p&gt;For any text sample, the first feature represents the total sum of how often each word in the text occurs in the training set samples labeled as positive. The second feature represents the sum of the frequencies that each word occurs in training set samples labeled as negative.&lt;/p&gt;
&lt;p&gt;I demonstrated this approach using a dataset of Amazon customer reviews. Here, I trained a linear SVM sentiment classifier that achieved a prediction accuracy of 75% on my testing set.&lt;/p&gt;</content:encoded></item><item><title>2020: My Year in Review</title><link>https://bryananthonio.com/blog/2020-in-review/</link><guid isPermaLink="true">https://bryananthonio.com/blog/2020-in-review/</guid><description>Reviewing my personal highlights from 2020 and my plans for 2021.</description><pubDate>Sat, 16 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Even though 2020 had its fair share of hardships, I still found some highlights of the year for which I am grateful. I’ve also looked to the future to begin setting goals for the coming year.&lt;/p&gt;
&lt;h2 id=&quot;contents&quot;&gt;Contents&lt;a href=&quot;#contents&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#pivoting-to-applied-machine-learning-research&quot;&gt;Pivoting to Applied Machine Learning Research&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#exploring-new-directions-in-my-photography&quot;&gt;Exploring New Directions in My Photography&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#creating-this-website&quot;&gt;Creating This Website&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#reuniting-with-my-extended-family&quot;&gt;Reuniting with My Extended Family&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#goals-for-2021&quot;&gt;Goals for 2021&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#deep-learning&quot;&gt;Deep Learning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#blogging&quot;&gt;Blogging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#reading&quot;&gt;Reading&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#photography&quot;&gt;Photography&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#social&quot;&gt;Social&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;pivoting-to-applied-machine-learning-research&quot;&gt;Pivoting to Applied Machine Learning Research&lt;a href=&quot;#pivoting-to-applied-machine-learning-research&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I began 2020 by finishing my master’s thesis, which I defended in late January. From this moment, I became a Ph.D. candidate.&lt;/p&gt;
&lt;p&gt;My thesis topic was in the research area of experimental condensed matter physics. More specifically, I studied the optical properties of &lt;a href=&quot;https://en.wikipedia.org/wiki/Carbon_nanotube&quot;&gt;single-wall carbon nanotubes&lt;/a&gt;. In the later months of 2019, I had second thoughts and opted to change my field of study.&lt;/p&gt;
&lt;p&gt;I originally became a Ph.D. student because I wanted to become a professor. However, it took a few years in graduate school before I recognized that this wasn’t the best career trajectory for me.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.nature.com/articles/d41586-019-02586-5&quot;&gt;There aren’t many tenure-track faculty positions open these days&lt;/a&gt;, meaning that most Ph.D. students won’t be able to attain these coveted opportunities. I also realized that I enjoy problem-solving in general. There are relatively more jobs in the private sector where I can develop innovative solutions.&lt;/p&gt;
&lt;p&gt;In late 2019, I visited the career center at Rice University to get some ideas on possible career paths. My counselor suggested some career assessments.&lt;/p&gt;
&lt;p&gt;After completing those, the top-recommended profession was that of a computer scientist. I wasn’t surprised. I’ve known that I enjoy computer programming. After all, I started programming in my second year of high school. The results from the assessments also got me thinking about getting involved in computational research once again.&lt;/p&gt;
&lt;p&gt;As an undergraduate at Cornell University, I collaborated on a &lt;a href=&quot;https://www.sciencedirect.com/science/article/abs/pii/S092702561830569X&quot;&gt;research project&lt;/a&gt; involving machine learning. Here, I investigated how to use machine learning algorithms to predict the properties of newly discovered materials.&lt;/p&gt;
&lt;p&gt;I remembered how much I enjoyed working on this project. Revisiting this experience convinced me to finish my Ph.D. under a professor interested in applying machine learning to research problems.&lt;/p&gt;
&lt;p&gt;I started working under a new Ph.D. advisor in February 2020. Since then, I’ve been applying machine learning models to decode brain activity measured using &lt;a href=&quot;https://en.wikipedia.org/wiki/Functional_magnetic_resonance_imaging&quot;&gt;functional magnetic resonance imaging&lt;/a&gt;. In time, I hope to improve my expertise in &lt;a href=&quot;https://en.wikipedia.org/wiki/Deep_learning&quot;&gt;deep learning&lt;/a&gt;. The application of deep learning algorithms to medical data is still relatively new and I’m aiming to blaze new trails in this research area.&lt;/p&gt;
&lt;h2 id=&quot;exploring-new-directions-in-my-photography&quot;&gt;Exploring New Directions in My Photography&lt;a href=&quot;#exploring-new-directions-in-my-photography&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In 2020, I got into the swing of my photography. Following in the footsteps of two of my favorite photographers, &lt;a href=&quot;http://alanschaller.com/&quot;&gt;Alan Schaller&lt;/a&gt; and &lt;a href=&quot;https://www.kaiziehlphotos.com/portfolio/&quot;&gt;Kai Zeihl&lt;/a&gt;, I did only black and white photography in the early months of the year. I feel nostalgic as I took some of my best photographs in this period. These photos below are some highlights:&lt;/p&gt;
&lt;p&gt;Unfortunately, I started slowing down in March. The coronavirus lockdowns dampened my spirits. Not to mention, I struggled to cope with the deaths of Breonna Taylor, George Floyd, and Ahmaud Arbery alongside others.&lt;/p&gt;
&lt;p&gt;The death of George Floyd especially hit close to home as his funeral was held here in Houston. As a person of color, I didn’t feel inspired to create new artwork as I reflected on these events. I even thought about quitting photography altogether. But I found the will to resume at the beginning of the fall season.&lt;/p&gt;
&lt;p&gt;By this time, I felt that my work was becoming stagnant. Black and white photography felt too limiting so I decided to try color photography instead. I was initially nervous at first as I wasn’t familiar with color theory.&lt;/p&gt;
&lt;p&gt;However, &lt;a href=&quot;https://www.youtube.com/watch?v=mC8ol2-V7Ck&quot;&gt;this online workshop&lt;/a&gt; presented by &lt;a href=&quot;https://joannakustra.com/&quot;&gt;Joanna Kustra&lt;/a&gt;, a fashion photographer, inspired me. I’m grateful for this workshop as it opened up new creative possibilities in my photography. These photos below are some of my favorites since making the switch to color photography:&lt;/p&gt;
&lt;h2 id=&quot;creating-this-website&quot;&gt;Creating This Website&lt;a href=&quot;#creating-this-website&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I’m happy that I finally created my website back in October. I’ve wanted to do this for quite some time. Because I had no prior experience in developing websites, I took a few online web development courses. The &lt;a href=&quot;https://www.theodinproject.com/home&quot;&gt;Odin Project&lt;/a&gt; and &lt;a href=&quot;https://www.freecodecamp.org/&quot;&gt;Free Code Camp&lt;/a&gt; were my favorites. After learning enough HTML and CSS, I began creating my website.&lt;/p&gt;
&lt;p&gt;I initially found it tricky to structure my web pages. It was also cumbersome to create each web page by hand. But I soon discovered the convenience of using &lt;a href=&quot;https://www.netlify.com/blog/2020/04/14/what-is-a-static-site-generator-and-3-ways-to-find-the-best-one/&quot;&gt;static site generators&lt;/a&gt; (SSGs) for this purpose. There are so many to choose from!&lt;/p&gt;
&lt;p&gt;I selected &lt;a href=&quot;https://www.11ty.dev/&quot;&gt;Eleventy&lt;/a&gt; as my SSG as it appeared simple, modern, and flexible enough for my needs. I mainly write code in Python these days so I briefly considered using &lt;a href=&quot;https://blog.getpelican.com/&quot;&gt;Pelican&lt;/a&gt;. Because I wanted to learn a new programming language, I picked Eleventy because it was written in JavaScript. This experience has taught me a lot about web development and I look forward to adding new features to my website as time goes.&lt;/p&gt;
&lt;h2 id=&quot;reuniting-with-my-extended-family&quot;&gt;Reuniting with My Extended Family&lt;a href=&quot;#reuniting-with-my-extended-family&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After so many years, I got in touch with my cousins on my father’s side of the family. Remarkably, it took a pandemic for this to finally happen just a few months ago.&lt;/p&gt;
&lt;p&gt;My cousins all reside in Ghana, the land of my birth. It has been at least 15 years since I left Ghana in the middle of my childhood which is partly why I haven’t kept in touch with my relatives over the years. I’m hoping that I will be able to visit them later in 2021 or perhaps early next year.&lt;/p&gt;
&lt;h2 id=&quot;goals-for-2021&quot;&gt;Goals for 2021&lt;a href=&quot;#goals-for-2021&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;deep-learning&quot;&gt;Deep Learning&lt;a href=&quot;#deep-learning&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One of my goals in 2021 is to develop my expertise in machine learning, especially for medical applications. I want to become acquainted with state-of-the-art algorithms.&lt;/p&gt;
&lt;p&gt;Given that I am a novice, I plan to first acquire some general knowledge by finishing both parts of the &lt;a href=&quot;https://www.fast.ai/&quot;&gt;fast.ai&lt;/a&gt; course by the end of April. I’ve thought of pivoting to the &lt;a href=&quot;https://www.deeplearning.ai/deep-learning-specialization/&quot;&gt;deep learning course by Dr. Andrew Ng&lt;/a&gt; afterwards.&lt;/p&gt;
&lt;p&gt;Much of the material here overlaps with that of fast.ai. But Dr. Ng’s course also contains additional topics such as regularization. I expect it will take three months for me to finish this course. Soon after, I will focus on learning the applications of deep learning in the medical field.&lt;/p&gt;
&lt;p&gt;I’m looking to take the &lt;a href=&quot;https://www.coursera.org/specializations/ai-for-medicine#courses&quot;&gt;AI for medicine specialization course&lt;/a&gt; to receive an introduction to the subject. This course will also take another 3 months to complete. That means if all goes according to plan, I finish by the end of the year.&lt;/p&gt;
&lt;p&gt;I’m also interested in natural language processing. The applications in this area fascinate me. These include building chatbots, performing sentiment analysis on written text, or even doing language translations. At some stage, I may adjust my plans to accommodate this interest.&lt;/p&gt;
&lt;p&gt;This year, I’d also like to work on personal projects to solidify my understanding of deep learning. I may end up spending more time on these projects than on taking the courses listed above. That shouldn’t be a problem even if I don’t complete all of the coursework as intended. It would be more valuable for me to apply deep learning wherever my curiosity leads me.&lt;/p&gt;
&lt;h3 id=&quot;blogging&quot;&gt;Blogging&lt;a href=&quot;#blogging&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I want to post new articles here regularly. I’m aiming to publish at least one new blog post each month. Furthermore, I also intend to publish at least six articles on technical subjects related to machine learning. Most of these articles will concentrate on what I learn from my adventures in deep learning.&lt;/p&gt;
&lt;h3 id=&quot;reading&quot;&gt;Reading&lt;a href=&quot;#reading&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I didn’t spend much time reading books in 2020. I intend to change that in 2021. I’m aspiring to read 12 books this year. I’m also thinking about writing some book reviews to post here along the way.&lt;/p&gt;
&lt;h3 id=&quot;photography&quot;&gt;Photography&lt;a href=&quot;#photography&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Of my weaknesses as a photographer, I feel that portrait photography is chief among those. I don’t often take portraits of people. I want to rectify this even though it will be challenging to do so in light of the pandemic.&lt;/p&gt;
&lt;p&gt;In any case, I want to become more familiar with using an off-camera flash for portrait photography. I already have one sitting in my closet that I haven’t used very often. In the worst-case scenario, I can practice by taking self-portraits.&lt;/p&gt;
&lt;h3 id=&quot;social&quot;&gt;Social&lt;a href=&quot;#social&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;This year, I want to find ways to be more socially active. I frequently receive emails regarding Zoom socials. But I rarely bother to attend them because I sometimes dread having to socialize on Zoom. Yet, I imagine that other people feel the same way. Given that there aren’t any other safer alternatives, I will make an effort to be part of Zoom socials whenever I can.&lt;/p&gt;
&lt;p&gt;I’m also considering that Twitter may be a place to forge new connections with others. However, I’m not so active on Twitter as I don’t yet know how to use it to meet my needs. I intend to explore this further.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;2020 was a challenging but pivotal year for me in many respects. 2021 has so far gotten off to another difficult start as political tensions are quite high at the moment. Nonetheless, I continue to look forward to what the new year will bring.&lt;/p&gt;</content:encoded></item><item><title>Rethinking My Relationship with Instagram</title><link>https://bryananthonio.com/blog/rethinking-my-relationship-with-instagram/</link><guid isPermaLink="true">https://bryananthonio.com/blog/rethinking-my-relationship-with-instagram/</guid><description>Examining how I&apos;ve used Instagram in the past and my newfound desire to start sharing my photography on my website.</description><pubDate>Fri, 08 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For as long as I can remember, my relationship with Instagram has always obeyed a boom and bust cycle. There are days when I happily use the app.&lt;/p&gt;
&lt;p&gt;On such occasions, I enjoy scrolling through a bottomless feed of pictures posted by my friends and my favorite photographers, and sharing my photos. These moments become eclipsed by periods where I’d rather have nothing to do with Instagram.&lt;/p&gt;
&lt;p&gt;I’ve struggled with the fact that sites like Instagram &lt;a href=&quot;https://behavioralscientist.org/21st-century-skinner-box/&quot;&gt;are addicting, by design&lt;/a&gt;. Social media platforms &lt;a href=&quot;https://www.theatlantic.com/technology/archive/2013/06/skinner-marketing-were-the-rats-and-facebook-likes-are-the-reward/276613/&quot;&gt;function as Skinner boxes&lt;/a&gt;, doling out virtual rewards to users at irregular intervals to keep them hooked on these services.&lt;/p&gt;
&lt;p&gt;Likes represent the most prominent of these rewards. Indeed, it’s incredible that something as simple as the &lt;a href=&quot;https://www.vice.com/en/article/mbag3a/the-inventor-of-the-like-button-wants-you-to-stop-worrying-about-likes&quot;&gt;like button&lt;/a&gt; aids in retaining users. It’s also peculiar that we have allowed the like button to play a significant role in our online social interactions.&lt;/p&gt;
&lt;p&gt;When I first started sharing my photography on Instagram, I paid close attention to the number of likes others gave me. Because my posts didn’t receive comments, I naively regarded likes as a substitute for constructive feedback. My photos with more likes were superior to those with fewer, I thought.&lt;/p&gt;
&lt;p&gt;I also felt elated whenever I surpassed my high score, the maximum number of likes I ever received on a single post. But I eventually learned better.&lt;/p&gt;
&lt;p&gt;Does anyone keep track of similar social metrics in their day to day lives? For instance, would you track the number of people who congratulated you for getting promoted at work? Would you bother to count the amount of people who wished you a happy birthday?&lt;/p&gt;
&lt;p&gt;Most people probably wouldn’t measure these figures on their own. But Instagram and other social media platforms certainly would, if you allowed them.&lt;/p&gt;
&lt;p&gt;I’ve realized that focusing on social approval metrics places too much emphasis on the quantity of our social interactions. It also obscures the depth and quality of how we connect with each other. Nowadays, I try not to pay attention to likes.&lt;/p&gt;
&lt;p&gt;For many months, I’ve aimed to avoid seeing the number of likes on my posts. After opening the app, I quickly clear the bright red notification that shows me the number of likes and new followers I’ve obtained.&lt;/p&gt;
&lt;p&gt;I’ve felt awkward about adopting this strange ritual to prevent myself from getting caught in Instagram’s Skinner box. It’s quite a chore.&lt;/p&gt;
&lt;p&gt;Upon failing to maintain this practice, I find myself on a slippery slope. I begin to care more about the number of likes I’m receiving than I should. This happens when I start visiting Instagram out of boredom, instead of using the app sparingly.&lt;/p&gt;
&lt;p&gt;I ultimately take a break from Instagram as I contend with these conflicting thoughts regarding likes. Once this cognitive dissonance fades, I resume using Instagram once more.&lt;/p&gt;
&lt;p&gt;The boom and bust cycle partly persists because I enjoy posting new photos. My page on Instagram functions as a public diary of my photography journey. Creating a new post is akin to adding a new entry.&lt;/p&gt;
&lt;p&gt;As I continue to improve in my photography, I enjoy being able to review my journey. Admittedly, some part of me still enjoys getting likes on new posts even though I don’t always see them. Nonetheless, the seemingly eternal struggle to maintain my sanity on the platform has urged me to pursue a healthier alternative.&lt;/p&gt;
&lt;p&gt;I recently decided to start sharing my photography on my website. Here, I don’t have to worry about navigating through predatory cues designed to get me hooked on performing certain behaviors. But I don’t plan to get rid of my Instagram page yet as it still possesses sentimental value. It’s more likely in the future that I will be using Instagram less frequently.&lt;/p&gt;
&lt;p&gt;Surprisingly, I’ve found it refreshing to post photos on my website. The experience of sharing my images without expecting to receive likes feels more organic. It’s almost as though a weight has been lifted off my shoulders.&lt;/p&gt;
&lt;p&gt;I expect that the photo gallery on my website will eventually overshadow my Instagram page. It’s hard to pinpoint exactly why, but I strongly feel that the gallery on my website will also be more authentic.&lt;/p&gt;</content:encoded></item><item><title>My First Steps in Photography</title><link>https://bryananthonio.com/blog/my-first-steps-in-photography/</link><guid isPermaLink="true">https://bryananthonio.com/blog/my-first-steps-in-photography/</guid><description>How I got started in photography, including some of the challenges I faced and the lessons I learned.</description><pubDate>Thu, 03 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When I first started photography almost a year ago, I was going through a difficult time in my graduate school career. In the fall season of 2019, I started writing my master’s thesis only to eventually realize that I didn’t feel passionate about my research. This experience discouraged me and withered my enthusiasm to continue writing. Looking for ways to cope with these circumstances, I decided to start a new hobby.&lt;/p&gt;
&lt;p&gt;It would be misleading of me to say that I knew what I was getting myself into when I chose to become a photographer. I thought it would be easy. After all, how hard could it be to take good pictures? Surprisingly, my journey in photography challenged this notion and gave me a newfound appreciation for photography as an art form.&lt;/p&gt;
&lt;p&gt;My journey began on October 19th, 2019, the day I bought a used camera: a Nikon D3300 along with a 35 mm prime lens. When I first held my camera in my hands, I pored over it as if it was a technological marvel. So much so that I felt like a caveman who had just discovered fire when I finally figured out how to take my first picture. Now, what exactly was I going to photograph? In this blog post, I will showcase&lt;/p&gt;
&lt;p&gt;I held my first photography sessions at Hermann Park. Here, I wandered around, taking snapshots of scenes that grabbed my attention. I often didn’t know what I was doing; that hardly prevented me from enjoying myself. On days where writing my thesis took a toll on me, I would schedule more time to go outside to take pictures.&lt;/p&gt;
&lt;div class=&quot;layout-stack&quot; style=&quot;--stack-gap: var(--spacing-xl);&quot;&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-mcgovern_lake-lg_Z2gJwmw.jpg&quot; alt=&quot;A view of the Texas Medical Center from McGovern Lake.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;A view of the Texas Medical Center from McGovern Lake&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-hermann_park_trees-lg_1YP4Gq.jpg&quot; alt=&quot;A collection of tall trees by McGovern Lake.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Trees by McGovern Lake.&lt;/figcaption&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;h2 id=&quot;learning-the-technical-aspects-of-photography&quot;&gt;Learning the Technical Aspects of Photography&lt;a href=&quot;#learning-the-technical-aspects-of-photography&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Within my first two months as a photographer, I learned how to operate my camera. During this process, I ascertained how to focus my camera, investigated three distinct camera settings that determined the quality of my photographs, and discovered additional features that made my camera easy to use.&lt;/p&gt;
&lt;p&gt;At first, I didn’t understand how to focus the camera lens at a fixed position. This frustrated me as some of my photos looked very sharp, whereas others appeared quite blurry. It took me a few visits to my camera’s manual before I finally understood how to control the autofocus settings.&lt;/p&gt;
&lt;p&gt;Everything finally clicked for me one day when I practiced focusing my camera on various objects in my apartment. It came as a surprise when I realized that my camera could focus on scenes with prominent contours but would struggle to focus on areas with little contrast. For example, it couldn’t focus directly on the walls in my apartment. Yet, it could easily focus on wall corners, where two adjacent walls united to form a well-defined edge. This insight marked a small but significant step towards understanding my camera’s capabilities.&lt;/p&gt;
&lt;p&gt;I also spent time analyzing how the shutter speed, ISO sensitivity, and aperture settings on my camera affected my photographs. Despite reading various articles and watching many videos about this, I experimented with these different settings to fully grasp their significance. I would photograph the same scenes several times—each captured with distinct settings for the lens aperture, shutter speed, and ISO.&lt;/p&gt;
&lt;p&gt;In the process, I devised some very rough, approximate rules of thumb to guide me. A wider aperture setting could produce a blurry background, commonly referred to as a shallow depth of field, around my subject. To negate this, I needed to set the lens aperture to be more closed. The shutter speed setting dealt primarily with handling motion blur. At high enough shutter speeds, my camera could effectively capture a photo with all motion frozen. Finally, the ISO setting, which controls the camera’s sensitivity to light, needed to be adjusted to make sure that the photo would be adequately bright.&lt;/p&gt;
&lt;p&gt;I found it difficult to mentally juggle each of these individual rules at the same time so I began using my camera in the aperture-priority mode instead. Essentially, this mode allowed me to control just the aperture. The camera would then automatically pick a suitable ISO and shutter speed.&lt;/p&gt;
&lt;p&gt;Quite often, I experimented with the aperture setting in the McGovern Centennial Gardens at Hermann Park where I enjoyed photographing flowers. My first photos depicting flowers with a shallow depth of field impressed me. At this point, I felt like I had taken my first photographs that looked “professional.”&lt;/p&gt;
&lt;div class=&quot;layout-stack&quot; style=&quot;--stack-gap: var(--spacing-xl);&quot;&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-flowers-1-lg_Z111oHo.jpg&quot; alt=&quot;A picture of two pink roses with a shallow depth of field in the background.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Pink roses in the rose garden of the McGovern Centennial Gardens.&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-flowers-2-lg_ZbWP7u.jpg&quot; alt=&quot;A picture of daisies with a shallow depth of field in the background.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;Daisies at the McGovern Centennial Gardens.&lt;/figcaption&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;On one occasion, I was walking by the reflection pool at Hermann Park when I noticed a scene that drew my attention. This event became one of my first real tests in photography. I pulled out my camera, hastily tried to get the correct settings, and captured a few photos. One of them stood out.&lt;/p&gt;
&lt;p&gt;It was a decent picture of a duck swimming in the reflection pool, shown below, leaving behind a trail of ripples. I felt proud of this small victory. If I took too long to determine the best settings, I would have missed the moment and failed to capture it. Getting a somewhat decent shot is better than getting no shot at all.&lt;/p&gt;
&lt;p&gt;Lastly, I can safely say that reading through my camera’s manual helped a lot. The manual typically introduced me to the capabilities of my camera that I previously didn’t know! Some features offered convenience. In one instance, I learned that I could designate one of the side buttons to change the ISO setting without having to access menus on the camera screen to do this. I also discovered two autofocus modes: one suitable for photographing subjects at rest, and another for capturing subjects in motion. Learning about these features boosted my confidence in knowing how to best use my camera in any given situation.&lt;/p&gt;
&lt;h2 id=&quot;understanding-composition&quot;&gt;Understanding Composition&lt;a href=&quot;#understanding-composition&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Unlike learning how to use my camera, I found composition much more tricky to grasp. Composition focuses on how to arrange visual elements in a photograph to create a compelling experience for the viewer. From my background in physics, I was so accustomed to having neatly packaged axioms to guide me. In line with this, I scoured the internet for videos, articles, and books for the best practices regarding composition.&lt;/p&gt;
&lt;p&gt;I discovered that the best ways to compose a photograph weren’t always straightforward. On the one hand, I found many general tips that gave some insight regarding compositional mistakes to avoid. On the other hand, I didn’t obtain a foolproof method that would work for every photograph. It took me a long time to realize why this was the case.&lt;/p&gt;
&lt;p&gt;Composition merely provides guidelines used to frame a photographer’s unique perspective within a picture. It doesn’t yield a creative vision to a photographer. This mirrors the role of grammar in writing. Grammar dictates the structure of a sentence but does not make creative choices regarding a sentence’s tone, mood, or pacing. Only experience, whether obtained by careful experimentation or by examining the works of proficient photographers and writers, can equip you with a better sense of how to make creative choices.&lt;/p&gt;
&lt;div class=&quot;layout-stack&quot; style=&quot;--stack-gap: var(--spacing-xl);&quot;&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-discovery_green-lg_xXTPo.jpg&quot; alt=&quot;TheGeorge R. Brown Convention center.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The McGovern Convention Center in downtown Houston.&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-texas_medical_center-lg_2iQVrX.jpg&quot; alt=&quot;The Texas Medical Center skyline at dusk.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The Texas Medical Center skyline at dusk.&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure class=&quot;photo-wide photo-wide--landscape&quot; data-astro-cid-wrwtn42u&gt;&lt;img src=&quot;https://bryananthonio.com/_astro/copyright-bryan-anthonio_com-houston_skyline-lg_Z1hTDce.jpg&quot; alt=&quot;The Houston skyline at night.&quot; sizes=&quot;auto&quot; data-astro-cid-wrwtn42u=&quot;true&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1425&quot; height=&quot;950&quot; class=&quot;photo-wide__img&quot;&gt;&lt;figcaption class=&quot;figcaption&quot; data-astro-cid-wrwtn42u&gt;The Houston skyline at night.&lt;/figcaption&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;I developed my eye for photography by consistently going outside with my camera (sometimes, daily) to take new pictures and critiquing my work. After reviewing hundreds, if not thousands of photos in my archive, I recognized only a tiny handful of images that I liked. I felt sad about this at first. However, this is the norm for even the most celebrated photographers in history (including &lt;a href=&quot;https://petapixel.com/2016/05/25/contact-sheets-story-behind-every-photo/&quot;&gt;Henri Cartier-Bresson&lt;/a&gt;). On the positive side, I now had some photographs that I could study to get a sense of my artistic voice.&lt;/p&gt;
&lt;p&gt;I see that in some of my early pictures, I heavily gravitated towards symmetry. Perhaps this approach to composition drew its origins from my technical background. Symmetry provides a meaningful contribution to physics (see &lt;a href=&quot;https://www.discovermagazine.com/the-sciences/how-mathematician-emmy-noethers-theorem-changed-physics&quot;&gt;Noether’s theorem&lt;/a&gt;). As a physics student, I received thorough training on recognizing relevant symmetries in a given problem statement. The presence of symmetries often unlocked mathematical shortcuts, making it feasible to derive simple solutions to many homework problems and exam questions. By extension, I found it quite satisfying to identify symmetries in the real world.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;#conclusion&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I entered the world of photography during a difficult period of my graduate school career. My new hobby lifted my spirits as I got through writing my thesis. Along the way, I taught myself how to use my camera and became acquainted with its capabilities. I also took my first steps in discovering my artistic voice. Even though a year has passed since I started, I still haven’t yet worked out a coherent creative direction I’d like to pursue. Nonetheless, I continue to enjoy my photography journey as I develop as an artist.&lt;/p&gt;</content:encoded></item><item><title>Making the Most of My Solitude</title><link>https://bryananthonio.com/blog/making-the-most-of-my-solitude/</link><guid isPermaLink="true">https://bryananthonio.com/blog/making-the-most-of-my-solitude/</guid><description>Reflecting on my life under quarantine and the steps I took to find equanimity.</description><pubDate>Fri, 20 Nov 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I did not know what to expect when I first learned that we now had an invisible enemy: COVID-19. It all happened so fast.&lt;/p&gt;
&lt;p&gt;In March, the constant downpour of news regarding the pandemic had prompted me to shelter in place.&lt;/p&gt;
&lt;p&gt;Anxiety now lingered in the fresh air every time I needed to venture outside as stores were facing shortages of food and supplies. My thoughts became bleak as I adjusted to feelings of isolation in this new world. Yet, my experiences taught me to make the most of my time in solitude.&lt;/p&gt;
&lt;p&gt;My life had been upended by the pandemic. Almost all forms of meaningful social interaction now took place online, making day-to-day interactions with my friends, family, and colleagues less tangible.&lt;/p&gt;
&lt;p&gt;For several weeks, I did not sleep very well. I also lost a sense of a daily routine as I started working from home.&lt;/p&gt;
&lt;p&gt;Finding ways to separate my work hours from my personal time proved to be challenging as these two facets of my life were now more closely intertwined, competing to occupy the same space in my apartment. What was I going to do to find balance?&lt;/p&gt;
&lt;p&gt;I obtained some insight after many nights of journaling. In this practice, I felt urged to tend to my life as though it were a garden by developing a clearer understanding of my needs. It soon became evident that apart from not having much of a social life, I was not spending enough time engaging myself in meaningful activities.&lt;/p&gt;
&lt;p&gt;After work, I would aimlessly browse the web on social media and various news aggregate sites or look for something to watch on video streaming platforms. These were quick and easy ways of pacifying my boredom. They did not fulfill me in the long run.&lt;/p&gt;
&lt;p&gt;After setting restrictions on such activities I needed to figure out what to do with my time. I sat down and quietly contemplated this near the end of each day. My mind’s eye soon pictured the person I wanted to become by the end of the pandemic and helped me to define actionable steps towards my goals.&lt;/p&gt;
&lt;p&gt;First, I envisioned myself developing as a writer whilst honing my talents in photography. For quite some time, photography had remained my only creative outlet. Over the past year, I learned a lot about the craft and continually pushed to take my skills to greater heights.&lt;/p&gt;
&lt;p&gt;However, I took frequent breaks from photography to avoid burning out and now felt eager to pick up writing to occupy myself during these periods. I decided to facilitate this by creating a platform where I could document my photography journey and share other insights I have learned. Hence, I put my web development skills to the test by building my blog site. I yearned to make this place my very own digital garden.&lt;/p&gt;
&lt;p&gt;Furthermore, I reorganized my daily schedule to focus more on self-care. I now went on long walks each morning to energize myself before work and catch some sunlight which I sorely lacked.&lt;/p&gt;
&lt;p&gt;Given that I was spending most of my time indoors, I devoted more effort towards keeping my apartment clean and free of clutter. In addition, I set aside time to read engaging long-form essays and write in my journal regularly. These changes helped me nurture my mental health, especially on the most difficult days.&lt;/p&gt;
&lt;p&gt;In solitude, I learned to become more intentional with my time. I examined my need for fulfillment and opted to invest time towards becoming a writer. This pursuit encouraged me to launch this website to write about my progress in photography and share my musings.&lt;/p&gt;
&lt;p&gt;I also learned to prioritize taking better care of myself. While it remains tough to maintain an active social life, I take solace in knowing that things will not remain this way forever as the pandemic will eventually come to pass.&lt;/p&gt;</content:encoded></item><item><title>Hello, World!</title><link>https://bryananthonio.com/blog/hello-world/</link><guid isPermaLink="true">https://bryananthonio.com/blog/hello-world/</guid><description>Here&apos;s a few words on launching this blog.</description><pubDate>Sat, 31 Oct 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Hello, World! This marks my first post on my new blog. In putting together this blog, I decided to try my hand at building a website from scratch rather than use a website template. It is not the first time that I have built a website before, but it has certainly been my most ambitious project in the realm of web development. At the moment, I am feeling quite elated to have finally finished the first version of this website. Furthermore, I could not have pulled off this feat without relying on the help of the static site builder &lt;a href=&quot;https://www.11ty.dev/&quot;&gt;Eleventy&lt;/a&gt; which made the process of developing this website relatively pleasant.&lt;/p&gt;
&lt;p&gt;In the meantime, I have already finished adding an &lt;a href=&quot;https://bryananthonio.com/about/&quot;&gt;About Me&lt;/a&gt; section where you can learn a bit more about myself. So far, I am still in the process of drafting new articles and I look forward to posting them here soon. Thank you for tuning in!&lt;/p&gt;</content:encoded></item></channel></rss>