1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
<link rel="stylesheet" href="/assets/main.css" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>id="building-a-fake-news-detector-with-turicreate">Building a Fake News Detector with Turicreate</title>
<meta name="og:site_name" content="Navan Chauhan" />
<link rel="canonical" href="https://web.navan.dev/posts/2019-12-22-Fake-News-Detector.html" />
<meta name="twitter:url" content="https://web.navan.dev/posts/2019-12-22-Fake-News-Detector.html />
<meta name="og:url" content="https://web.navan.dev/posts/2019-12-22-Fake-News-Detector.html" />
<meta name="twitter:title" content="id="building-a-fake-news-detector-with-turicreate">Building a Fake News Detector with Turicreate" />
<meta name="og:title" content="id="building-a-fake-news-detector-with-turicreate">Building a Fake News Detector with Turicreate" />
<meta name="description" content="In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app" />
<meta name="twitter:description" content="In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app" />
<meta name="og:description" content="In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/images/favicon.png" type="image/png" />
<link rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan" />
<meta name="twitter:image" content="https://web.navan.dev/images/opengraph/posts/2019-12-22-Fake-News-Detector.png" />
<meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2019-12-22-Fake-News-Detector.png" />
<meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k" />
<script data-goatcounter="https://navanchauhan.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
<script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
<link rel="manifest" href="/manifest.json" />
</head>
<body>
<center><nav style="display: block;">
|
<a href="/">home</a> |
<a href="/about/">about/links</a> |
<a href="/posts/">posts</a> |
<a href="/3D-Designs/">3D designs</a> |
<!--<a href="/publications/">publications</a> |-->
<!--<a href="/repo/">iOS repo</a> |-->
<a href="/feed.rss">RSS Feed</a> |
</nav>
</center>
<main>
<h1 id="building-a-fake-news-detector-with-turicreate">Building a Fake News Detector with Turicreate</h1>
<p><strong>In this tutorial we will build a fake news detecting app from scratch, using Turicreate for the machine learning model and SwiftUI for building the app</strong></p>
<p>Note: These commands are written as if you are running a jupyter notebook.</p>
<h2 id="building-the-machine-learning-model">Building the Machine Learning Model</h2>
<h3 id="data-gathering">Data Gathering</h3>
<p>To build a classifier, you need a lot of data. George McIntire (GH: @joolsa) has created a wonderful dataset containing the headline, body and whether it is fake or real.
Whenever you are looking for a dataset, always try searching on Kaggle and GitHub before you start building your own</p>
<h3 id="dependencies">Dependencies</h3>
<p>I used a Google Colab instance for training my model. If you also plan on using Google Colab then I recommend choosing a GPU Instance (It is Free)
This allows you to train the model on the GPU. Turicreate is built on top of Apache's MXNet Framework, for us to use GPU we need to install
a CUDA compatible MXNet package.</p>
<div class="codehilite">
<pre><span></span><code><span class="nt">!pip</span><span class="na"> install turicreate</span>
<span class="na">!pip uninstall -y mxnet</span>
<span class="na">!pip install mxnet-cu100==1.4.0.post0</span>
</code></pre>
</div>
<p>If you do not wish to train on GPU or are running it on your computer, you can ignore the last two lines</p>
<h3 id="downloading-the-dataset">Downloading the Dataset</h3>
<div class="codehilite">
<pre><span></span><code><span class="nt">!wget</span><span class="na"> -q "https</span><span class="p">:</span><span class="nc">//github.com/joolsa/fake_real_news_dataset/raw/master/fake_or_real_news.csv.zip"</span>
<span class="nt">!unzip</span><span class="na"> fake_or_real_news.csv.zip</span>
</code></pre>
</div>
<h3 id="model-creation">Model Creation</h3>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">turicreate</span> <span class="k">as</span> <span class="nn">tc</span>
<span class="n">tc</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">set_num_gpus</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># If you do not wish to use GPUs, set it to 0</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">dataSFrame</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">SFrame</span><span class="p">(</span><span class="s1">'fake_or_real_news.csv'</span><span class="p">)</span>
</code></pre>
</div>
<p>The dataset contains a column named "X1", which is of no use to us. Therefore, we simply drop it</p>
<div class="codehilite">
<pre><span></span><code><span class="n">dataSFrame</span><span class="o">.</span><span class="n">remove_column</span><span class="p">(</span><span class="s1">'X1'</span><span class="p">)</span>
</code></pre>
</div>
<h4 id="splitting-dataset">Splitting Dataset</h4>
<div class="codehilite">
<pre><span></span><code><span class="n">train</span><span class="p">,</span> <span class="n">test</span> <span class="o">=</span> <span class="n">dataSFrame</span><span class="o">.</span><span class="n">random_split</span><span class="p">(</span><span class="mf">.9</span><span class="p">)</span>
</code></pre>
</div>
<h4 id="training">Training</h4>
<div class="codehilite">
<pre><span></span><code><span class="n">model</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">text_classifier</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="n">dataset</span><span class="o">=</span><span class="n">train</span><span class="p">,</span>
<span class="n">target</span><span class="o">=</span><span class="s1">'label'</span><span class="p">,</span>
<span class="n">features</span><span class="o">=</span><span class="p">[</span><span class="s1">'title'</span><span class="p">,</span><span class="s1">'text'</span><span class="p">]</span>
<span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="o">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="o">|</span> <span class="n">Iteration</span> <span class="o">|</span> <span class="n">Passes</span> <span class="o">|</span> <span class="n">Step</span> <span class="n">size</span> <span class="o">|</span> <span class="n">Elapsed</span> <span class="n">Time</span> <span class="o">|</span> <span class="n">Training</span> <span class="n">Accuracy</span> <span class="o">|</span> <span class="n">Validation</span> <span class="n">Accuracy</span> <span class="o">|</span>
<span class="o">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
<span class="o">|</span> <span class="mi">0</span> <span class="o">|</span> <span class="mi">2</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.156349</span> <span class="o">|</span> <span class="mf">0.889680</span> <span class="o">|</span> <span class="mf">0.790036</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">1</span> <span class="o">|</span> <span class="mi">4</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.359196</span> <span class="o">|</span> <span class="mf">0.985952</span> <span class="o">|</span> <span class="mf">0.918149</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">2</span> <span class="o">|</span> <span class="mi">6</span> <span class="o">|</span> <span class="mf">0.820091</span> <span class="o">|</span> <span class="mf">1.557205</span> <span class="o">|</span> <span class="mf">0.990260</span> <span class="o">|</span> <span class="mf">0.914591</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">3</span> <span class="o">|</span> <span class="mi">7</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.684872</span> <span class="o">|</span> <span class="mf">0.998689</span> <span class="o">|</span> <span class="mf">0.925267</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">4</span> <span class="o">|</span> <span class="mi">8</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">1.814194</span> <span class="o">|</span> <span class="mf">0.999063</span> <span class="o">|</span> <span class="mf">0.925267</span> <span class="o">|</span>
<span class="o">|</span> <span class="mi">9</span> <span class="o">|</span> <span class="mi">14</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">2.507072</span> <span class="o">|</span> <span class="mf">1.000000</span> <span class="o">|</span> <span class="mf">0.911032</span> <span class="o">|</span>
<span class="o">+-----------+----------+-----------+--------------+-------------------+---------------------+</span>
</code></pre>
</div>
<h3 id="testing-the-model">Testing the Model</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">est_predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
<span class="n">accuracy</span> <span class="o">=</span> <span class="n">tc</span><span class="o">.</span><span class="n">evaluation</span><span class="o">.</span><span class="n">accuracy</span><span class="p">(</span><span class="n">test</span><span class="p">[</span><span class="s1">'label'</span><span class="p">],</span> <span class="n">test_predictions</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">'Topic classifier model has a testing accuracy of </span><span class="si">{</span><span class="n">accuracy</span><span class="o">*</span><span class="mi">100</span><span class="si">}</span><span class="s1">% '</span><span class="p">,</span> <span class="n">flush</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="n">Topic</span> <span class="n">classifier</span> <span class="n">model</span> <span class="n">has</span> <span class="n">a</span> <span class="n">testing</span> <span class="n">accuracy</span> <span class="n">of</span> <span class="mf">92.3076923076923</span><span class="o">%</span>
</code></pre>
</div>
<p>We have just created our own Fake News Detection Model which has an accuracy of 92%!</p>
<div class="codehilite">
<pre><span></span><code><span class="n">example_text</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"title"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Middling ‘Rise Of Skywalker’ Review Leaves Fan On Fence About Whether To Threaten To Kill Critic"</span><span class="p">],</span> <span class="s2">"text"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Expressing ambivalence toward the relatively balanced appraisal of the film, Star Wars fan Miles Ariely admitted Thursday that an online publication’s middling review of The Rise Of Skywalker had left him on the fence about whether he would still threaten to kill the critic who wrote it. “I’m really of two minds about this, because on the one hand, he said the new movie fails to live up to the original trilogy, which makes me at least want to throw a brick through his window with a note telling him to watch his back,” said Ariely, confirming he had already drafted an eight-page-long death threat to Stan Corimer of the website Screen-On Time, but had not yet decided whether to post it to the reviewer’s Facebook page. “On the other hand, though, he commended J.J. Abrams’ skillful pacing and faithfulness to George Lucas’ vision, which makes me wonder if I should just call the whole thing off. Now, I really don’t feel like camping outside his house for hours. Maybe I could go with a response that’s somewhere in between, like, threatening to kill his dog but not everyone in his whole family? I don’t know. This is a tough one.” At press time, sources reported that Ariely had resolved to wear his Ewok costume while he murdered the critic in his sleep."</span><span class="p">]}</span>
<span class="n">example_prediction</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">classify</span><span class="p">(</span><span class="n">tc</span><span class="o">.</span><span class="n">SFrame</span><span class="p">(</span><span class="n">example_text</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">example_prediction</span><span class="p">,</span> <span class="n">flush</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre>
</div>
<div class="codehilite">
<pre><span></span><code><span class="o">+-------+--------------------+</span>
<span class="o">|</span> <span class="k">class</span> <span class="err">| </span><span class="nc">probability</span> <span class="o">|</span>
<span class="o">+-------+--------------------+</span>
<span class="o">|</span> <span class="n">FAKE</span> <span class="o">|</span> <span class="mf">0.9245648658345308</span> <span class="o">|</span>
<span class="o">+-------+--------------------+</span>
<span class="p">[</span><span class="mi">1</span> <span class="n">rows</span> <span class="n">x</span> <span class="mi">2</span> <span class="n">columns</span><span class="p">]</span>
</code></pre>
</div>
<h3 id="exporting-the-model">Exporting the Model</h3>
<div class="codehilite">
<pre><span></span><code><span class="n">model_name</span> <span class="o">=</span> <span class="s1">'FakeNews'</span>
<span class="n">coreml_model_name</span> <span class="o">=</span> <span class="n">model_name</span> <span class="o">+</span> <span class="s1">'.mlmodel'</span>
<span class="n">exportedModel</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">export_coreml</span><span class="p">(</span><span class="n">coreml_model_name</span><span class="p">)</span>
</code></pre>
</div>
<p><strong>Note: To download files from Google Colab, simply click on the files section in the sidebar, right click on filename and then click on download</strong></p>
<p><a rel="noopener" target="_blank" href="https://colab.research.google.com/drive/1onMXGkhA__X2aOFdsoVL-6HQBsWQhOP4">Link to Colab Notebook</a></p>
<h2 id="building-the-app-using-swiftui">Building the App using SwiftUI</h2>
<h3 id="initial-setup">Initial Setup</h3>
<p>First we create a single view app (make sure you check the use SwiftUI button)</p>
<p>Then we copy our .mlmodel file to our project (Just drag and drop the file in the XCode Files Sidebar)</p>
<p>Our ML Model does not take a string directly as an input, rather it takes bag of words as an input.
DescriptionThe bag-of-words model is a simplifying representation used in NLP, in this text is represented as a bag of words, without any regard for grammar or order, but noting multiplicity</p>
<p>We define our bag of words function</p>
<div class="codehilite">
<pre><span></span><code><span class="kd">func</span> <span class="nf">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="nb">String</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nv">bagOfWords</span> <span class="p">=</span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]()</span>
<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NSLinguisticTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">tokenType</span><span class="p">],</span> <span class="n">options</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">range</span> <span class="p">=</span> <span class="n">NSRange</span><span class="p">(</span><span class="n">location</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">length</span><span class="p">:</span> <span class="n">text</span><span class="p">.</span><span class="n">utf16</span><span class="p">.</span><span class="bp">count</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">options</span><span class="p">:</span> <span class="bp">NSLinguisticTagger</span><span class="p">.</span><span class="n">Options</span> <span class="p">=</span> <span class="p">[.</span><span class="n">omitPunctuation</span><span class="p">,</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">]</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">text</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">range</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span> <span class="n">scheme</span><span class="p">:</span> <span class="p">.</span><span class="n">tokenType</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">options</span><span class="p">)</span> <span class="p">{</span> <span class="kc">_</span><span class="p">,</span> <span class="n">tokenRange</span><span class="p">,</span> <span class="kc">_</span> <span class="k">in</span>
<span class="kd">let</span> <span class="nv">word</span> <span class="p">=</span> <span class="p">(</span><span class="n">text</span> <span class="k">as</span> <span class="bp">NSString</span><span class="p">).</span><span class="n">substring</span><span class="p">(</span><span class="n">with</span><span class="p">:</span> <span class="n">tokenRange</span><span class="p">)</span>
<span class="k">if</span> <span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span><span class="o">!</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="p">=</span> <span class="mi">1</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">bagOfWords</span>
<span class="p">}</span>
</code></pre>
</div>
<p>We also declare our variables</p>
<div class="codehilite">
<pre><span></span><code><span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">title</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">headline</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertTitle</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertText</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">showingAlert</span> <span class="p">=</span> <span class="kc">false</span>
</code></pre>
</div>
<p>Finally, we implement a simple function which reads the two text fields, creates their bag of words representation and displays an alert with the appropriate result</p>
<p><strong>Complete Code</strong></p>
<div class="codehilite">
<pre><span></span><code><span class="kd">import</span> <span class="nc">SwiftUI</span>
<span class="kd">struct</span> <span class="nc">ContentView</span><span class="p">:</span> <span class="n">View</span> <span class="p">{</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">title</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">headline</span><span class="p">:</span> <span class="nb">String</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertTitle</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">alertText</span> <span class="p">=</span> <span class="s">""</span>
<span class="p">@</span><span class="n">State</span> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">showingAlert</span> <span class="p">=</span> <span class="kc">false</span>
<span class="kd">var</span> <span class="nv">body</span><span class="p">:</span> <span class="n">some</span> <span class="n">View</span> <span class="p">{</span>
<span class="n">NavigationView</span> <span class="p">{</span>
<span class="n">VStack</span><span class="p">(</span><span class="n">alignment</span><span class="p">:</span> <span class="p">.</span><span class="n">leading</span><span class="p">)</span> <span class="p">{</span>
<span class="n">Text</span><span class="p">(</span><span class="s">"Headline"</span><span class="p">).</span><span class="n">font</span><span class="p">(.</span><span class="n">headline</span><span class="p">)</span>
<span class="n">TextField</span><span class="p">(</span><span class="s">"Please Enter Headline"</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="err">$</span><span class="n">title</span><span class="p">)</span>
<span class="p">.</span><span class="n">lineLimit</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
<span class="n">Text</span><span class="p">(</span><span class="s">"Body"</span><span class="p">).</span><span class="n">font</span><span class="p">(.</span><span class="n">headline</span><span class="p">)</span>
<span class="n">TextField</span><span class="p">(</span><span class="s">"Please Enter the content"</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="err">$</span><span class="n">headline</span><span class="p">)</span>
<span class="p">.</span><span class="n">lineLimit</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">.</span><span class="n">navigationBarTitle</span><span class="p">(</span><span class="s">"Fake News Checker"</span><span class="p">)</span>
<span class="p">.</span><span class="n">navigationBarItems</span><span class="p">(</span><span class="n">trailing</span><span class="p">:</span>
<span class="n">Button</span><span class="p">(</span><span class="n">action</span><span class="p">:</span> <span class="n">classifyFakeNews</span><span class="p">)</span> <span class="p">{</span>
<span class="n">Text</span><span class="p">(</span><span class="s">"Check"</span><span class="p">)</span>
<span class="p">})</span>
<span class="p">.</span><span class="n">padding</span><span class="p">()</span>
<span class="p">.</span><span class="n">alert</span><span class="p">(</span><span class="n">isPresented</span><span class="p">:</span> <span class="err">$</span><span class="n">showingAlert</span><span class="p">){</span>
<span class="n">Alert</span><span class="p">(</span><span class="n">title</span><span class="p">:</span> <span class="n">Text</span><span class="p">(</span><span class="n">alertTitle</span><span class="p">),</span> <span class="n">message</span><span class="p">:</span> <span class="n">Text</span><span class="p">(</span><span class="n">alertText</span><span class="p">),</span> <span class="n">dismissButton</span><span class="p">:</span> <span class="p">.</span><span class="k">default</span><span class="p">(</span><span class="n">Text</span><span class="p">(</span><span class="s">"OK"</span><span class="p">)))</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">classifyFakeNews</span><span class="p">(){</span>
<span class="kd">let</span> <span class="nv">model</span> <span class="p">=</span> <span class="n">FakeNews</span><span class="p">()</span>
<span class="kd">let</span> <span class="nv">myTitle</span> <span class="p">=</span> <span class="n">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="n">title</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">myText</span> <span class="p">=</span> <span class="n">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="n">headline</span><span class="p">)</span>
<span class="k">do</span> <span class="p">{</span>
<span class="kd">let</span> <span class="nv">prediction</span> <span class="p">=</span> <span class="k">try</span> <span class="n">model</span><span class="p">.</span><span class="n">prediction</span><span class="p">(</span><span class="n">title</span><span class="p">:</span> <span class="n">myTitle</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="n">myText</span><span class="p">)</span>
<span class="n">alertTitle</span> <span class="p">=</span> <span class="n">prediction</span><span class="p">.</span><span class="n">label</span>
<span class="n">alertText</span> <span class="p">=</span> <span class="s">"It is likely that this piece of news is </span><span class="si">\(</span><span class="n">prediction</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">lowercased</span><span class="si">())</span><span class="s">."</span>
<span class="bp">print</span><span class="p">(</span><span class="n">alertText</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">{</span>
<span class="n">alertTitle</span> <span class="p">=</span> <span class="s">"Error"</span>
<span class="n">alertText</span> <span class="p">=</span> <span class="s">"Sorry, could not classify if the input news was fake or not."</span>
<span class="p">}</span>
<span class="n">showingAlert</span> <span class="p">=</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">bow</span><span class="p">(</span><span class="n">text</span><span class="p">:</span> <span class="nb">String</span><span class="p">)</span> <span class="p">-></span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nv">bagOfWords</span> <span class="p">=</span> <span class="p">[</span><span class="nb">String</span><span class="p">:</span> <span class="nb">Double</span><span class="p">]()</span>
<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NSLinguisticTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">tokenType</span><span class="p">],</span> <span class="n">options</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">range</span> <span class="p">=</span> <span class="n">NSRange</span><span class="p">(</span><span class="n">location</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">length</span><span class="p">:</span> <span class="n">text</span><span class="p">.</span><span class="n">utf16</span><span class="p">.</span><span class="bp">count</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">options</span><span class="p">:</span> <span class="bp">NSLinguisticTagger</span><span class="p">.</span><span class="n">Options</span> <span class="p">=</span> <span class="p">[.</span><span class="n">omitPunctuation</span><span class="p">,</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">]</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">text</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">range</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span> <span class="n">scheme</span><span class="p">:</span> <span class="p">.</span><span class="n">tokenType</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">options</span><span class="p">)</span> <span class="p">{</span> <span class="kc">_</span><span class="p">,</span> <span class="n">tokenRange</span><span class="p">,</span> <span class="kc">_</span> <span class="k">in</span>
<span class="kd">let</span> <span class="nv">word</span> <span class="p">=</span> <span class="p">(</span><span class="n">text</span> <span class="k">as</span> <span class="bp">NSString</span><span class="p">).</span><span class="n">substring</span><span class="p">(</span><span class="n">with</span><span class="p">:</span> <span class="n">tokenRange</span><span class="p">)</span>
<span class="k">if</span> <span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span><span class="o">!</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">bagOfWords</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="p">=</span> <span class="mi">1</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">bagOfWords</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">struct</span> <span class="nc">ContentView_Previews</span><span class="p">:</span> <span class="n">PreviewProvider</span> <span class="p">{</span>
<span class="kd">static</span> <span class="kd">var</span> <span class="nv">previews</span><span class="p">:</span> <span class="n">some</span> <span class="n">View</span> <span class="p">{</span>
<span class="n">ContentView</span><span class="p">()</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
<script data-isso="https://comments.navan.dev/"
src="https://comments.navan.dev/js/embed.min.js"></script>
<section id="isso-thread">
<noscript>Javascript needs to be activated to view comments.</noscript>
</section>
</main>
<script src="assets/manup.min.js"></script>
<script src="/pwabuilder-sw-register.js"></script>
</body>
</html>
|