Monday, February 15, 2016

Building Microsoft’s What-Dog AI in under 100 Lines of Code

Rather recently, Microsoft released an app using AI to detect a dog’s breed. When I tested it on my beagle, though…

The app identifies a beagle as a Saluki

Hmm, not quite, app. Not quite.

In my non-SitePoint time, I also work for Diffbot - the startup you may have heard of over the past few weeks - who also dabble in AI. To test how they compare, in this tutorial we’ll recreate Microsoft’s application using Diffbot’s technology to see if it does a better job at recognizing the adorable beasts we throw at it!

We’ll build a very primitive single-file “app” for uploading images and outputting the information about the breed under the form.

Prerequisites

If you’d like to follow along, please register for a free 14-day token at Diffbot.com, if you don’t have an account there yet.

To install the client, we use the following composer.json file:

{
    "require": {
        "swader/diffbot-php-client": "^2",
        "php-http/guzzle6-adapter": "^1.0"
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require-dev": {
        "symfony/var-dumper": "^3.0"
    }
}

Then, we run composer install.

The minimum stability flag is there because a part of the Puli package is still in beta, and it’s a dependency of the PHP HTTP project now. The prefer stable directive is there to make sure the highest stable version of a package is used if available. We also need an HTTP client, and in this case I opted for Guzzle6, though the Diffbot PHP client supports any modern HTTP client via Httplug, so feel free to use your own favorite.

Once these items have been installed, we can create an index.php file, which will contain all of our application’s logic. But first, bootstrapping:

<?php

require 'vendor/autoload.php';

$token = 'my_token';

The Upload

Let’s build a primitive upload form above the PHP content of our index.php file.

<form action="/" method="post" enctype="multipart/form-data">
    <h2>Please either paste in a link to the image, or upload the image directly.</h2>
    <h3>URL</h3>
    <input type="text" name="url" id="url" placeholder="Image URL">
    <h3>Upload</h3>
    <input type="file" name="file" id="file">
    <input type="submit" value="Analyze">
</form>

<?php

...

We’re focusing on the PHP side only here, so we’ll leave out the CSS. I apologize to your eyes.

Ugly form

We’ll be using Imgur to host the images, so that we don’t have to host the application in order to make the calls to Diffbot (the images will be public even if our app isn’t, saving us hosting costs). Let’s first register an application on Imgur via this link:

Imgur registration

This will produce a client ID and a secret, though we’ll only be using the client ID (anonymous uploads), so we should add it to our file:

$token = 'my_token';
$imgur_client = 'client';

Analyzing the Images

So, how will the analysis happen, anyway?

As described in the docs, Diffbot’s Image API can accept a URL and then scans the page for images. All found images are additionally analyzed and some data is returned about them.

The data we need are the tags Diffbot attaches to the image entries. tags is an array of JSON objects, each of which contains a tag label, and a link to http://dbpedia.org for the related resource. We won’t be needing these links in this tutorial, but we will be looking into them in a later piece. The tags array takes a form similar to this:

"tags": [
        {
          "id": 4368,
          "label": "Beagle",
          "uri": "http://ift.tt/1oh5DtU"
        },
        {
          "id": 2370241,
          "label": "Treeing Walker Coonhound",
          "uri": "http://ift.tt/1oE6sgJ"
        }
      ]

As you can see, each tag has the aforementioned values. If there’s only one tag, only one object will be present. By default, Diffbot returns up to 5 tags per entry - so each image can have up to 5 tags, and they don’t have to be directly related (e.g. submitting an image of a running shoe might return both the tag Nike and the tag shoe).

It is these tag labels we’ll be using as suggested guesses of dog breeds. Once the request goes through and returns the tags in the response, we’ll print the suggested labels below the image.

Continue reading %Building Microsoft’s What-Dog AI in under 100 Lines of Code%


by Bruno Skvorc via SitePoint

How to Get Started With Push Notifications On Android

The How and Why of Property-Based Testing in Ruby

Think about the last time you wrote unit tests (which hopefully is pretty recent). You had to come up with the happy-path, the tragic-path, and those hard-to-find edge cases. Since we are not infallible human beings, we tend to miss things more often than not. We miss an edge case here, forget about handling an error there, and discover our omissions during production.

Property-based testing totally flips the notion of unit testing on its head. Instead of writing specific examples of a test, you would write a property instead. A property, in this context, signifies a condition that would hold for all inputs that you define.

An example here is certainly helpful, if not required. Say you want to test a function that reverses an array. What could be some useful properties? Well, I can think of the following:

  • The first element of the array becomes the last element
  • The length of the array remains the same before and after reversing
  • Reversing the array twice will give back the original array

You could probably come up with a few more. With a property-based testing tool, you could express the above as code and tell it to generate hundreds or even thousands of test cases with randomly generated arrays.

A cool feature of many property-based testing tools is shrinking. This means that the tool, to the best of its ability, will find the smallest input that causes the property to be unsatisfied.

Continue reading %The How and Why of Property-Based Testing in Ruby%


by Benjamin Tan Wei Hao via SitePoint

Master Microsoft Word, Excel, and Powerpoint for $49

Master Microsoft Word, Excel, and Powerpoint for $49

Microsoft Excel, PowerPoint, and Word are more than just bullet points you stick on your resume and claim you’re proficient at. They’re powerful tools that, when mastered, can help you work more efficiently, save time, and take your projects (and career) to the next level. Master Microsoft Office with the GoSkills Microsoft Office bundle at SitePoint Shop—it’s now just $49, a savings of 94%.

This bundle contains five pro-taught courses, all of which come with certification. Take your Excel skills from “what does this button do?” to expert as you learn how to organize data, create charts, and format PivotTables. Learn how to format text, customize page layouts, and create any kind of document with Word. And polish your presentations with themes, layouts, and audio features in PowerPoint. Courses include easy-to-follow video tutorials, quick-reference cheat sheets, and short quizzes that ensure you’re on track.

Boost your Microsoft Office know-how and your career. Get the GoSkills Microsoft Office bundle for $49.

Continue reading %Master Microsoft Word, Excel, and Powerpoint for $49%


by SitePoint Offers via SitePoint

A Smooth Refresher on Python's Conditional Statements

Life is about taking the right decisions, and our programs are not exempt from that. Well, in the end we are programming something for some aspects of our lives. We thus should expect the issue of making decisions in our programs.

Here is where conditional statements come into play. They help us make such decisions by the logic control of our programs. The conditional statements in Python are: if, elif, and else.

But, what does the conditional statement do? It simply checks whether a statement (test) is true or false, based on which decision is carried out.

Branching Programs

Unlike straight-line programs where the statements are executed in the order in which they appear, branching programs allow us to navigate to statements regardless of the order, but based on the decision. The conditional statements we mentioned above are considered to be of this type of programs, provided that if a conditional statement has been executed, program execution continues at the code following the conditional statement.

Conditional Statement Structure 

In this section I will describe the different parts a conditional statement is composed of. A conditional statement basically consists of the following main parts:

  • a test that evaluates to either true or false
  • a block of code that runs if the test is true
  • an optional block of code if the test is false

The conditional statement in Python thus looks as follows:

where test is a boolean expression, that is, an expression which evaluates to either true or false. In other words, conditional statements allow us to check the truthfulness of some statement. Let's see a simple example of the above structure:

What is the output of this code snippet? Go ahead, give it a try.

At this point, I think I should mention quickly about indentation (spaces), which Python uses as opposed to braces in other languages like C. In the above script, what would happen if you wrote the last print statement on the same level under the print statement above (the else code)? In other words, if we wrote it as follows:

In this case, print 'That\'s it!' will be part of the else code block.

This is just a quick example of indentation, which Python relies on extensively in its syntax. When you program in Python, you should expect errors like this to pop up occasionally when there is an issue with your indentation:

IndentationError: expected an indented block 

Nested Conditional Statements

The conditional statement is said to be nested if the true block of code or false block of code (i.e. else) contains another conditional statement. Let's see an example of that:

We here have a nested conditional statement since the first if statement contains another if statement inside it. Notice again how the code is indented. This is very critical when programming in Python.

Compound Boolean Expressions

As you remember, we have mentioned that the test in the conditional statement structure is a boolean expression. Sometimes you may need more than one boolean expression in the same test, and this is what we call compound boolean expressions

Let's take an example that finds the smallest number of three numbers. Notice that the first test is a compound boolean expression.

In this example we use the third conditional statement for the first time in this tutorial, that is elif, which refers to else if.

We have also used a boolean operation called and, which means that all the statements have to be true in order for the following statement to run. Python's boolean operations can be summarized in the following table:

Boolean operation Description
or the following statement runs if any expression is true
and all the expressions need to be true for the following statement to run
not the expression is false if it evaluates to true, and vice versa

If you had a statement (test) with a mix of those operations, the order of priority is as follows: or runs first, then and, then not.

Let's take another example that shows how we can use boolean operations with lists:

See how we used not in this example? As you know, for the code block in the if statement to run, the statement (boolean expression) should evaluate to true. The statement originally evaluates to false in our casesince 13 does not belong to the list. If you run the program, however, you will notice that the print statement is being run. How did that happen? This happened since we used the not operator, which inverts the original evaluation value. In our case, it inverted false to true.

Python and Switch?

I know you may have been wondering till now about when a switch example will come along. Maybe if you learned the conditional statements in another programming language, you were confronted with examples showing the beauty of using switch statements. The reason I haven't mentioned examples of such statements is because Python does not have switch statements!

For more information about Python's conditions, you can refer to the documentation.


by Abder-Rahman Ali via Envato Tuts+ Code

An Introduction to Web MIDI

Sublime

sublime

Lovely subtle parallax effect with good whitespace in this One Pager for 'Sublime' - a well curated list of talks by opinion leaders.

by Rob Hope via One Page Love