示例#1
0
var_dump(nanomacro("hi\${0}hi, _fn\$(hai)\$(hey)", ["fn" => "This is argument 1: \${0}, and 2: \${1}, and 3: \${2}"], 4));
echo "took " . (microtime(true) - $stime) . " seconds";
/**/
echo "<hr>";
$stime = microtime(true);
$syntax = nanomacro("{This} {is} {_of\$(Aristo)\$(house)}.", ["of" => "(\${0}{$OP_APOS}s \${1}|\${1} of \${0})", "a" => "[a[n]|the]"], 4);
var_dump($syntax);
var_dump(compare_syntax($syntax, "This is Aristos house", ["unescaped" => true]));
echo "took " . (microtime(true) - $stime) . " seconds";
/**/
echo "<hr>";
$stime = microtime(true);
$syntax = nanomacro('{*Agricola} {_cum$({he|Agricola} {had arrived})} {_imp$(want) to (say|speak)} {_a few (words|things)}.', ['cum' => ', when ${0},', 'a' => '[a[n]|the]', 'imp' => '(was ${0}ing|${0}ed)'], 4);
var_dump($syntax);
var_dump(compare_syntax($syntax, "Agricola, when he had arrived, wanted to say a few things", ["unescaped" => true]));
var_dump(compare_syntax($syntax, "When he had arrived, Agricola was wanting to say a few words", ["unescaped" => true]));
echo "took " . (microtime(true) - $stime) . " seconds";
/**/
echo "<hr>";
$stime = microtime(true);
$sentence = " , When he said {$OP_LQUOTE}Hello?{$OP_RQUOTE}, they were silent. , {$OP_LQUOTE}Hello{$OP_RQUOTE}, he said. {$OP_LQUOTE}What happened?{$OP_RQUOTE}. He repeated, {$OP_LQUOTE}Hello{$OP_RQUOTE}. {$OP_LQUOTE}What happened?{$OP_RQUOTE}";
var_dump($sentence);
var_dump(lexify_punctuation($sentence));
var_dump(normalize_punctuation($sentence));
echo "took " . (microtime(true) - $stime) . " seconds";
/**/
// centuriō, iuvenem cōnspicātus, “hunc agnōscō!” inquit.
// kenturioo, iuvenem koonspikaatus, "hunk agnooskoo!" inquit.
echo "<hr>";
$stime = microtime(true);
$syntax = '{*_a centurion} {_perfactv$(centurion|he)$(caught sight of)$(_a young man)} {said} {_quot$(I recognize (this [man|guy]|him))}.';
示例#2
0
function compare_syntax3($syntax, $target, $dictionary = null, $matchall = false, $distance = 0, $lang = NULL)
{
    if ($dictionary === null) {
        $dictionary = nano_dfdict();
    }
    $syntax = nanomacro($syntax, $dictionary, 4);
    $match = compare_syntax($syntax, $target, ["unescaped" => true, "matchall" => $matchall, "max_distance" => $distance, "lang" => $lang]);
    return $match ? normalize_punctuation($match) : $match;
}
<p>The true innovation over, say, regular expressions, is the permutation opportunities with curly braces. This allows sentences in languages, such as Latin, Ancient Greek, and Russian, which have a more flexible word order due to their nominal inflections, to be easily expressed. Even in English, dependent clauses and other phrases can sometimes move around, coming before or after their subject.

<p>These expressions are only meant to work against an input string to match. They essentially provide a (finite) set of alternatives, from which the one nearest to the input will be picked. The punctuation and syntax will come from the expression, but the content and form from the input.

<p>A few exempla to illustrate just the pattern matching:

<ul>
<?php 
foreach ([['{*this }{that}', 'this that'], ['{*this }{that}', 'that this'], ['This ([wo]man|person) [here]', 'this man'], ['This ([wo]man|person) [here]', 'this woman'], ['This ([wo]man|person) [here]', 'this person here'], ['{*thou} {knowest} {not} {me}.', 'me thou knowest not'], ['{*venistine} {ex foro} {[tu]}?', 'uenistine·tu·ex·foro'], ['{*[a[n]|the] centurion} {, (having (caught sight of) ([a[n]|the] young man)|who had (caught sight of) ([a[n]|the] young man)),} {said} {, “I recognize (this [man|guy]|him)!”,}.', 'thecenturionhavingcaughtsightoftheyoungmansaidirecognizehim']] as $example) {
    ?>
<li><?php 
    echo2($example[0]);
    echo "matches";
    echo2($example[1]);
    echo "creating";
    echo2(compare_syntax($example[0], $example[1], ["unescaped" => true]));
    ?>
<br><?php 
}
?>
</ul>

<p>You can see why we need to normalize the punctuation (make it look pretty) and use macros (simplify our expression-writing needs). Let's see how it all works:
<ul>
<?php 
foreach ([['{*_a centurion} {_perfactv$(caught sight of)$(_a young man)} {said} {_quot$(I recognize (this [man|guy]|him)!)}.', 'thecenturionhavingcaughtsightoftheyoungmansaidirecognizehim'], ['{*{, {himself} {_a young man}, } {_a centurion}} {smiled|was smiling}.', 'the centurion a young man himself was smiling'], ['{*{, {himself} {_a young man}, } {_a centurion}} {smiled|was smiling}.', 'a young man himself the centurion was smiling'], ['_opts$(*thou} {not} {me)$(dost} {know)$(knowest).', 'thou dost not know me'], ['_opts$(*thou} {not} {me)$(dost} {know)$(knowest).', 'thou dost know me not'], ['_opts$(*thou} {not} {me)$(dost} {know)$(knowest).', 'thou not me knowest'], ['_Appos$(_a centurion)$(having seen _a child) {wept} _quot$({*have mercy} {on me} {,O deity,}).', 'the centurion having seen the child wept on me have mercy O deity']] as $example) {
    ?>
<li><?php 
    echo2($example[0]);
    echo "makes this syntax";
    echo2(nanomacro($example[0], $dict, 4));