|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) Andreas Heigl<[email protected]> |
|
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
5
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
6
|
|
|
* in the Software without restriction, including without limitation the rights |
|
7
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
8
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
9
|
|
|
* furnished to do so, subject to the following conditions: |
|
10
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
11
|
|
|
* all copies or substantial portions of the Software. |
|
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
18
|
|
|
* THE SOFTWARE. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Andreas Heigl<[email protected]> |
|
21
|
|
|
* @copyright Andreas Heigl |
|
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
|
23
|
|
|
* @since 12.10.2016 |
|
24
|
|
|
* @link http://github.com/heiglandreas/org.heigl.TextStatistics |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Org_Heigl\TextStatistics\Util; |
|
28
|
|
|
|
|
29
|
|
|
use Org\Heigl\Hyphenator\Filter\Filter; |
|
30
|
|
|
use Org\Heigl\Hyphenator\Tokenizer as t; |
|
31
|
|
|
|
|
32
|
|
|
class SyllableFilter extends Filter |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Run the filter over the given Token |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Org\Heigl\Hyphenator\Tokenizer\TokenRegistry $tokens The registry |
|
39
|
|
|
* to apply the filter to |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Org\Heigl\Hyphenator\Tokenizer\TokenRegistry |
|
42
|
|
|
*/ |
|
43
|
|
|
public function run(t\TokenRegistry $tokens) |
|
44
|
|
|
{ |
|
45
|
|
|
foreach ($tokens as $token) { |
|
46
|
|
|
if (! $token instanceof t\WordToken) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
$string = $token->getFilteredContent(); |
|
50
|
|
|
$pattern = $token->getMergedPattern(); |
|
51
|
|
|
$length = $token->length(); |
|
52
|
|
|
$lastOne = 0; |
|
53
|
|
|
$syllables = array(); |
|
54
|
|
|
for ($i = 1; $i <= $length; $i++) { |
|
55
|
|
|
$currPattern = mb_substr($pattern, $i, 1); |
|
56
|
|
|
if ($i < $this->options->getLeftMin()) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
if ($i > $length - $this->options->getRightMin()) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
if (0 == $currPattern) { |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
if (0 === (int) $currPattern % 2) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
$sylable = mb_substr($string, $lastOne, $i-$lastOne); |
|
69
|
|
|
$lastOne = $i; |
|
70
|
|
|
$syllables[] = $sylable; |
|
71
|
|
|
} |
|
72
|
|
|
$syllables[] = mb_substr($string, $lastOne); |
|
73
|
|
|
$token->setHyphenatedContent($syllables); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $tokens; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Concatenate the given TokenRegistry to return one result |
|
81
|
|
|
* |
|
82
|
|
|
* @param \Org\Heigl\Hyphenator\Tokenizer\TokenRegistry $tokens The registry |
|
83
|
|
|
* to apply the filter to |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function doConcatenate(t\TokenRegistry $tokens) // @codingStandardsIgnoreLine |
|
88
|
|
|
{ |
|
89
|
|
|
$syllables = []; |
|
90
|
|
|
foreach ($tokens as $token) { |
|
91
|
|
|
if (! $token instanceof t\WordToken) { |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
foreach ($token->getHyphenatedContent() as $syllable) { |
|
95
|
|
|
$syllables[] = $syllable; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $syllables; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|