PHP Classes

PHP NGram Predictor: Predict the next characters based on the previous

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 37 All time: 10,957 This week: 104Up
Version License PHP version Categories
ngrampredictor 1.0BSD License7Searching, Text processing, Artificia..., P..., P...
Description 

Author

This package can predict the following characters based on the previous ones.

It can be trained with sentences with valid words to learn the most common words users may type when asking questions.

The package can parse a sequence of characters a user may have typed and predict the following letters based on what it learned during the training phase.

It uses an N-Gram model, where the probability of each character is calculated based on the previous N characters.

The class includes methods for converting text to integers, creating a dictionary of character sequences, finding the previous(todo) and following characters based on a given sequence, and predicting the most likely next character based on a given sequence.

Picture of JImmy Bo
  Performance   Level  
Name: JImmy Bo is available for providing paid consulting. Contact JImmy Bo .
Classes: 14 packages by
Country: United States United States
Age: ???
All time rank: 1198173 in United States United States
Week rank: 41 Up6 in United States United States Up
Innovation award
Innovation award
Nominee: 8x

Winner: 1x

Example

<?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
    */
?>


  Files folder image Files (2)  
File Role Description
Plain text file class.npredict.php Class PHP Class for N-Gram Prediction
Accessible without login Plain text file example.npredict.php Example example of using the NGramPredictor class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:37
This week:0
All time:10,957
This week:104Up