Monday, December 22, 2014

NamedEntity via Stanford NER with PhP

Here is a snipped to get NER for a given sentence using PhP and powered by Stanford NER http://nlp.stanford.edu/software/CRF-NER.shtml

Step0: Download Stanford-NER in above URL and unzip in a location of your choice
Step1: Download PHP-Stanford-NLP-master.zip from https://github.com/agentile/PHP-Stanford-NLP
Step2: Unzip PHP-Stanford-NLP-master.zip under ~/Sites/
Step3: Store below code test_NER.php under ~/Sites/
Step4: Open browser and provide URL as localhost/~user/

#Code to perform Named Entity using StanfordNamedEntity Recogniser
#Author Hota S

require './PHP-Stanford-NLP-master/src/StanfordNLP/Base.php';
require './PHP-Stanford-NLP-master/src/StanfordNLP/Exception.php';
require './PHP-Stanford-NLP-master/src/StanfordNLP/Parser.php';
require './PHP-Stanford-NLP-master/src/StanfordNLP/StanfordTagger.php';
require './PHP-Stanford-NLP-master/src/StanfordNLP/NERTagger.php';
require './PHP-Stanford-NLP-master/src/StanfordNLP/POSTagger.php';

$pos = new \StanfordNLP\NERTagger('/Users/XXXX/Downloads/stanford-ner-2014-10-26/classifiers/english.all.3class.distsim.crf.ser.gz','/Users/XXXX/Downloads/stanford-ner-2014-10-26/stanford-ner-3.5.0.jar');

$a_str = "U.S. stocks rose in a generally quiet session, sending the Dow Jones Industrial Average and S&P 500 to fresh record highs, their 35th and 50th of the year, respectively, and setting up the Dow just a stone’s throw from the 18000 mark.Meanwhile, crude oil resumed its slide after a brief respite.WTI crude fell more than 3%, logging in its second-worst close this year.The Federal Reserve Bank of New York led by Timothy R. Geithner.";

$result = $pos -> tag(explode(' ', $a_str));
$output="";
$max = sizeof($result);
for ($row = 0; $row<$max;$row++) {
   for ($col = 0; $col < 2; $col++) {
    $output = $output."_";
    $output = $output.$result[$row][$col];
  }
$output=$output. " "; 
}

$output = str_replace(" _"," ",$output);
$output = substr($output, 1);
echo $output."\n";

#var_dump($result);
?>

###########OUTPUT###################

U.S._LOCATION stocks_O rose_O in_O a_O generally_O quiet_O session_O ,_O sending_O the_O Dow_ORGANIZATION Jones_ORGANIZATION Industrial_O Average_O and_O S&P_ORGANIZATION 500_O to_O fresh_O record_O highs_O ,_O their_O 35th_O and_O 50th_O of_O the_O year_O ,_O respectively_O ,_O and_O setting_O up_O the_O Dow_O just_O a_O stone_O 's_O throw_O from_O the_O 18000_O mark.Meanwhile_O ,_O crude_O oil_O resumed_O its_O slide_O after_O a_O brief_O respite.WTI_O crude_O fell_O more_O than_O 3_O %_O ,_O logging_O in_O its_O second-worst_O close_O this_O year.The_O Federal_ORGANIZATION Reserve_ORGANIZATION Bank_ORGANIZATION of_ORGANIZATION New_ORGANIZATION York_ORGANIZATION led_O by_O Timothy_PERSON R._PERSON Geithner_PERSON ._O