-
Notifications
You must be signed in to change notification settings - Fork 1
/
WBA.php
183 lines (160 loc) · 9.07 KB
/
WBA.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
<?php
/*******************************************************************************
* WBA.php
* year : 2014
*
* The WBA algorithm is based on identifying the language using the common words
* of each language, where it consists of computing the sum of the word
* frequencies of each language, and consequently classifying the promising
* language corresponding to the one having the highest sum.
*
* NOTE: the algorithm requires including CHistogram.h, CStringUTF8.h, and
* defines.h header files to work perfectly.
******************************************************************************/
require_once('CStringUTF8.php');
require_once('defines.php');
class WBA
{
private $m_language_profiles;
/*
* Constructor, in which the reference words are loaded for each language.
*/
public function WBA()
{
// allocate the memory for the language profiles array
$this->m_language_profiles = array();
for($language=0; $language<NUMBER_LANGUAGES; $language++)
{
$this->m_language_profiles[$language] = new CHistogram();
}
$this->m_language_profiles[LNG_FRENCH]->loadWordsFromFile('./LID_tools/References/Common_Words/french.txt');
$this->m_language_profiles[LNG_ENGLISH]->loadWordsFromFile('./LID_tools/References/Common_Words/english.txt');
$this->m_language_profiles[LNG_ARABIC]->loadWordsFromFile('./LID_tools/References/Common_Words/arabic.txt');
$this->m_language_profiles[LNG_RUSSIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/russian.txt');
$this->m_language_profiles[LNG_GERMAN]->loadWordsFromFile('./LID_tools/References/Common_Words/german.txt');
$this->m_language_profiles[LNG_ITALIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/italian.txt');
$this->m_language_profiles[LNG_GREEK]->loadWordsFromFile('./LID_tools/References/Common_Words/greek.txt');
$this->m_language_profiles[LNG_SPANISH]->loadWordsFromFile('./LID_tools/References/Common_Words/spanish.txt');
$this->m_language_profiles[LNG_PERSIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/persian.txt');
$this->m_language_profiles[LNG_CHINESE]->loadWordsFromFile('./LID_tools/References/Common_Words/chinese.txt');
$this->m_language_profiles[LNG_TURKISH]->loadWordsFromFile('./LID_tools/References/Common_Words/turkish.txt');
$this->m_language_profiles[LNG_FINNISH]->loadWordsFromFile('./LID_tools/References/Common_Words/finnish.txt');
$this->m_language_profiles[LNG_HEBREW]->loadWordsFromFile('./LID_tools/References/Common_Words/hebrew.txt');
$this->m_language_profiles[LNG_PORTUGUESE]->loadWordsFromFile('./LID_tools/References/Common_Words/portuguese.txt');
$this->m_language_profiles[LNG_ROMAN]->loadWordsFromFile('./LID_tools/References/Common_Words/roman.txt');
$this->m_language_profiles[LNG_POLISH]->loadWordsFromFile('./LID_tools/References/Common_Words/polish.txt');
$this->m_language_profiles[LNG_HUNGARIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/hungarian.txt');
$this->m_language_profiles[LNG_DUTCH]->loadWordsFromFile('./LID_tools/References/Common_Words/dutch.txt');
$this->m_language_profiles[LNG_IRISH]->loadWordsFromFile('./LID_tools/References/Common_Words/irish.txt');
$this->m_language_profiles[LNG_SWEDISH]->loadWordsFromFile('./LID_tools/References/Common_Words/swedish.txt');
$this->m_language_profiles[LNG_LATIN]->loadWordsFromFile('./LID_tools/References/Common_Words/latin.txt');
$this->m_language_profiles[LNG_ICELANDIC]->loadWordsFromFile('./LID_tools/References/Common_Words/icelandic.txt');
$this->m_language_profiles[LNG_HINDI]->loadWordsFromFile('./LID_tools/References/Common_Words/hindi.txt');
$this->m_language_profiles[LNG_CZECH]->loadWordsFromFile('./LID_tools/References/Common_Words/czech.txt');
$this->m_language_profiles[LNG_MALAYSIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/malaysian.txt');
$this->m_language_profiles[LNG_BULGARIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/bulgarian.txt');
$this->m_language_profiles[LNG_NORWEGIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/norwegian.txt');
$this->m_language_profiles[LNG_ALBANIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/albanian.txt');
$this->m_language_profiles[LNG_URDU]->loadWordsFromFile('./LID_tools/References/Common_Words/urdu.txt');
$this->m_language_profiles[LNG_THAI]->loadWordsFromFile('./LID_tools/References/Common_Words/thai.txt');
$this->m_language_profiles[LNG_INDONESIAN]->loadWordsFromFile('./LID_tools/References/Common_Words/indonesian.txt');
$this->m_language_profiles[LNG_DANISH]->loadWordsFromFile('./LID_tools/References/Common_Words/danish.txt');
}
/*
* identification: function to run the identification process.
*
* @param text: is the text to identify.
*/
public function identification($text)
{
$probabilities = $this->computeProbabilities($text);
return $this->classification($probabilities);
}
/*
* ComputeProbabilities: function computes the language probabilities
* which represent the sum of the words frequencies.
*
* [output]: a table of probabilities corresponding to 32 languages.
*
* @param text: is the text to identify.
*/
public function computeProbabilities($text)
{
$input_text = new CStringUTF8();
$input_text->utf8_TextToArray($text);
// pre-process the text
$input_text->stripUserTags();
$input_text->stripHashTags();
$input_text->stripURLs();
$input_text->stripUnusedChars();
$input_text->separateWords();
$input_text->stripMultipleSeparator();
$input_text->uppercaseToLowercase();
// extract a list of words from the text
$words = $input_text->getWords();
// compute the probabilities of the languages (sum of frequencies)
$probabilities = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
for($word=0; $word<count($words); $word++)
{
for($language=0; $language<NUMBER_LANGUAGES; $language++)
{
for($word_ref=0; $word_ref<count($this->m_language_profiles[$language]->m_vector); $word_ref++)
if(utf8_strcmp($this->m_language_profiles[$language]->m_vector[$word_ref]->m_element, $words[$word]))
{
$probabilities[$language] ++;
break;
}
}
}
return $probabilities;
}
public function classification($probabilities)
{
// retrieve the highest probability (sum of frequencies)
$max = 0; // keeps the highest probability
$promising_language = -1; // keeps the promising language
for($language=0; $language<NUMBER_LANGUAGES; $language++)
{
if($probabilities[$language] > $max)
{
$max = $probabilities[$language];
$promising_language = $language;
}
}
// determine exactly the promising language by applying an order of classification
if($promising_language != LNG_CHINESE && $promising_language != LNG_GREEK &&
$promising_language != LNG_HEBREW && $promising_language != LNG_HINDI &&
$promising_language != LNG_THAI)
{
if($max == $probabilities[LNG_ARABIC]) $promising_language = LNG_ARABIC;
else if($max == $probabilities[LNG_PERSIAN]) $promising_language = LNG_PERSIAN;
else if($max == $probabilities[LNG_URDU]) $promising_language = LNG_URDU;
else if($max == $probabilities[LNG_BULGARIAN]) $promising_language = LNG_BULGARIAN;
else if($max == $probabilities[LNG_RUSSIAN]) $promising_language = LNG_RUSSIAN;
else if($max == $probabilities[LNG_ENGLISH]) $promising_language = LNG_ENGLISH;
else if($max == $probabilities[LNG_DUTCH]) $promising_language = LNG_DUTCH;
else if($max == $probabilities[LNG_INDONESIAN]) $promising_language = LNG_INDONESIAN;
else if($max == $probabilities[LNG_MALAYSIAN]) $promising_language = LNG_MALAYSIAN;
else if($max == $probabilities[LNG_LATIN]) $promising_language = LNG_LATIN;
else if($max == $probabilities[LNG_ROMAN]) $promising_language = LNG_ROMAN;
else if($max == $probabilities[LNG_FRENCH]) $promising_language = LNG_FRENCH;
else if($max == $probabilities[LNG_ITALIAN]) $promising_language = LNG_ITALIAN;
else if($max == $probabilities[LNG_IRISH]) $promising_language = LNG_IRISH;
else if($max == $probabilities[LNG_SPANISH]) $promising_language = LNG_SPANISH;
else if($max == $probabilities[LNG_PORTUGUESE]) $promising_language = LNG_PORTUGUESE;
else if($max == $probabilities[LNG_ALBANIAN]) $promising_language = LNG_ALBANIAN;
else if($max == $probabilities[LNG_CZECH]) $promising_language = LNG_CZECH;
else if($max == $probabilities[LNG_FINNISH]) $promising_language = LNG_FINNISH;
else if($max == $probabilities[LNG_HUNGARIAN]) $promising_language = LNG_HUNGARIAN;
else if($max == $probabilities[LNG_SWEDISH]) $promising_language = LNG_SWEDISH;
else if($max == $probabilities[LNG_GERMAN]) $promising_language = LNG_GERMAN;
else if($max == $probabilities[LNG_NORWEGIAN]) $promising_language = LNG_NORWEGIAN;
else if($max == $probabilities[LNG_DANISH]) $promising_language = LNG_DANISH;
else if($max == $probabilities[LNG_ICELANDIC]) $promising_language = LNG_ICELANDIC;
else if($max == $probabilities[LNG_TURKISH]) $promising_language = LNG_TURKISH;
else if($max == $probabilities[LNG_POLISH]) $promising_language = LNG_POLISH;
}
return $promising_language;
}
}
?>