Professional Documents
Culture Documents
Omar M’Haimdat
Jun 5, 2020 · 9 min read
Since the beginning of the coronavirus pandemic, the news has been overwhelmingly
negative and somewhat depressing. You open Twitter or your favorite news
application, and you’re left with loads and loads of information that can drain your
energy.
In a world saturated with information, more and more people are choosing to offer
positive news, even if it’s still often confined to a specific section.
For example, The British daily newspaper The Guardian offers “The Upside”, while Fox
News, MSN, the HuffPost site, and Yahoo! all have “good news” pages.
Figure 1: Google trends for “good news” (red) and “positive news” (blue) in the USA
The most surprising lesson of this crisis is probably the need for people to access ‘good’
news. Google trends data shows a spike of interesting queries (Figure 1) such as good
The data shows that this is also the case in multiple regions (Europe, India…).
Figure 2: Google trends for “good news” (red) and “positive news” (blue) Worldwide
After seeing this, I decided that I might be able to choose what kinds of information to
consume in order to break the chain of negativity. I’m a developer so I thought, why not
create an application that’ll select only positive news and remove negative and neutral
ones for me?
With recent advances in natural language processing (NLP) and mobile processing, we
can select, with a certain level of accuracy, the most positive news and create our own
custom feeds.
In this article, I’ll create an iOS application that will consume an API and select the
most positive articles.
Overview
1. Find the right news API
I have included code in this article where it’s most instructive. Full code and data can
be found on my GitHub page. Let’s get started.
omarmhaimdat/positive_newsfeed
How You Can Effortlessly Create Your Own Positive News iOS
Application The project consists of an iOS application that…
github.com
I chose to get top and breaking news headlines from the United States curated in an
API called “Top headlines from the United States”, but you can choose whatever topic
interests you.
You do need to create an account to get an apiKey , and the rest is pretty
straightforward. You choose the “News sources” and the subcategory, and the website
will then generate a GET URL with the appropriate key and news sources.
You can easily test your link in your browser and understand the structure of the API
call:
Figure 4: API request status
error : Indicates whether the request was successful or not—“ok” for a successful
Now we have our project ready to go. I don’t like using storyboards myself, so the app
in this tutorial is built programmatically, which means no buttons or switches to toggle
— just pure code.
To follow this method, you’ll have to delete the main.storyboard file and set your
SceneDelegate.swift file (Xcode 11 only).
With Xcode 11, you’ll have to change the Info.plist file like so:
Figure 7: delete the Storyboard Name from the .plist file
You need to delete the “Storyboard Name” in the file, and that’s about it.
SceneDelegate.swift
hosted with ❤ by GitHub view raw
ViewController() :
This is where we’ll set our UICollectionView with all the articles retrieved from the
news API.
DetailsViewController() :
This is where we will show the article’s body.
Create a navigation
MainTabBarController() :
This is our main navigation, where we’ll create the full navigation structure for our
application.
Navigation in iOS is pretty straightforward and easy to implement. Here, I’ve changed
some things like the color and icon for the navigation bar (and others) so it looks nice.
The API returns a series of elements that could be parsed using two important
structures. The first one will mimic the API call upper node with a status ,
totalResult , and finally articles , which is an array of articles. The other one will
abstract a representation of an article, and thus will be used to create the array of
articles.
Both structures conform to the Decodable protocol. And the former is using the first
one to create an array of Article . Make sure that the properties' names are the exact
Create a UICollectionView :
The news feed will have a stream of articles, we will use UICollectionView() that will
be instantiated in ViewController() with the following code:
Then, we need to register the cells and set the data source, as well as set up the
ViewController() to conform to the UICollectionView() protocol:
Finally, we need to add the UICollectionView to the subview and set up the layout:
The collection is made up of cells that are members of the feed, think of it as Medium’s
homepage, UICollectionViewCell is an instance of an article, and the homepage is the
UICollectionView — each cell will have a title, author name and will also lead to the
full article. Thus, the homepage is a ‘Collection’ of articles.
I’m using a package made by Paolo Cuscela called Cards that mimics the iOS app store.
For the sake of simplicity, I manually included the files in the project and created a new
Class that conforms to the CardDelegate protocol. The Class is also included in the
GitHub repository.
First, we need to instantiate an object of type CardArticle which will be the design
structure of the cell
Create an initializer where we initialize the Card and the Article object
Create a function that will set up the Card’s bounds and attributes (title, author,
background color, text color and many more)
Finally, we need to override an important function that gets invoked every time a
cell is reused by the UICollectionView() . This function exists to improve the
efficiency and the performance of the feed. Thus, when a cell is not visible on the
screen, the collection view dequeues it and remove it from the stack.
Since we only want positive text, we’ll only surface articles that have a score strictly
above 0.
Run inference by calling the method tag() , specifying that the text is a paragraph
Test the output and set the final score if the text is positive ( 1 ), negative ( 0 ) or
neutral ( -1 ).
Before loading the CollectionView , we’ll run inference on each and every article’s text.
Create your own API by scraping and parsing your own favorite news outlet, which
will help you customize your feed even more.
Create your own custom NLP model using state-of-the-art architectures such as
Transformer GPT-2 or BERT. In these cases, the model will perform the inference
on the server-side rather than on-device. I wrote an article that can help you get
started with sentiment analysis using Transformers.
Add a section for the most important and breaking news articles in order to stay up
to date.
The Internet is full of positive news, you just need to search harder on the various news
sites and you will find good news. By using your coding skills you can leverage the
power of ML and programming and totally automate the process. The news feed can
also cover subjects that you deem important, as long as the news fits your own
expectations.
Thanks for reading this article. If you have any questions, don’t hesitate to send me an
email at omarmhaimdat@gmail.com.
omarmhaimdat/positive_newsfeed
How You Can Effortlessly Create Your Own Positive News iOS
Application The project consists of an iOS application that…
github.com
If you’d like to contribute, head on over to our call for contributors. You can also sign up
to receive our weekly newsletters (Deep Learning Weekly and the Fritz AI Newsletter),
join us on Slack, and follow Fritz AI on Twitter for all the latest in mobile machine
learning.