NER: example in PHP with TransformersPHP
Token classification pipeline with Xenova/bert-base-NER
This example uses the TransformersPHP token-classification pipeline to detect entities in text and output token-to-label pairs.
Example of use
<?php
include 'code-en.php';
if ($nerError !== null) {
echo 'Warning: ' . $nerError . PHP_EOL;
return;
}
echo $text;
echo PHP_EOL . '-----------' . PHP_EOL;
if ($entities === []) {
echo 'No entities found.' . PHP_EOL;
return;
}
foreach ($entities as $entity) {
echo ($entity['word'] ?? '') . ' -> ' . ($entity['entity'] ?? '') . PHP_EOL;
}
Result:
Memory: 0 Mb
Time running: < 0.001 sec.
Microsoft signed a contract with John Smith in London for $3 million.
-----------
Microsoft -> B-ORG
John -> B-PER
Smith -> I-PER
London -> B-LOC