fastText-php is a PHP bindings for fastText.
fastText is a library for efficient learning of word representations and sentence classification.
PHP 7.x
fastText shard object
$ git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ mkdir build && cd build && cmake ..
$ make
$ sudo make install
$ cd fastText-php
$ phpize
$ ./configure
$ make
$ sudo make install
edit your php.ini and add:
extension=fasttext.so
fastText {
public __construct ( void )
public int load ( string filename )
public int getWordRows ( void )
public int getLabelRows ( void )
public int getWordId ( string word )
public string getWord ( int word_id )
public string getLabel ( int label_id )
public array getWordVectors ( string word )
public array getSentenceVectors ( string sentence )
public mixed getPredict ( streing word [, int k] )
public mixed getNN ( streing word [, int k] )
public mixed getAnalogies ( streing word [, int k] )
public mixed getNgramVectors ( streing word )
public string lastErrorMsg ( void )
}
fastText::__construct
fastText::load
fastText::getWordRows
fastText::getLabelRows
fastText::getWordId
fastText::getWord
fastText::getLabel
fastText::getWordVectors
fastText::getSentenceVectors
fastText::getPredict
fastText::getNN
fastText::getAnalogies
fastText::getNgramVectors
fastText::lastErrorMsg
Instantiates a fastText object.
$ftext = new fastText();
load a model.
$model = 'result/model.bin';
$ftext->load($model);
get the number of vocabularies.
$rows = $ftext->getWordRows();
$words = [];
for ($idx = 0; $idx < $rows; $idx++) {
$words[$idx] = $ftext->getWord($idx);
}
get the number of labels.
$rows = $ftext->getLabelRows();
$labels = [];
for ($idx = 0; $idx < $rows; $idx++) {
$labels[$idx] = $ftext->getLabel($idx);
}
get the word ID within the dictionary.
$word = 'Bern';
$rowId = $ftext->getWordId($word);
converts a ID into a word.
$rows = $ftext->getWordRows();
$words = [];
for ($idx = 0; $idx < $rows; $idx++) {
$words[$idx] = $ftext->getWord($idx);
}
converts a ID into a label.
$rows = $ftext->getLabelRows();
$labels = [];
for ($idx = 0; $idx < $rows; $idx++) {
$labels[$idx] = $ftext->getLabel($idx);
}
get the vector representation of word.
$vectors = $ftext->getWordVectors('Beijing');
print_r($vectors);
get the vector representation of sentence.
$sentence = "It's fine day";
$vectors = $ftext->getSentenceVectors($sentence);
print_r($vectors);
- array fastText::getPredict(string word)
- FALSE fastText::getPredict(string word)
predict most likely labels with probabilities.
$probs = $ftext->getPredict('Berlin');
foreach ($probs as $row) {
echo $row['label'].' '.$row['prob'];
}
- array fastText::getNN(string word)
- FALSE fastText::getNN(string word)
query for nearest neighbors.
$probs = $ftext->getNN('Washington, D.C.');
foreach ($probs as $row) {
echo $row['label'].' '.$row['prob'];
}
- array fastText::getAnalogies(string word)
- FALSE fastText::getAnalogies(string word)
query for analogies.
$probs = $ftext->getAnalogies('Paris + France - Spain');
foreach ($probs as $row) {
echo $row['label'].' '.$row['prob'];
}
- array fastText::getNgramVectors(string word)
- FALSE fastText::getNgramVectors(string word)
get the ngram vectors.
$res = $ftext->getNgramVectors('London');
print_r($res);
get the latest error message.
$probs = $ftext->getNN('Tokyo');
if (FALSE === $probs) {
echo $ftext->lastErrorMsg();
}
print_r($probs);
$probs =
[
['label'=> '__label__1', 'prob'=> 0.4234 ],
['label'=> '__label__2', 'prob'=> 0.2345 ],
['label'=> '__label__3', 'prob'=> 0.1456 ],
:
:
:
]