1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spell check a sentence |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category GLICER |
8
|
|
|
* @package GlSpellChecker |
9
|
|
|
* @author Emmanuel ROECKER |
10
|
|
|
* @author Rym BOUCHAGOUR |
11
|
|
|
* @copyright 2015 GLICER |
12
|
|
|
* @license MIT |
13
|
|
|
* @link http://dev.glicer.com/ |
14
|
|
|
* |
15
|
|
|
* Created : 04/05/15 |
16
|
|
|
* File : GlSpellCheckerSentence.php |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace GlSpellChecker; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class GlSpellCheckerError |
24
|
|
|
* @package GlSpellChecker |
25
|
|
|
*/ |
26
|
|
|
class GlSpellCheckerSentence |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $text; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var GlSpellCheckerError[] $errors |
36
|
|
|
*/ |
37
|
|
|
private $errors = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $text |
41
|
|
|
*/ |
42
|
|
|
public function __construct($text) |
43
|
|
|
{ |
44
|
|
|
$this->text = $text; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getText() |
51
|
|
|
{ |
52
|
|
|
return $this->text; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param GlSpellCheckerError $newerror |
57
|
|
|
*/ |
58
|
|
|
public function addError(GlSpellCheckerError $newerror) |
59
|
|
|
{ |
60
|
|
|
$this->errors[] = $newerror; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return GlSpellCheckerError[] |
65
|
|
|
*/ |
66
|
|
|
public function getErrors() |
67
|
|
|
{ |
68
|
|
|
return $this->errors; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return GlSpellCheckerError[] |
73
|
|
|
*/ |
74
|
|
|
public function mergeErrors() { |
75
|
|
|
$this->sortedErrorsByOffset(); |
76
|
|
|
$errors = $this->errors; |
77
|
|
|
/** |
78
|
|
|
* @var GlSpellCheckerError[] $newerrors |
79
|
|
|
*/ |
80
|
|
|
$newerrors = []; |
81
|
|
|
foreach ($errors as $error) { |
82
|
|
|
|
83
|
|
|
$finded = false; |
84
|
|
|
foreach ($newerrors as $newerror) { |
85
|
|
|
$start = $newerror->getOffset(); |
86
|
|
|
$end = $start + $newerror->getLength(); |
87
|
|
|
if (($error->getOffset() >= $start) && ($error->getOffset() <= $end)) { |
88
|
|
|
$newerror->merge($error); |
89
|
|
|
$finded = true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
if (!$finded) { |
93
|
|
|
$newerrors[] = clone $error; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $newerrors; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
|
private function sortedErrorsByOffset() { |
104
|
|
|
usort( |
105
|
|
|
$this->errors, |
106
|
|
|
function (GlSpellCheckerError $a, GlSpellCheckerError $b) { |
107
|
|
|
if ($a->getOffset() == $b->getOffset()) { |
108
|
|
|
return 0; |
109
|
|
|
} |
110
|
|
|
return ($a->getOffset() < $b->getOffset()) ? -1 : 1; |
111
|
|
|
} |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|