summaryrefslogtreecommitdiff
path: root/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
diff options
context:
space:
mode:
authornavanchauhan <navanchauhan@gmail.com>2022-11-07 23:36:11 -0700
committernavanchauhan <navanchauhan@gmail.com>2022-11-07 23:36:11 -0700
commitd75527f7eecc4e2fcdd18ab157412506717c8adb (patch)
tree8a96e3036d59030f5654725edb1ca5ad6db4cb4e /docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
parent8ca94ab784138ef673bc7c1691b99e2d4d69e015 (diff)
add blog post
Diffstat (limited to 'docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html')
-rw-r--r--docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html30
1 files changed, 20 insertions, 10 deletions
diff --git a/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html b/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
index 0b307fd..cdae911 100644
--- a/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
+++ b/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
@@ -89,7 +89,8 @@ I created a sample JSON with only 3 examples (I know, very less, but works for a
<p><img src="/assets/posts/swift-chatbot/drugs-json.png" alt="Screenshot of Sample Dataset" /></p>
-<div class="codehilite"><pre><span></span><code><span class="p">[</span><span class="w"></span>
+<div class="codehilite">
+<pre><span></span><code><span class="p">[</span><span class="w"></span>
<span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="nt">&quot;tokens&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;Tell&quot;</span><span class="p">,</span><span class="s2">&quot;me&quot;</span><span class="p">,</span><span class="s2">&quot;about&quot;</span><span class="p">,</span><span class="s2">&quot;the&quot;</span><span class="p">,</span><span class="s2">&quot;drug&quot;</span><span class="p">,</span><span class="s2">&quot;Aspirin&quot;</span><span class="p">,</span><span class="s2">&quot;.&quot;</span><span class="p">],</span><span class="w"></span>
<span class="w"> </span><span class="nt">&quot;labels&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">]</span><span class="w"></span>
@@ -103,7 +104,8 @@ I created a sample JSON with only 3 examples (I know, very less, but works for a
<span class="w"> </span><span class="nt">&quot;labels&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">]</span><span class="w"></span>
-</code></pre></div>
+</code></pre>
+</div>
<p><img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></p>
@@ -113,7 +115,8 @@ I created a sample JSON with only 3 examples (I know, very less, but works for a
<p><img src="/assets/posts/swift-chatbot/carbon.png" alt="Screenshot" /></p>
-<div class="codehilite"><pre><span></span><code><span class="kd">import</span> <span class="nc">CoreML</span>
+<div class="codehilite">
+<pre><span></span><code><span class="kd">import</span> <span class="nc">CoreML</span>
<span class="kd">import</span> <span class="nc">NaturalLanguage</span>
<span class="kd">let</span> <span class="nv">mlModelClassifier</span> <span class="p">=</span> <span class="k">try</span> <span class="n">IntentDetection_1</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span> <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span>
@@ -124,7 +127,8 @@ I created a sample JSON with only 3 examples (I know, very less, but works for a
<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NLTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">nameType</span><span class="p">,</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">&quot;Apple&quot;</span><span class="p">)])</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">setModels</span><span class="p">([</span><span class="n">tagPredictor</span><span class="p">],</span> <span class="n">forTagScheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">&quot;Apple&quot;</span><span class="p">))</span>
-</code></pre></div>
+</code></pre>
+</div>
<p>Now, we define a simple structure which the custom function(s) can use to access the provided input.
It can also be used to hold additional variables.
@@ -134,7 +138,8 @@ The latter can be replaced with a function which asks the user for the input. </
<p><img src="/assets/posts/swift-chatbot/carbon-2.png" alt="Screenshot" /></p>
-<div class="codehilite"><pre><span></span><code><span class="kd">struct</span> <span class="nc">User</span> <span class="p">{</span>
+<div class="codehilite">
+<pre><span></span><code><span class="kd">struct</span> <span class="nc">User</span> <span class="p">{</span>
<span class="kd">static</span> <span class="kd">var</span> <span class="nv">message</span> <span class="p">=</span> <span class="s">&quot;&quot;</span>
<span class="p">}</span>
@@ -158,14 +163,16 @@ The latter can be replaced with a function which asks the user for the input. </
<span class="p">}</span>
<span class="p">}</span>
-</code></pre></div>
+</code></pre>
+</div>
<p>Sometimes, no action needs to be performed, and the bot can use a predefined set of responses.
Otherwise, if an action is required, it can call the custom action.</p>
<p><img src="/assets/posts/swift-chatbot/carbon-3.png" alt="Screenshot" /></p>
-<div class="codehilite"><pre><span></span><code><span class="kd">let</span> <span class="nv">defaultResponses</span> <span class="p">=</span> <span class="p">[</span>
+<div class="codehilite">
+<pre><span></span><code><span class="kd">let</span> <span class="nv">defaultResponses</span> <span class="p">=</span> <span class="p">[</span>
<span class="s">&quot;greetings&quot;</span><span class="p">:</span> <span class="s">&quot;Hello&quot;</span><span class="p">,</span>
<span class="s">&quot;banter&quot;</span><span class="p">:</span> <span class="s">&quot;no, plix no&quot;</span>
<span class="p">]</span>
@@ -173,14 +180,16 @@ Otherwise, if an action is required, it can call the custom action.</p>
<span class="kd">let</span> <span class="nv">customActions</span> <span class="p">=</span> <span class="p">[</span>
<span class="s">&quot;deez-drug&quot;</span><span class="p">:</span> <span class="n">customAction</span>
<span class="p">]</span>
-</code></pre></div>
+</code></pre>
+</div>
<p>In the sample input, the program is updating the User.message and checking if it has a default response.
Otherwise, it calls the custom action.</p>
<p><img src="/assets/posts/swift-chatbot/carbon-4.png" alt="Screenshot" /></p>
-<div class="codehilite"><pre><span></span><code><span class="kd">let</span> <span class="nv">sampleMessages</span> <span class="p">=</span> <span class="p">[</span>
+<div class="codehilite">
+<pre><span></span><code><span class="kd">let</span> <span class="nv">sampleMessages</span> <span class="p">=</span> <span class="p">[</span>
<span class="s">&quot;Hey there, how is it going&quot;</span><span class="p">,</span>
<span class="s">&quot;hello, there&quot;</span><span class="p">,</span>
<span class="s">&quot;Who let the dogs out&quot;</span><span class="p">,</span>
@@ -200,7 +209,8 @@ Otherwise, it calls the custom action.</p>
<span class="bp">print</span><span class="p">(</span><span class="n">customActions</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span><span class="o">!</span><span class="p">())</span>
<span class="p">}</span>
<span class="p">}</span>
-</code></pre></div>
+</code></pre>
+</div>
<p><img src="/assets/posts/swift-chatbot/output.png" alt="Output" /></p>