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 />

For this cell, we will be coding in HTML and a little JavaScript. First, define a div<br />

element to store our current tweet to be labeled. I've also added some instructions for<br />

using this form. Then, create the #tweet_text div that will store the text of the next<br />

tweet to be labeled. As stated before, we need to create a textbox to be able to capture<br />

key presses. The code is as follows:<br />

<br />

Instructions: Click in textbox. Enter a 1 if the tweet is<br />

relevant, enter 0 otherwise.<br />

Tweet: <br />

<br />

<br />

Don't run the cell just yet!<br />

We create the JavaScript for capturing the key presses. This has to be defined<br />

after creating the form, as the #tweet_text div doesn't exist until the above<br />

code runs. We use the JQuery library (which IPython is already using, so we don't<br />

need to include the JavaScript file) to add a function that is called when key presses<br />

are made on the #capture textbox we defined. However, keep in mind that this is<br />

a %%html cell and not a JavaScript cell, so we need to enclose this JavaScript in the<br />

tags.<br />

We are only interested in key presses if the user presses the 0 or the 1, in which case<br />

the relevant label is added. We can determine which key was pressed by the ASCII<br />

value stored in e.which. If the user presses 0 or 1, we append the label and clear out<br />

the textbox. The code is as follows:<br />

<br />

$("input#capture").keypress(function(e) {<br />

if(e.which == 48) {<br />

set_label(0);<br />

$("input#capture").val("");<br />

}else if (e.which == 49){<br />

set_label(1);<br />

$("input#capture").val("");<br />

}<br />

});<br />

All other key presses are ignored.<br />

[ 113 ]

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

Saved successfully!

Ooh no, something went wrong!