Git Product home page Git Product logo

fasttext-php's Introduction

fastText-php

fastText-php is a PHP bindings for fastText.

fastText is a library for efficient learning of word representations and sentence classification.

Requirements

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

Building fastText for PHP

$ cd fastText-php
$ phpize
$ ./configure
$ make
$ sudo make install

edit your php.ini and add:

extension=fasttext.so

Class synopsis

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 )
}

Table of Contents

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

return value format


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 ],
                        :
                        :
                        :
]

fasttext-php's People

Contributors

yujirotakahashi avatar microaijp avatar

Watchers

James Cloos avatar  avatar  avatar Kazumasa Nishikawa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.