(Android) Article classifier in Python
For the soon to start newsletter
How did I get to this?
Machine learning is the thing right now, up there with Augmented Reality, Virtual Reality and cryptocurencies. I can’t stay out of it! I wanted to make something small to get me going and hype my interest in the field. I can’t stand coding without purpose so I invented one: a tool that would make me check very fast if a link is an Android resource or (spam) not.
The usecase
You get a random link from whoever, you paste it in the Python script and then the script will answer 1, if it’s about Android or 0 otherwise. Pretty simple, right? Of course I might develop this into some other product with CoreML or Tensor Flow but for now, it’s an “internal” tool.
Note 1: know your tools. I will NOT tell you how to install Python, SKLearn or BeautifulSoup. You handle that and come back when you did.
Note 2: I have almost zero knowledge about Neural Networks for now so I can’t explain the theory behind Multinomial Naive Bayes. The purpose was to help me understand what it does and see it’s power.
How does it work?
- You need a CSV file with links to resources about Android (or something else) and resources that are not about the same topic
- Write a Python function that opens that CSV file and reads row by row the links and loads them
- Request the webpage and use BeautifulSoup to easily extract the text and links.
- Create a “bag of words” for each resource (article) and then normalise it.
5. Train the Naive Bayes classifier with the data
Let’s dig right into writing stuff, following the steps above:
Here are 10 links that I’ve classified for you. In order to get good accuracy for the classifier you should add some more links. Diversity is the key here. For each link, you will add 1 if it’s an Android tutorial or 0 otherwise.
Get Matei Șuică’s stories in your inbox
Join Medium for free to get updates from this writer.
Save these links in a CSV file, let’s call it “data.csv”.
Open a text editor line Atom or Sublime (something that heard of Python for highlighting the syntax). Start a new file and save it in the same folder as your “data.csv”.
Here’s the function that will open “data.csv”, read it and save the links in a variable:
Then, we need a function to actually load the content of each link:
The ‘hdr’ are headers that are sent to the webserver so it will think we are using a legitimate browser.
And then we need to parse the HTML and extract the text from it. We consider ‘text’ anything that is in H1-H5 tags, <p> and <a>:
The ‘main’ function of this script will loop through the links we extracted from the CSV, load the webpage content and parse it. Then it will add everything to a ‘final_data’ array. This is where all the input data is.
We will also create a ‘target’ array that will contain the label of each link (zero or one):
Now the ‘intelligent’ part of everything. First of all we need to put our input data in order. For each article we will create a ‘bag of words’, meaning that we will count each word appearance in the article. Then we will normalise this data: we divide the number of appearances to the number of total words. This way, longer articles won’t always be in advantage. Then we feed the “learning monster”.
This is very easily created with a SKLearn Pipeline. It has all the components we need to create our “bag of words” and feed it directly to training in a couple of lines of code:
Great! Now “text_clf” is ready to make some predictions. To do this:
My answer was ironically “zero”. So it means that my personal website is not about Android. Might be, check it out! But this answer will depend on your training data set. The more classified links you provide to your algorithm, the better classification you will get.






