-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSummarize.php
555 lines (464 loc) · 17.3 KB
/
Summarize.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
namespace ArabyPHP\Summarize;
class Summarize
{
/**
* @var array
*/
private $_normalizeAlef = ['أ', 'إ', 'آ'];
/**
* @var array
*/
private $_normalizeDiacritics = ['َ', 'ً', 'ُ', 'ٌ', 'ِ', 'ٍ', 'ْ', 'ّ'];
/**
* @var array
*/
private $_commonChars = ['ة', 'ه', 'ي', 'ن', 'و', 'ت', 'ل', 'ا', 'س', 'م', 'e', 't', 'a', 'o', 'i', 'n', 's'];
/**
* Separators.
*
* @var array
*/
private $_separators = ['.', "\n", '،', '؛', '(', '[', '{', ')', ']', '}', ',', ';'];
/**
* @var array
*/
private $_commonWords = [];
/**
* @var array
*/
private $_importantWords = [];
/**
* Loads initialize values.
*
* @ignore
*/
public function __construct()
{
// This common words used in cleanCommon method
$words = $this->getJsonData(__DIR__.'/data/ArStopWords.json');
$en_words = $this->getJsonData(__DIR__.'/data/EnStopWords.json');
// Merge arabic word with english words
$words = array_merge($words, $en_words);
// Remove spaces
$words = array_map('trim', $words);
$this->_commonWords = $words;
// This important words used in rankSentences method
$words = $this->getJsonData(__DIR__.'/data/ImportantWords.json');
// Remove spaces
$words = array_map('trim', $words);
$this->_importantWords = $words;
}
/**
* Load enhanced Arabic stop words list.
*
* @return void
*/
public function loadExtra()
{
$extra_words = $this->getJsonData(__DIR__.'/data/ArExtraStopWords.json');
$this->_commonWords = array_merge($this->_commonWords, $extra_words);
}
/**
* Core summarize function that implement required steps in the algorithm.
*
* @param string $str Input Arabic document as a string
* @param int $int Sentences value (see $mode effect also)
* @param string $keywords List of keywords higlited by search process
* @param string $mode Mode of sentences count [number|rate]
* @param string $output Output mode [summary|highlight]
* @param string $style Name of the CSS class you would like to apply
* @param string $style Name of the CSS class you would like to apply
*
* @return string Output summary requested
*
*/
protected function summarize($str, $int, $keywords, $mode, $output, $style = null)
{
preg_match_all("/[^\.\n\،\؛\,\;](.+?)[\.\n\،\؛\,\;]/u", $str, $sentences);
$_sentences = $sentences[0];
if ($mode == 'rate') {
$str = preg_replace("/\s{2,}/u", ' ', $str);
$totalChars = mb_strlen($str);
$totalSentences = count($_sentences);
$maxChars = round($int * $totalChars / 100);
$int = round($int * $totalSentences / 100);
} else {
$maxChars = 99999;
}
$summary = '';
$str = strip_tags($str);
$normalizedStr = $this->doNormalize($str);
$cleanedStr = $this->cleanCommon($normalizedStr);
$stemStr = $this->draftStem($cleanedStr);
preg_match_all(
"/[^\.\n\،\؛\,\;](.+?)[\.\n\،\؛\,\;]/u",
$stemStr,
$sentences
);
$_stemmedSentences = $sentences[0];
$wordRanks = $this->rankWords($stemStr);
if ($keywords) {
$keywords = $this->doNormalize($keywords);
$keywords = $this->draftStem($keywords);
$words = explode(' ', $keywords);
foreach ($words as $word) {
$wordRanks[$word] = 1000;
}
}
$sentencesRanks = $this->rankSentences($_sentences, $_stemmedSentences, $wordRanks);
list($sentences, $ranks) = $sentencesRanks;
$minRank = $this->minAcceptedRank($sentences, $ranks, $int, $maxChars);
$totalSentences = count($ranks);
for ($i = 0; $i < $totalSentences; $i++) {
if ($sentencesRanks[1][$i] >= $minRank) {
if ($output == 'summary') {
$summary .= ' '.$sentencesRanks[0][$i];
} else {
$summary .= '<span class="'.$style.'">'.
$sentencesRanks[0][$i].'</span>';
}
} else {
if ($output == 'highlight') {
$summary .= $sentencesRanks[0][$i];
}
}
}
if ($output == 'highlight') {
$summary = str_replace("\n", '<br />', $summary);
}
return $summary;
}
/**
* Summarize input Arabic string (document content) into specific number of
* sentences in the output.
*
* @param string $str Input Arabic document as a string
* @param int $int Number of sentences required in output summary
* @param string $keywords List of keywords higlited by search process
* @param string $style Name of the CSS class you would like to apply
*
* @return string Output summary requested
*
*/
public function doSummarize($str, $int, $keywords, $style = null)
{
$summary = $this->summarize($str, $int, $keywords, 'number', 'summary', $style);
return $summary;
}
/**
* Summarize percentage of the input Arabic string (document content) into output.
*
* @param string $str Input Arabic document as a string
* @param int $rate Rate of output summary sentence number as
* percentage of the input Arabic string
* (document content)
* @param string $keywords List of keywords higlited by search process
* @param string $style Name of the CSS class you would like to apply
*
* @return string Output summary requested
*
*/
public function doRateSummarize($str, $rate, $keywords, $style = null)
{
$summary = $this->summarize($str, $rate, $keywords, 'rate', 'summary', $style);
return $summary;
}
/**
* Highlight key sentences (summary) of the input string (document content)
* using CSS and send the result back as an output.
*
* @param string $str Input Arabic document as a string
* @param int $int Number of key sentences required to be
* highlighted in the input string
* (document content)
* @param string $keywords List of keywords higlited by search process
* @param string $style Name of the CSS class you would like to apply
*
* @return string Output highlighted key sentences summary (using CSS)
*
*/
public function highlightSummary($str, $int, $keywords, $style = null)
{
$summary = $this->summarize($str, $int, $keywords, 'number', 'highlight', $style);
return $summary;
}
/**
* Highlight key sentences (summary) as percentage of the input string
* (document content) using CSS and send the result back as an output.
*
* @param string $str Input Arabic document as a string
* @param int $rate Rate of highlighted key sentences summary
* number as percentage of the input Arabic
* string (document content)
* @param string $keywords List of keywords higlited by search process
* @param string $style Name of the CSS class you would like to apply
*
* @return string Output highlighted key sentences summary (using CSS)
*
*/
public function highlightRateSummary($str, $rate, $keywords, $style = null)
{
$summary = $this->summarize($str, $rate, $keywords, 'rate', 'highlight', $style);
return $summary;
}
/**
* Extract keywords from a given Arabic string (document content).
*
* @param string $str Input Arabic document as a string
* @param int $int Number of keywords required to be extracting
* from input string (document content)
*
* @return string List of the keywords extracting from input Arabic string
* (document content)
*
*/
public function getMetaKeywords($str, $int)
{
$patterns = [];
$replacements = [];
$metaKeywords = '';
array_push($patterns, '/\.|\n|\،|\؛|\(|\[|\{|\)|\]|\}|\,|\;/u');
array_push($replacements, ' ');
$str = preg_replace($patterns, $replacements, $str);
$normalizedStr = $this->doNormalize($str);
$cleanedStr = $this->cleanCommon($normalizedStr);
$str = preg_replace('/(\W)ال(\w{3,})/u', '\\1\\2', $cleanedStr);
$str = preg_replace('/(\W)وال(\w{3,})/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})هما(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})كما(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})تين(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})هم(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})هن(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ها(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})نا(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ني(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})كم(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})تم(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})كن(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ات(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ين(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})تن(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ون(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ان(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})تا(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})وا(\W)/u', '\\1\\2', $str);
$str = preg_replace('/(\w{3,})ة(\W)/u', '\\1\\2', $str);
$stemStr = preg_replace('/(\W)\w{1,3}(\W)/u', '\\2', $str);
$wordRanks = $this->rankWords($stemStr);
arsort($wordRanks, SORT_NUMERIC);
$i = 1;
foreach ($wordRanks as $key => $value) {
if ($this->acceptedWord($key)) {
$metaKeywords .= $key.'، ';
$i++;
}
if ($i > $int) {
break;
}
}
$metaKeywords = mb_substr($metaKeywords, 0, -2);
return $metaKeywords;
}
/**
* Normalized Arabic document.
*
* @param string $str Input Arabic document as a string
*
* @return string Normalized Arabic document
*
*/
protected function doNormalize($str)
{
$str = str_replace($this->_normalizeAlef, 'ا', $str);
$str = str_replace($this->_normalizeDiacritics, '', $str);
$str = strtr(
$str,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'
);
return $str;
}
/**
* Extracting common Arabic words (roughly)
* from input Arabic string (document content).
*
* @param string $str Input normalized Arabic document as a string
*
* @return string Arabic document as a string free of common words (roughly)
*
*/
public function cleanCommon($str)
{
$str = str_replace($this->_commonWords, ' ', $str);
return $str;
}
/**
* Remove less significant Arabic letter from given string (document content).
* Please note that output will not be human readable.
*
* @param string $str Input Arabic document as a string
*
* @return string Output string after removing less significant Arabic letter
* (not human readable output)
*
*/
protected function draftStem($str)
{
$str = str_replace($this->_commonChars, '', $str);
return $str;
}
/**
* Ranks words in a given Arabic string (document content). That rank refers
* to the frequency of that word appears in that given document.
*
* @param string $str Input Arabic document as a string
*
* @return hash Associated array where document words referred by index and
* those words ranks referred by values of those array items.
*
*/
protected function rankWords($str)
{
$wordsRanks = [];
$str = str_replace($this->_separators, ' ', $str);
$words = preg_split("/[\s,]+/u", $str);
foreach ($words as $word) {
if (isset($wordsRanks[$word])) {
$wordsRanks[$word]++;
} else {
$wordsRanks[$word] = 1;
}
}
foreach ($wordsRanks as $wordRank => $total) {
if (mb_substr($wordRank, 0, 1) == 'و') {
$subWordRank = mb_substr($wordRank, 1, mb_strlen($wordRank) - 1);
if (isset($wordsRanks[$subWordRank])) {
unset($wordsRanks[$wordRank]);
$wordsRanks[$subWordRank] += $total;
}
}
}
return $wordsRanks;
}
/**
* Ranks sentences in a given Arabic string (document content).
*
* @param array $sentences Sentences of the input Arabic document
* as an array
* @param array $stemmedSentences Stemmed sentences of the input Arabic
* document as an array
* @param array $arr Words ranks array (word as an index and
* value refer to the word frequency)
*
* @return array Two dimension array, first item is an array of document
* sentences, second item is an array of ranks of document
* sentences.
*
*/
protected function rankSentences(array $sentences, array $stemmedSentences, array $arr)
{
$sentenceArr = [];
$rankArr = [];
$max = count($sentences);
for ($i = 0; $i < $max; $i++) {
$sentence = $sentences[$i];
$w = 0;
$first = mb_substr($sentence, 0, 1);
$last = mb_substr($sentence, -1, 1);
if ($first == "\n") {
$w += 3;
} elseif (in_array($first, $this->_separators)) {
$w += 2;
} else {
$w += 1;
}
if ($last == "\n") {
$w += 3;
} elseif (in_array($last, $this->_separators)) {
$w += 2;
} else {
$w += 1;
}
foreach ($this->_importantWords as $word) {
if ($word != '') {
$w += mb_substr_count($sentence, $word);
}
}
$sentence = mb_substr(mb_substr($sentence, 0, -1), 1);
if (!in_array($first, $this->_separators)) {
$sentence = $first.$sentence;
}
$stemStr = $stemmedSentences[$i];
$stemStr = mb_substr($stemStr, 0, -1);
$words = preg_split("/[\s,]+/u", $stemStr);
$totalWords = count($words);
if ($totalWords > 4) {
$totalWordsRank = 0;
foreach ($words as $word) {
if (isset($arr[$word])) {
$totalWordsRank += $arr[$word];
}
}
$wordsRank = $totalWordsRank / $totalWords;
$sentenceRanks = $w * $wordsRank;
array_push($sentenceArr, $sentence.$last);
array_push($rankArr, $sentenceRanks);
}
}
$sentencesRanks = [$sentenceArr, $rankArr];
return $sentencesRanks;
}
/**
* Calculate minimum rank for sentences which will be including in the summary.
*
* @param array $str Document sentences
* @param array $arr Sentences ranks
* @param int $int Number of sentences you need to include in your summary
* @param int $max Maximum number of characters accepted in your summary
*
* @return int Minimum accepted sentence rank (sentences with rank more
* than this will be listed in the document summary)
*
*/
protected function minAcceptedRank($str, $arr, $int, $max)
{
$len = [];
$minRank = 0;
foreach ($str as $line) {
$len[] = mb_strlen($line);
}
rsort($arr, SORT_NUMERIC);
$totalChars = 0;
for ($i = 0; $i <= $int; $i++) {
if (!isset($arr[$i])) {
$minRank = 0;
break;
}
$totalChars += $len[$i];
if ($totalChars >= $max) {
$minRank = $arr[$i];
break;
}
$minRank = $arr[$i];
}
return $minRank;
}
/**
* Check some conditions to know if a given string is a formal valid word or not.
*
* @param string $word String to be checked if it is a valid word or not
*
* @return bool True if passed string is accepted as a valid word else
* it will return False
*
*/
protected function acceptedWord($word)
{
$accept = true;
if (mb_strlen($word) < 3) {
$accept = false;
}
return $accept;
}
}