1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2008-2011 Andreas Heigl<[email protected]> |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category Hyphenation |
24
|
|
|
* @package Org_Heigl_Hyphenator |
25
|
|
|
* @subpackage Tokenizer |
26
|
|
|
* @author Andreas Heigl <[email protected]> |
27
|
|
|
* @copyright 2008-2011 Andreas Heigl<[email protected]> |
28
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
29
|
|
|
* @version 2.0.1 |
30
|
|
|
* @link http://github.com/heiglandreas/Hyphenator |
31
|
|
|
* @since 04.11.2011 |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
namespace Org\Heigl\Hyphenator\Tokenizer; |
35
|
|
|
|
36
|
|
|
use Org\Heigl\Hyphenator\Hyphenator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This Class describes a Token representing a word |
40
|
|
|
* |
41
|
|
|
* @category Hyphenation |
42
|
|
|
* @package Org_Heigl_Hyphenator |
43
|
|
|
* @subpackage Tokenizer |
44
|
|
|
* @author Andreas Heigl <[email protected]> |
45
|
|
|
* @copyright 2008-2011 Andreas Heigl<[email protected]> |
46
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
47
|
|
|
* @version 2.0.1 |
48
|
|
|
* @link http://github.com/heiglandreas/Hyphenator |
49
|
|
|
* @since 04.11.2011 |
50
|
|
|
*/ |
51
|
|
|
class WordToken extends Token |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* The hyphenation patterns for this token. |
55
|
|
|
* |
56
|
|
|
* @var array $_pattern |
57
|
|
|
*/ |
58
|
|
|
protected $pattern = array(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add a substring=>pattern array to the already existing ones |
62
|
|
|
* |
63
|
|
|
* @param array $pattern The new patterns to add |
64
|
|
|
* |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
|
|
public function addPattern(array $pattern) |
68
|
|
|
{ |
69
|
|
|
$this->pattern = array_merge($this->pattern, $pattern); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the content for the hyphenator. |
76
|
|
|
* |
77
|
|
|
* THis will prepend and append a dot to the content for better hyphenation |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getHyphenateContent() |
82
|
|
|
{ |
83
|
|
|
return '.' . $this->content . '.'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Merge the given Hyphenation patterns to one pattern for the given token |
88
|
|
|
* |
89
|
|
|
* This is done using the given quality value. |
90
|
|
|
* |
91
|
|
|
* @param int $quality The hyphenation quality to use |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getMergedPattern($quality = Hyphenator::QUALITY_HIGHEST) |
96
|
|
|
{ |
97
|
|
|
$content = $this->getHyphenateContent(); |
98
|
|
|
$endPattern = str_repeat('0', mb_strlen($content)+1); |
99
|
|
|
foreach ($this->pattern as $string => $pattern) { |
100
|
|
|
$strStart = -1; |
101
|
|
|
while (false !== $strStart = @mb_strpos($content, $string, $strStart + 1)) { |
102
|
|
|
$strLen = mb_strlen($string); |
103
|
|
|
for ($i=0; $i <= $strLen; $i++) { |
104
|
|
|
$start = $i+$strStart; |
105
|
|
|
$currentQuality = substr($endPattern, $start, 1); |
106
|
|
|
$patternQuality = substr($pattern, $i, 1); |
107
|
|
|
if ($currentQuality >= $patternQuality) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
if ($quality < $patternQuality) { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
$endPattern = substr($endPattern, 0, $start) . $patternQuality . substr($endPattern, $start+1); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return substr($endPattern, 1, strlen($endPattern)-2); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|