Exemplo n.º 1
0
 /**
  * Returns the first tree that can be parsed from $words, given a $Grammar.
  */
 public static function getFirstTree(Grammar $Grammar, array $words)
 {
     $Parser = new EarleyParser($Grammar, $words, true);
     $Parser->parseWords();
     $tree = $Parser->extractFirstTree();
     return $tree;
 }
<?php

/**
 * @author Patrick van Bergen
 */
use earley\parser\EarleyParser;
use earley\reader\GrammarReader;
use earley\parser\SentenceTokenizer;
require_once __DIR__ . '/autoloader.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
// sanitize input
$ruleHead = isset($_POST['rule-head']) ? $_POST['rule-head'] : [];
$ruleBody = isset($_POST['rule-body']) ? $_POST['rule-body'] : [];
$sentence = isset($_POST['sentence']) ? $_POST['sentence'] : '';
$grammarReader = new GrammarReader();
$sentenceTokenizer = new SentenceTokenizer();
// create a grammar from user input
$grammar = $grammarReader->readGrammar($ruleHead, $ruleBody);
// create a word array from user input
$words = $sentenceTokenizer->tokenize($sentence);
// parse sentence using Earley algorithm
$parser = new EarleyParser($grammar, $words, false);
$trees = $parser->getAllTrees();
# test: output raw results
print_r($trees);