summaryrefslogtreecommitdiff
path: root/docs/posts
diff options
context:
space:
mode:
Diffstat (limited to 'docs/posts')
-rw-r--r--docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html74
1 files changed, 64 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 e4e4d1d..50c74a2 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
@@ -51,8 +51,33 @@ Disclaimer: This is a very crude proof-of-concept, but it does work.</p>
<p>I opened a CSV file and added some sample entries, with a corresponding label.</p>
-<p><img src="/assets/posts/swift-chatbot/intent-csv.png" alt="Screenshot of Sample Dataset" />
-<img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></p>
+<p><img src="/assets/posts/swift-chatbot/intent-csv.png" alt="Screenshot of Sample Dataset" /></p>
+
+<pre><code>text,label
+hey there,greetings
+hello,greetings
+good morning,greetings
+good evening,greetings
+hi,greetings
+open the pod bay doors,banter
+who let the dogs out,banter
+ahh that's hot,banter
+bruh that's rad,banter
+nothing,banter
+da fuq,banter
+can you tell me details about the compound aspirin,deez-drug
+i want to know about some compounds,deez-drug
+search about the compound,deez-drug
+tell me about the molecule,deez-drug
+tell me about something,banter
+tell me something cool,banter
+tell a joke,banter
+make me a sandwich,banter
+whatcha doing,greetings
+i love you,banter
+</code></pre>
+
+<p><img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></p>
<h2>Word Tagging</h2>
@@ -60,8 +85,25 @@ Disclaimer: This is a very crude proof-of-concept, but it does work.</p>
This model will be only called if the intent from the classifier is a custom action.
I created a sample JSON with only 3 examples (I know, very less, but works for a crude PoC).</p>
-<p><img src="/assets/posts/swift-chatbot/drugs-json.png" alt="Screenshot of Sample Dataset" />
-<img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></p>
+<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="p">{</span>
+ <span class="nt">&quot;tokens&quot;</span><span class="p">:</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="nt">&quot;labels&quot;</span><span class="p">:</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="p">},</span>
+ <span class="p">{</span>
+ <span class="nt">&quot;tokens&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;Please&quot;</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;information&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;compound&quot;</span><span class="p">,</span><span class="s2">&quot;salicylic&quot;</span><span class="p">,</span><span class="s2">&quot;acid&quot;</span><span class="p">,</span><span class="s2">&quot;.&quot;</span><span class="p">],</span>
+ <span class="nt">&quot;labels&quot;</span><span class="p">:</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;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;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">]</span>
+ <span class="p">},</span>
+ <span class="p">{</span>
+ <span class="nt">&quot;tokens&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;Information&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;compound&quot;</span><span class="p">,</span><span class="s2">&quot;Ibuprofen&quot;</span><span class="p">,</span><span class="s2">&quot;please&quot;</span><span class="p">,</span><span class="s2">&quot;.&quot;</span><span class="p">],</span>
+ <span class="nt">&quot;labels&quot;</span><span class="p">:</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="p">}</span>
+<span class="p">]</span>
+</code></pre></div>
+
+<p><img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></p>
<h2>Time to Get Swift-y</h2>
@@ -136,14 +178,26 @@ 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">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>
+<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>
+ <span class="s">&quot;can you tell me about the compound Geraniin&quot;</span><span class="p">,</span>
+ <span class="s">&quot;what do you know about the compound Ibuprofen&quot;</span><span class="p">,</span>
+ <span class="s">&quot;please, tell me more about the compound&quot;</span><span class="p">,</span>
+ <span class="s">&quot;please, tell me more about the molecule dihydrogen-monoxide&quot;</span>
<span class="p">]</span>
-<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>
+<span class="k">for</span> <span class="n">sampleMessage</span> <span class="k">in</span> <span class="n">sampleMessages</span> <span class="p">{</span>
+ <span class="n">User</span><span class="p">.</span><span class="n">message</span> <span class="p">=</span> <span class="n">sampleMessage</span>
+ <span class="kd">let</span> <span class="nv">prediction</span> <span class="p">=</span> <span class="n">intentPredictor</span><span class="p">.</span><span class="n">predictedLabel</span><span class="p">(</span><span class="k">for</span><span class="p">:</span> <span class="n">sampleMessage</span><span class="p">)</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="n">defaultResponses</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">)</span> <span class="p">{</span>
+ <span class="bp">print</span><span class="p">(</span><span class="n">defaultResponses</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="k">else</span> <span class="k">if</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="kc">nil</span><span class="p">)</span> <span class="p">{</span>
+ <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>
<p><img src="/assets/posts/swift-chatbot/output.png" alt="Output" /></p>