PHP Classes

File: example.npredict.php

Recommend this page to a friend!
  Classes of JImmy Bo   PHP NGram Predictor   example.npredict.php   Download  
File: example.npredict.php
Role: Example script
Content type: text/plain
Description: example of using the NGramPredictor class
Class: PHP NGram Predictor
Predict the next characters based on the previous
Author: By
Last change: accessibility
Date: 1 year ago
Size: 1,674 bytes
 

Contents

Class file image Download
<?php
   
// example of using the NGramPredictor class
    // shows:
    // 1. how to train the predictor
    // 2. how to predict the next character
    // 3. how to get all matches for a given string
    // 4. how to get the next character for a given string
    // 5. how to get the previous character for a given string (TODO)
   

   
require_once("class.npredict.php");

   
$predictor = new NGramPredictor(2);
   
$predictor->train("Hello my name is Fred and I like to enjoy life. Life is one hell of a ride. Likewise, naturally, I like to jump. Martha helped the donkey open the gate while tying her shoe.");

   
$str = "el";
    for (
$i = 0; $i < 10; $i++) {
       
$char = $predictor->predict($str);
       
$str .= $char;
        print(
"c:".$char."<br />\n");
        print(
"s:".$str."<br />\n");
    }

    echo
"\r\n<br /><br />";

   
$match_str = "el";
    print(
"match_str:".$match_str."<br />\n");
   
// now show the matching dict for the matches
   
foreach ($predictor->predict_get_all_matches($match_str) as $key => $val) {
       
// decode key
       
$decoded = $predictor->decode([$key]);
        print(
"key:".$key." val:".$val." decoded:".$decoded."<br />\n");
    }


   
/*
    Output:
        c:l
        s:ell
        c:o
        s:ello
        c:
        s:ello
        c:m
        s:ello m
        c:y
        s:ello my
        c:
        s:ello my
        c:n
        s:ello my n
        c:a
        s:ello my na
        c:m
        s:ello my nam
        c:e
        s:ello my name


        match_str:el
        key:2 val:2 decoded:l
        key:25 val:1 decoded:p
    */
?>