diff options
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.html | 30 |
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">"tokens"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"Tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"drug"</span><span class="p">,</span><span class="s2">"Aspirin"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span><span class="w"></span> <span class="w"> </span><span class="nt">"labels"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</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">"labels"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</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">"Apple"</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">"Apple"</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">""</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">"greetings"</span><span class="p">:</span> <span class="s">"Hello"</span><span class="p">,</span> <span class="s">"banter"</span><span class="p">:</span> <span class="s">"no, plix no"</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">"deez-drug"</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">"Hey there, how is it going"</span><span class="p">,</span> <span class="s">"hello, there"</span><span class="p">,</span> <span class="s">"Who let the dogs out"</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> |