forked from samwilson/Wikparser
/
wikparser.php
executable file
·148 lines (139 loc) · 6.07 KB
/
wikparser.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
<?php
/***********************************************************************************/
// Wikitionary Text Parser 0.3a
// Author: Yves Bourque
// Go to http://www.igrec.ca/projects/ for full instructions.
/***********************************************************************************/
header('Content-type: text/html; charset=utf-8');
/***********************************************************************************/
// Set variables. Valid values as follows:
// *word: any string
// *query: 'pos', 'def', 'syn', 'hyper', 'gender'
// lang: 'en', 'fr', 'es', 'de' -- other codes as set in the config file
// count: any number > 0
// source: 'local' for local wiki mysql database, 'api' for Wiktionary's api (slow)
/***********************************************************************************/
// search word
@$word = $_GET['word']
or die("ERROR: You must specify a search string. Append ?word=WORD
to the end of the URL.");
/***********************************************************************************/
// query type; 'pos' for part of speech, 'def' for definition. Validated in case
// below.
@$query = $_GET['query']
or die("ERROR: You must specify a query type. Append &query=QUERY TYPE
('pos','def', 'syn' or 'hyper') to the end of the URL.");
/***********************************************************************************/
// Language code for search, default english (en)
if (isset($_GET['lang'])) {
$langCode = $_GET['lang'];
}
else {
$langCode = "en";
}
/***********************************************************************************/
// Number of results; default '100'
if (isset($_GET['count'])) {
$count = $_GET['count'];
$count = intval($count);
if ($count < 1 || $count > 100) {
$count = 100;
}
}
else {
$count = 100;
}
/***********************************************************************************/
// Set wikisource to local if not set. Values either 'local' or 'api'.
if (isset($_GET['source'])) {
if ($_GET['source'] == 'api' || $_GET['source'] == 'local') {
$wikiSource = $_GET['source'];
}
else {
$wikiSource = 'api';
}
}
else {
$wikiSource = 'api';
}
/***********************************************************************************/
// Create $langParameters variable using the values defined in config file.
include './language.config.php';
/***********************************************************************************/
switch ($query) {
/***********************************************************************************/
// Include defparse class and create new object with 3 variables.
/***********************************************************************************/
case "def":
include 'classes/class.defparse.php';
$DefParse = new DefParse($langParameters);
$wikitext = get_wiki_text($langParameters, $wikiSource, $word);
$defArray = $DefParse->getDef($wikitext, $count);
printResults($defArray);
break;
/***********************************************************************************/
// Include posparse class and create new object with 3 variables.
/***********************************************************************************/
case "pos":
include 'classes/class.posparse.php';
$posparse = new PosParse($langParameters);
$wikitext = get_wiki_text($langParameters, $wikiSource, $word);
$posArray = $posparse->get_pos($wikitext, $count);
printResults($posArray);
break;
/***********************************************************************************/
// Include synparse class and create new object with 3 variables.
/***********************************************************************************/
case "syn":
include 'classes/class.synparse.php';
$SynParse = new SynParse($langParameters);
$wikitext = get_wiki_text($langParameters, $wikiSource, $word);
$synArray = $SynParse->get_syn($wikitext, $count);
printResults($synArray);
break;
/***********************************************************************************/
// Include hyperparse class and create new object with 3 variables. (Hypernyms)
/***********************************************************************************/
case "hyper":
include 'classes/class.hyperparse.php';
$HyperParse = new HyperParse($langParameters);
$wikitext = get_wiki_text($langParameters, $wikiSource, $word);
$hyperArray = $HyperParse->get_hyper($wikitext, $count);
printResults($hyperArray);
break;
/***********************************************************************************/
// Include genderparse class and create new object with 3 variables. (Gender)
/***********************************************************************************/
case "gender":
include 'classes/class.genderparse.php';
$GenderParse = new GenderParse($langParameters);
$wikitext = get_wiki_text($langParameters, $wikiSource, $word);
$genderArray = $GenderParse->get_gender($wikitext, $count);
printResults($genderArray);
break;
/***********************************************************************************/
default:
echo "You must specify a valid query type ('pos', 'def', 'syn', 'hyper', or 'gender').";
break;
}
/***********************************************************************************/
// Include wikiextract class and create new object with 2 variables. Returns the
// contents of the wiktionary entry for a given word.
/***********************************************************************************/
function get_wiki_text($langParameters, $wikiSource, $word) {
include 'classes/class.wikiextract.php';
$WikiExtract = new WikiExtract($langParameters, $wikiSource);
return $WikiExtract->get_wikitext($word);
}
/***********************************************************************************/
// Prints contents of array.
/***********************************************************************************/
function printResults($array) {
$resultseparator = " | ";
$printresults = null;
foreach ($array as $value) {
$printresults .= $value.$resultseparator;
}
echo rtrim($printresults, $resultseparator);
}
?>