Code Display

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/php -q

<?php

//About: Adds dictionary lists to Metaphone Database and Searches Metaphone Database for Similar Words 

//Usage: ./metaphone.php -a <path_to_dictinary>
//       ./metaphone.php -s <searchterm>

//Written by: droops@gmail.com
//          nomicon.info


////////////////////////////////////////////
//     Define Database Information
////////////////////////////////////////////

DEFINE ('DB_USER''');
DEFINE ('DB_PASSWORD''');
DEFINE ('DB_HOST''');
DEFINE ('DB_NAME''');

////////////////////////////////////////////
//        Database SQL File
///////////////////////////////////////////

/*
CREATE TABLE `metaphones` (
  `id` int(10) NOT NULL auto_increment,
  `word` varchar(20) NOT NULL,
  `meta` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `word` (`word`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
*/


///////////////////////////////////////////
//    Tread Carefully Below this Line
///////////////////////////////////////////


if ($dbc = @mysql_connect (DB_HOSTDB_USERDB_PASSWORD)) { 

        if (!
mysql_select_db (DB_NAME)) { 

                echo 
'\nCannot connect to Database\n';
                exit();
        } 
}

$arg1 $_SERVER['argv'][1];

if (
$arg1 == "-s" or $arg1 == "-a"){
    
$startime date('U');
    
$rows "0";
    
$processed "0";
    
$exists_count "8";
    
    if (
$arg1 == "-a"){
    
        
$file $_SERVER['argv'][2];
        
$handle = @fopen($file"r");
    
        while ((
$data fgetcsv($handle1000" ")) !== FALSE) {
                
$processed++;
                    
$word $data[0];
                
$meta_word metaphone($word,20);
    
                
$query_add "INSERT INTO metaphones VALUES ('NULL', '$word', '$meta_word')";
                
$result mysql_query$query_add );
            if (
$result) {
                        
$rows++;
                echo 
"$word - $meta_word\n";
            } else {
                
                switch(
$exists_count) {
                    case 
8:
                        
$image "|";
                        
$exists_count "1";
                                            break;
                                    case 
7:
                                            
$image "/";
                                            
$exists_count++;
                                            break;
                                    case 
6:
                                            
$image "-";
                                            
$exists_count++;
                                            break;
                                    case 
5:
                                            
$image "\\";
                                            
$exists_count++;
                                            break;
                                    case 
4:
                                            
$image "|";
                                            
$exists_count++;
                                            break;
                                    case 
3:
                                            
$image "/";
                                            
$exists_count++;
                                            break;
                                    case 
2:
                                            
$image "-";
                                            
$exists_count++;
                                            break;
                                    case 
1:
                                            
$image "\\";
                                            
$exists_count++;
                        break;
                }
    
                echo 
"already exists $image \n";
            }
        }
    
        
$endtime date('U');
        
        
$time $endtime $startime;
    
        print 
"\n\n\tProcessed $processed added $rows words in $time seconds\n\n";
    
    }
    
    if (
$arg1 == "-s"){
    
        
$word $_SERVER['argv'][2];
    
        
$meta_word metaphone($word,20);
    
        echo 
"\nSearchterm: $word - $meta_word\n";
    
        
$query "SELECT word FROM metaphones WHERE meta = '$meta_word' ORDER BY id ASC";
    
        if(
$r mysql_query($query)) {
                while(
$row mysql_fetch_array($r)) {
                    
$rows++;
                    
$word $row['word'];
                    echo 
"\n$word";
        
                }
        }
    
        
$total_words_count mysql_query("SELECT id FROM metaphones");
        
$totalwords mysql_num_rows($total_words_count);
    
        
$endtime date('U');
    
        
$time $endtime $startime;
    
        print 
"\n\n\tFound $rows out of $totalwords possible results in $time seconds \n\n";
    
    }
    
    
} else {

        echo 
"\nAbout:\tAdds dictionary lists to Metaphone Database and Searches Metaphone Database for Similar Words \n\nUsage:\n\t./metaphone.php -a <path_to_dictinary>\n\t./metaphone.php -s <searchterm>\n\n";
        echo 
"Written by: droops@gmail.com\n\n";
        exit;    
}
?>