Skip to content

Commit 8ee452b

Browse files
authored
Merge pull request #73 from alexander-nitsche/feature-handle-hyphenations-case-insensitive
FEATURE: Use `\hyphenations` case-insensitive (like `\patterns`)
2 parents d47fe93 + 774ad9a commit 8ee452b

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/Syllable.php

+37-5
Original file line numberDiff line numberDiff line change
@@ -748,19 +748,51 @@ private function parseWord($word)
748748
{
749749
$wordLength = mb_strlen($word);
750750

751-
// Is this word smaller than the minimal length requirement?
752751
if ($wordLength < $this->minHyphenLeft + $this->minHyphenRight
753752
|| $wordLength < $this->minWordLength) {
754753
return [$word];
755754
}
756755

757-
// Is it a pre-hyphenated word?
758-
if (isset($this->hyphenation[$word])) {
759-
return mb_split('-', $this->hyphenation[$word]);
756+
$wordLowerCased = mb_strtolower($word);
757+
758+
if (isset($this->hyphenation[$wordLowerCased])) {
759+
return $this->parseWordByHyphenation($word, $wordLowerCased);
760+
} else {
761+
return $this->parseWordByPatterns($word, $wordLength, $wordLowerCased);
760762
}
763+
}
764+
765+
private function parseWordByHyphenation($word, $wordLowerCased = null)
766+
{
767+
$wordLowerCased = $wordLowerCased ?: mb_strtolower($word);
768+
769+
$hyphenation = $this->hyphenation[$wordLowerCased];
770+
$hyphenationLength = mb_strlen($hyphenation);
771+
772+
$parts = [];
773+
$part = '';
774+
for ($i = 0, $j = 0; $i < $hyphenationLength; $i++) {
775+
if (mb_substr($hyphenation, $i, 1) !== '-') {
776+
$part .= mb_substr($word, $j++, 1);
777+
} else {
778+
$parts[] = $part;
779+
$part = '';
780+
}
781+
}
782+
if (!empty($part)) {
783+
$parts[] = $part;
784+
}
785+
786+
return $parts;
787+
}
788+
789+
private function parseWordByPatterns($word, $wordLength = 0, $wordLowerCased = null)
790+
{
791+
$wordLength = $wordLength > 0 ? $wordLength : mb_strlen($word);
792+
$wordLowerCased = $wordLowerCased ?: mb_strtolower($word);
761793

762794
// Convenience array
763-
$text = '.'.mb_strtolower($word).'.';
795+
$text = '.'.$wordLowerCased.'.';
764796
$textLength = $wordLength + 2;
765797
$patternLength = $this->maxPattern < $textLength
766798
? $this->maxPattern

tests/src/SyllableTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,13 @@ public function testCaseInsensitivity()
638638
{
639639
$this->object->setHyphen('-');
640640

641+
// Patterns
641642
$this->assertEquals(['IN', 'EX', 'PLIC', 'A', 'BLE'], $this->object->splitText('INEXPLICABLE'));
642643
$this->assertEquals(['in', 'ex', 'plic', 'a', 'ble'], $this->object->splitText('inexplicable'));
644+
645+
// Hyphenations
646+
$this->assertEquals(['as', 'so', 'ciate'], $this->object->splitText('associate'));
647+
$this->assertEquals(['AS', 'SO', 'CIATE'], $this->object->splitText('ASSOCIATE'));
643648
}
644649

645650
/**

0 commit comments

Comments
 (0)