コード例 #1
0
ファイル: WaveletTree.php プロジェクト: mskec/WaveletTree
 /**
  * Method creates node
  * @param $string
  * @param $left
  *
  * @return array|null
  */
 public function createNode($string, $left)
 {
     if ($string == null || $string == '') {
         return null;
     }
     $alphabet = WaveletTree::getAlphabet($string);
     $isLeaf = false;
     if (count($alphabet) == 1 || count($alphabet) == 2) {
         $isLeaf = true;
     }
     $letters = array_keys($alphabet);
     usort($letters, "WaveletTree::sortLetters");
     //set half of letters to 0 half to 1
     $dictionary = [];
     for ($i = 0; $i < count($letters) / 2; $i++) {
         $dictionary[$letters[$i]] = 0;
     }
     for ($i = count($letters) / 2; $i < count($letters); $i++) {
         $dictionary[$letters[$i]] = 1;
     }
     $result = [];
     for ($i = 0; $i < strlen($string); $i++) {
         $result[$i] = $dictionary[$string[$i]];
     }
     //translate dictionary to node strings
     return [new Node($result, $isLeaf, $left, $left + count($dictionary) - 1), $dictionary];
 }
コード例 #2
0
<?php

include 'db.php';
include 'queries.php';
include 'classes.php';
if (isset($_POST['keyword'])) {
    $keyword = $_POST['keyword'];
    $keys = explode(' ', $keyword);
    /* create dictionary starts*/
    $t_create_start = microtime(true);
    $qobj = new Queries();
    $wtree = new WaveletTree();
    $wtreeop = new WaveletOperation();
    $content = array();
    $contentlist = array();
    $docidlist = array();
    $doclist = $qobj->retrieve_documents($conn);
    while ($row = $doclist->fetch()) {
        echo $row['doc_id'] . " => ";
        echo $row['content'];
        $content = explode(' ', $row['content']);
        array_push($docidlist, $row['doc_id']);
        array_push($contentlist, $content);
        echo "<br>";
    }
    $collection = array_combine($docidlist, $contentlist);
    $dictionary = array();
    // create dictionary list
    foreach ($collection as $doc => $content) {
        foreach ($content as $i => $word) {
            $word = strtolower($word);