问题
Im trying to tag full name as a complete tag(one person) instead of individual tags. Here is an example.
http://nlp.stanford.edu:8080/ner/process (Stanford NER online)
Example sentence: Muhammad Ali was a great boxer. Ali's greatest rival was Joe Frazier. The name can also be written as M. Ali and J. Frazier.
This is my existing PHP code`
$text = "Muhammad Ali was a great boxer. Ali's greatest rival was Joe Frazier. The name can also be written as M. Ali and J. Frazier";
$pos = new \StanfordNLP\NERTagger(
'XPATH/NER/StanfordNLP/stanford-ner-2013-11-12/classifiers/english.conll.4class.distsim.crf.ser.gz',
'XPATH/NER/StanfordNLP/stanford-ner-2013-11-12/stanford-ner.jar'
);
$result = $pos->tag(explode(' ', " $text"));
foreach ($result as $eType)
{
if(!(strcmp($eType[1], 'PERSON')))
{
echo "Word ".$eType[0]." of Stanford entity type PERSON</br>";
}
}`
回答1:
Nevermind, I was able to solve it on my own. Basically, I focused on combining words if the previous word was also of the entity type person. This is the code that I came up with
<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
require "./php_aho_corasick-master/AhoCorasickPHP-master/AhoCorasick.php";
require "./php_aho_corasick-master/AhoCorasickPHP-master/TreeNodes.php";
include_once('AlchemyAPI/alchemyapi.php');
include_once('TextStatistics/TextStatistics.php');
require './NER/StanfordNLP/Base.php';
require './NER/StanfordNLP/Exception.php';
require './NER/StanfordNLP/Parser.php';
require './NER/StanfordNLP/StanfordTagger.php';
require './NER/StanfordNLP/NERTagger.php';
$text= "Muhammad Ali was a great boxer, Ali's greatest rival was Joe Frazier, The name can also be written as M. Ali and J. Frazier.";
$pos = new \StanfordNLP\NERTagger(
'C:/wamp/www/GoogleResultsParserTopK/NER/StanfordNLP/stanford-ner-2013-11-12/classifiers/english.conll.4class.distsim.crf.ser.gz',
'C:/wamp/www/GoogleResultsParserTopK/NER/StanfordNLP/stanford-ner-2013-11-12/stanford-ner.jar'
);
$a="Answer not found";
//$pos->setJavaPath('C:/Program Files/Java/jdk1.7.0_45/bin');
$result = $pos->tag(explode(' ', " $text"));
var_dump($result);
$previousValue="";
$previousType="";
$FullName="";
$i=0;
foreach ($result as $eType) {
echo $i." ".$FullName."</br>";
$i++;
if(!(strcmp($eType[1], 'PERSON')))
{
if(!(strcmp($previousType, 'PERSON')) && !(strcmp($FullName, "")))
{
$FullName=$previousValue." ".$eType[0];
}
else if(!(strcmp($previousType, 'PERSON')) && (strcmp($FullName, "")))
{
$FullName=$FullName." ".$eType[0];
}
else if(!(strcmp($a, "Answer not found")) && !(strcmp($FullName, "")))
$FullName=$eType[0];
else if((strcmp($FullName, "")))
$FullName=$FullName." or ".$eType[0];
}
$previousValue=$eType[0];
$previousType=$eType[1];
}
echo $FullName;
?>
来源:https://stackoverflow.com/questions/25842982/tagging-full-name-in-stanford-ner