Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract names from text
<textarea id="tu" cols="90" rows="10">Your text goes here </textarea>
<input id="btget" class="mybutton" type="button" value="Go Extract Info"/>
<div id="imgdiv" ></div>
<script>
function init(){
var contentanalysisURL="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20contentanalysis.analyze%20where%20text%3D%22{text}%22&format=json&callback=?";
//this function will be called on json recieve
function receiveJSON(data){
//6. recieve Data
try{
//7.get list of all entities
var et=data.query.results.entities.entity;
var out=[];
var len=et.length;
for(var i=0;i<len;i++){
var eee=et[i];
//8.get entity name
var content=eee.text.content;
var startChar=eee.text.start;
var etypes="";
if(eee.types){
//9.get entity type
var typ=eee.types.type;
for(var j=0;j<typ.length;j++){
var ttt=typ[j].content;
if(ttt.indexOf('/')>=0){
etypes=etypes+" "+ ttt.substring(ttt.lastIndexOf('/')+1);
}
}
}
//10.Append to dumo array
out[out.length]={
"Entity":content,
"startChar":startChar,
"etypes":etypes,
};
}
//11.update container with output
$('#imgdiv').html("<pre>"+JSON.stringify(out,null,'\t')+"</pre>");
}catch(e){
$('#imgdiv').html("<pre>error</pre>");
}
}
//1. hook click event of btget
$('#btget').click(function (){
//2. Get text, length of text should be less than 512 or less.
//Note:when text is too large this will not work
var text=$('#tu').val();
//3.Update login mesage
$('#imgdiv').html("<pre>Loading..........</pre>");
//4.Form a GET request, (YQL table )
var url=contentanalysisURL.replace("{text}",text);
//5. inject script block in dom with callback as receiveJSON
$.getJSON(url,receiveJSON);
});
}
$(document).ready(init);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.