@@ -54,6 +54,7 @@ class Syllable
54
54
private $ patterns = null ;
55
55
private $ maxPattern = null ;
56
56
private $ hyphenation = null ;
57
+ private $ userHyphenations = array ();
57
58
58
59
/**
59
60
* Character encoding to use.
@@ -137,6 +138,19 @@ public function setLanguage($language)
137
138
$ this ->setSource (new File ($ language , self ::$ languageDir ));
138
139
}
139
140
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
+
140
154
/**
141
155
* Set the hyphen text or object to use as a hyphen marker.
142
156
*
@@ -755,18 +769,19 @@ private function parseWord($word)
755
769
756
770
$ wordLowerCased = mb_strtolower ($ word );
757
771
772
+ if (isset ($ this ->userHyphenations [$ wordLowerCased ])) {
773
+ return $ this ->parseWordByHyphenation ($ this ->userHyphenations [$ wordLowerCased ], $ word , $ wordLowerCased );
774
+ }
775
+
758
776
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 );
762
778
}
779
+
780
+ return $ this ->parseWordByPatterns ($ word , $ wordLength , $ wordLowerCased );
763
781
}
764
782
765
- private function parseWordByHyphenation ($ word , $ wordLowerCased = null )
783
+ private function parseWordByHyphenation ($ hyphenation , $ word , $ wordLowerCased = null )
766
784
{
767
- $ wordLowerCased = $ wordLowerCased ?: mb_strtolower ($ word );
768
-
769
- $ hyphenation = $ this ->hyphenation [$ wordLowerCased ];
770
785
$ hyphenationLength = mb_strlen ($ hyphenation );
771
786
772
787
$ parts = [];
0 commit comments