Skip to content

Commit 9077602

Browse files
committed
addHyphenations
1 parent f13791c commit 9077602

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Syllable
22

3-
Version 1.7
3+
Version 1.8
44

55
[![Tests](https://github.com/vanderlee/phpSyllable/actions/workflows/tests.yml/badge.svg)](https://github.com/vanderlee/phpSyllable/actions/workflows/tests.yml)
66

@@ -114,6 +114,11 @@ Default to the `languages` subdirectory of the current directory.
114114

115115
Set the language whose rules will be used for hyphenation.
116116

117+
#### public addHyphenations(array $hyphenations)
118+
119+
Add any number of custom hyphenation patterns, using '-' to specify where hyphens may occur.
120+
Omit the '-' from the pattern to add words that will not be hyphenated.
121+
117122
#### public setHyphen(mixed $hyphen)
118123

119124
Set the hyphen text or object to use as a hyphen marker.

src/Syllable.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Syllable
5454
private $patterns = null;
5555
private $maxPattern = null;
5656
private $hyphenation = null;
57+
private $userHyphenations = array();
5758

5859
/**
5960
* Character encoding to use.
@@ -137,6 +138,19 @@ public function setLanguage($language)
137138
$this->setSource(new File($language, self::$languageDir));
138139
}
139140

141+
/**
142+
* Add custom hyphenation patterns for words using a '-' to explicitly specify hyphenation (if any)
143+
* @param array $hyphenations
144+
* @return void
145+
*/
146+
public function addHyphenations(array $hyphenations)
147+
{
148+
foreach ($hyphenations as $pattern) {
149+
$word = str_replace('-', '', $pattern);
150+
$this->userHyphenations[$word] = $pattern;
151+
}
152+
}
153+
140154
/**
141155
* Set the hyphen text or object to use as a hyphen marker.
142156
*
@@ -755,18 +769,19 @@ private function parseWord($word)
755769

756770
$wordLowerCased = mb_strtolower($word);
757771

772+
if (isset($this->userHyphenations[$wordLowerCased])) {
773+
return $this->parseWordByHyphenation($this->userHyphenations[$wordLowerCased], $word, $wordLowerCased);
774+
}
775+
758776
if (isset($this->hyphenation[$wordLowerCased])) {
759-
return $this->parseWordByHyphenation($word, $wordLowerCased);
760-
} else {
761-
return $this->parseWordByPatterns($word, $wordLength, $wordLowerCased);
777+
return $this->parseWordByHyphenation($this->hyphenation[$wordLowerCased], $word, $wordLowerCased);
762778
}
779+
780+
return $this->parseWordByPatterns($word, $wordLength, $wordLowerCased);
763781
}
764782

765-
private function parseWordByHyphenation($word, $wordLowerCased = null)
783+
private function parseWordByHyphenation($hyphenation, $word, $wordLowerCased = null)
766784
{
767-
$wordLowerCased = $wordLowerCased ?: mb_strtolower($word);
768-
769-
$hyphenation = $this->hyphenation[$wordLowerCased];
770785
$hyphenationLength = mb_strlen($hyphenation);
771786

772787
$parts = [];

tests/src/SyllableTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ public function testSetLanguage()
7171
);
7272
}
7373

74+
/**
75+
* @return void
76+
*/
77+
public function testSetExceptions()
78+
{
79+
$this->object->setHyphen('-');
80+
81+
$this->object->setLanguage('en-us');
82+
$this->object->addHyphenations([
83+
'explicit',
84+
'hyp-hena-ti-on'
85+
]);
86+
$this->assertEquals(
87+
'ap-ply explicit hyp-hena-ti-on pat-terns',
88+
$this->object->hyphenateText('apply explicit hyphenation patterns')
89+
);
90+
}
91+
7492
/**
7593
* @return void
7694
*/

0 commit comments

Comments
 (0)