-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi0_getsuggestClass.php
200 lines (192 loc) · 6.09 KB
/
api0_getsuggestClass.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
// Exclude WARNING messages also, to solve Peter Scharf Mac version.
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
?>
<?php
//api0_getsuggestClass.php 08-16-2019
require_once('utilities/transcoder.php'); // initializes transcoder
require_once("dbgprint.php");
require_once("dal.php");
require_once('parm.php');
/* Computes public variable $result, which is an associative array with keys:
status: 200 (ok) or 404 (problem)
errorinfo: empty string if ok, error message string if problem
request: associative array of parameters used:
dict : the dictionary code
term : the partial word to match in the dictionary. uses 'input' spelling
input : (optional) Default is slp1. Otherwise one of the recognized
spelling systems: slp1, hk, itrans, deva, iast (or roman)
termslp1 : term, in slp1 spelling.
nmatches: number of matches found for term in dictionary
matches : Array of strings, consisting of matching headwords.
Results are spelled in same transcoding as input.
*/
class api0_GetsuggestClass {
public $result;
public $matches; // needed?
public $xParm; // associative array of extra data needed from request
public function __construct() {
// initialize result
$this->init_result();
if ($this->result['status'] != 200) {
return; // do no more
}
$this->suggestions1();
}
public function suggestions1() { // second version
// Unpack some local variables
$getParms = $this->xParm['getParms'];
$keyin = $this->xParm['keyin'];
$keyin1 = $this->xParm['keyin1'];
$termslp1 = $this->xParm['termslp1'];
$english = $this->xParm['english'];
$filterin = $this->xParm['filterin'];
$dbg=false;
$origkey = $key;
$dict = $getParms->dict;
dbgprint($dbg,"getsuggestClass: call Dal($dict)\n");
$dal = new Dal($dict);
$maxlike=200000;
$max = $maxlike;
$matches=array();
$nmatches=0;
dbgprint($dbg,"getsuggestClass. call dal_get3c: $termslp1, $maxlike\n");
$results1 = $dal->get3c($termslp1,$maxlike);
dbgprint($dbg,"dal_get3c: $termslp1, $maxlike, nresults1=".count($results1)."\n");
$keys1 = $results1;
/*
$results=array();
foreach($results1 as $result){
$results[]=$result;
}
$keys1 = array();
foreach($results as $line) {
#list($key1,$lnum1,$data1) = $line;
$key1=$line;
if(in_array($key1,$keys1)) {continue;} // skip duplicates
$keys1[] = $key1;
#echo "adding key1=$key1<br/>";
}
*/
if(!$english) {
// sort $keys1 in Sanskrit order
usort($keys1,'api0_GetsuggestClass::slp_cmp');
}
// Pick first $max in $keys
foreach($keys1 as $key1) {
$matches[]=$key1;
$nmatches++;
if ($nmatches==$max) {
break;
}
}
if(!$english) {
// transcode back from slp1 to filterin
for($i=0;$i<$nmatches;$i++) {
$matches[$i] = transcoder_processString($matches[$i],"slp1",$filterin);
}
}
$this->result['nmatches'] = $nmatches;
$this->result['matches'] = $matches;
}
public function suggestions() { // first version
// Unpack some local variables
$getParms = $this->xParm['getParms'];
$keyin = $this->xParm['keyin'];
$keyin1 = $this->xParm['keyin1'];
$termslp1 = $this->xParm['termslp1'];
$english = $this->xParm['english'];
$filterin = $this->xParm['filterin'];
$dbg=false;
$origkey = $key;
$dict = $getParms->dict;
dbgprint($dbg,"getsuggestClass: call Dal($dict)\n");
$dal = new Dal($dict);
$maxlike=10000;
$max = $maxlike;
$matches=array();
$nmatches=0;
dbgprint($dbg,"getsuggestClass. call dal_get3a: $termslp1, $maxlike\n");
$results1 = $dal->get3a($termslp1,$maxlike);
dbgprint($dbg,"dal_get3a: $termslp1, $maxlike, nresults1=".count($results1)."\n");
$results=array();
foreach($results1 as $result){
$results[]=$result;
}
$keys1 = array();
foreach($results as $line) {
list($key1,$lnum1,$data1) = $line;
if(in_array($key1,$keys1)) {continue;} // skip duplicates
$keys1[] = $key1;
#echo "adding key1=$key1<br/>";
}
if(!$english) {
// sort $keys1 in Sanskrit order
usort($keys1,'api0_GetsuggestClass::slp_cmp');
}
// Pick first $max in $keys
foreach($keys1 as $key1) {
$matches[]=$key1;
$nmatches++;
if ($nmatches==$max) {
break;
}
}
if(!$english) {
// transcode back from slp1 to filterin
for($i=0;$i<$nmatches;$i++) {
$matches[$i] = transcoder_processString($matches[$i],"slp1",$filterin);
}
}
$this->result['nmatches'] = $nmatches;
$this->result['matches'] = $matches;
}
public function init_result() {
$result = array();
$result['status'] = 200; // assume no error
$result['errorinfo'] = ''; // assume no error
$result['nmatches'] = 0;
$result['matches'] = array();
$this->result = $result;
$this->result['request'] = $this->init_request();
}
public function term_error($term) {
$this->result['status'] = 404;
$this->result['errorinfo'] = "getsuggest term error: $term";
}
public function init_request() {
// initialize $this->result['request'] and $this->xParm;
$xParm = array();
$getParms = new Parm();
list($xParm['keyin'],$xParm['keyin1'],$xParm['termslp1']) =
$getParms->getsuggestParms();
$xParm['getParms'] = $getParms;
$xParm['english'] = $getParms->english;
$xParm['filterin'] = $getParms->filterin;
$term = $getParms->getsuggestTerm;
$request = array();
$request['dict'] = $getParms->dict;
$request['term'] = $term;
$request['termslp1'] = $xParm['termslp1'];
$request['input'] = $getParms->filterin;
if ($getParms->status != 200) {
$this->result['status'] = $getParms->status;
$this->result['errorinfo'] = $getParms->errorinfo;
}else if ($term == '') {
$this->term_error($term);
}
$this->xParm = $xParm;
return $request;
}
public function slp_cmp($a,$b) {
// $a, $b are strings in SLP1 coding of Sanskrit. Return -1,0,1 according to
// whether $a<$b, $a==$b, or $a>$b
// order per PMS (Sep 25, 2012): L after q, | after Q
$from = "aAiIuUfFxXeEoOMHkKgGNcCjJYwWqLQ|RtTdDnpPbBmyrlvSzsh";
$to = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy";
$a1 = strtr($a,$from,$to);
$b1 = strtr($b,$from,$to);
return strcmp($a1,$b1);
}
}
?>