PHP Classes

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

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-04-30 (11 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 35 This week: 1All time: 10,933 This week: 560Up
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: 1209173 in United States United States
Week rank: 20 Up3 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  
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

 Version Control Unique User Downloads Download Rankings  
 0%
Total:35
This week:1
All time:10,933
This week:560Up