/
Dictionary.php
59 lines (49 loc) · 1.21 KB
/
Dictionary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
require_once("TrieTree.php");
class Dictionary{
public $TrieTable = array();
//try it out first with a text file
function Dictionary(){
$File = fopen("largedictionary.txt","r");
$initLine = " ";
if(!$File)
print("Could Not Open File largedictionary.txt");
else{
/*
I know this is a lot of preprocessing but it
does help with organizing this. this will get
the size of the largest word.
*/
while(!feof($File)){
$line = fgets($File);
if(strlen($initLine) < strlen($line))
$initLine = $line;
}
fclose($File);
//size up the Table of Trie trees
for($i = 0; $i < strlen($initLine)+1;$i++){
$TrieTable[] = new Trie();
}
$File = fopen("largedictionary.txt","r");
//start inserting words
while(!feof($File)){
insert(fgets($File));
}
}
}
function insert($word){
$TrieTable[strlen($word)].insert($word);
}
/*Find the Shortest Path.*/
function WordLadderCompute($StringX, $StringY){
//if string sizes do not match then you can not
//compute the word ladder
if(strlen($StringX) == strlen($StringY)){
print("Incorrect/Mismach word sizes");
return;
}
else
$TrieTable[strlen($StringX)].findPath($StringX, $StringY);
}
}
?>