10.11.2016 Views

Learning Data Mining with Python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6<br />

Next, we create a simple function that will return the next tweet that needs to be<br />

labeled. We can work out which is the next tweet by finding the first one that hasn't<br />

yet been labeled. The code is as follows:<br />

def get_next_tweet():<br />

return tweet_sample[len(labels)]['text']<br />

The next step in our experiment is to collect information from<br />

the user (you!) on which tweets are referring to <strong>Python</strong> (the<br />

programming language) and which are not. As of yet, there is<br />

not a good, straightforward way to get interactive feedback <strong>with</strong><br />

pure <strong>Python</strong> in I<strong>Python</strong> Notebooks. For this reason, we will use<br />

some JavaScript and HTML to get this input from the user.<br />

Next we create some JavaScript in the I<strong>Python</strong> Notebook to run our input.<br />

Notebooks allow us to use magic functions to embed HTML and JavaScript<br />

(among other things) directly into the Notebook itself. Start a new cell <strong>with</strong> the<br />

following line at the top:<br />

%%javascript<br />

The code in here will be in JavaScript, hence the curly braces that are coming up.<br />

Don't worry, we will get back to <strong>Python</strong> soon. Keep in mind here that the following<br />

code must be in the same cell as the %%javascript magic function.<br />

The first function we will define in JavaScript shows how easy it is to talk to<br />

your <strong>Python</strong> code from JavaScript in I<strong>Python</strong> Notebooks. This function, if called,<br />

will add a label to the labels array (which is in python code). To do this, we load<br />

the I<strong>Python</strong> kernel as a JavaScript object and give it a <strong>Python</strong> command to execute.<br />

The code is as follows:<br />

function set_label(label){<br />

var kernel = I<strong>Python</strong>.notebook.kernel;<br />

kernel.execute("labels.append(" + label + ")");<br />

load_next_tweet();<br />

}<br />

At the end of that function, we call the load_next_tweet function. This function<br />

loads the next tweet to be labeled. It runs on the same principle; we load the I<strong>Python</strong><br />

kernel and give it a command to execute (calling the get_next_tweet function we<br />

defined earlier).<br />

[ 111 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!