24.07.2016 Views

www.allitebooks.com

Learning%20Data%20Mining%20with%20Python

Learning%20Data%20Mining%20with%20Python

SHOW MORE
SHOW LESS

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 Python (the<br />

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

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

pure Python in IPython 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 IPython 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 with the<br />

following line at the top:<br />

%%javascript<br />

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

Don't worry, we will get back to Python 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 Python code from JavaScript in IPython 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 IPython kernel as a JavaScript object and give it a Python <strong>com</strong>mand to execute.<br />

The code is as follows:<br />

function set_label(label){<br />

var kernel = IPython.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 IPython<br />

kernel and give it a <strong>com</strong>mand 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!