From 9077602434a8a393f1b76c8b84edca75c619c040 Mon Sep 17 00:00:00 2001 From: Martijn van der Lee Date: Mon, 13 Jan 2025 20:15:02 +0100 Subject: [PATCH] addHyphenations --- README.md | 7 ++++++- src/Syllable.php | 29 ++++++++++++++++++++++------- tests/src/SyllableTest.php | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0c1669a..690fb54 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Syllable -Version 1.7 +Version 1.8 [![Tests](https://github.com/vanderlee/phpSyllable/actions/workflows/tests.yml/badge.svg)](https://github.com/vanderlee/phpSyllable/actions/workflows/tests.yml) @@ -114,6 +114,11 @@ Default to the `languages` subdirectory of the current directory. Set the language whose rules will be used for hyphenation. +#### public addHyphenations(array $hyphenations) + +Add any number of custom hyphenation patterns, using '-' to specify where hyphens may occur. +Omit the '-' from the pattern to add words that will not be hyphenated. + #### public setHyphen(mixed $hyphen) Set the hyphen text or object to use as a hyphen marker. diff --git a/src/Syllable.php b/src/Syllable.php index 0132910..eaf1960 100644 --- a/src/Syllable.php +++ b/src/Syllable.php @@ -54,6 +54,7 @@ class Syllable private $patterns = null; private $maxPattern = null; private $hyphenation = null; + private $userHyphenations = array(); /** * Character encoding to use. @@ -137,6 +138,19 @@ public function setLanguage($language) $this->setSource(new File($language, self::$languageDir)); } + /** + * Add custom hyphenation patterns for words using a '-' to explicitly specify hyphenation (if any) + * @param array $hyphenations + * @return void + */ + public function addHyphenations(array $hyphenations) + { + foreach ($hyphenations as $pattern) { + $word = str_replace('-', '', $pattern); + $this->userHyphenations[$word] = $pattern; + } + } + /** * Set the hyphen text or object to use as a hyphen marker. * @@ -755,18 +769,19 @@ private function parseWord($word) $wordLowerCased = mb_strtolower($word); + if (isset($this->userHyphenations[$wordLowerCased])) { + return $this->parseWordByHyphenation($this->userHyphenations[$wordLowerCased], $word, $wordLowerCased); + } + if (isset($this->hyphenation[$wordLowerCased])) { - return $this->parseWordByHyphenation($word, $wordLowerCased); - } else { - return $this->parseWordByPatterns($word, $wordLength, $wordLowerCased); + return $this->parseWordByHyphenation($this->hyphenation[$wordLowerCased], $word, $wordLowerCased); } + + return $this->parseWordByPatterns($word, $wordLength, $wordLowerCased); } - private function parseWordByHyphenation($word, $wordLowerCased = null) + private function parseWordByHyphenation($hyphenation, $word, $wordLowerCased = null) { - $wordLowerCased = $wordLowerCased ?: mb_strtolower($word); - - $hyphenation = $this->hyphenation[$wordLowerCased]; $hyphenationLength = mb_strlen($hyphenation); $parts = []; diff --git a/tests/src/SyllableTest.php b/tests/src/SyllableTest.php index 52ecc16..1ee2357 100644 --- a/tests/src/SyllableTest.php +++ b/tests/src/SyllableTest.php @@ -71,6 +71,24 @@ public function testSetLanguage() ); } + /** + * @return void + */ + public function testSetExceptions() + { + $this->object->setHyphen('-'); + + $this->object->setLanguage('en-us'); + $this->object->addHyphenations([ + 'explicit', + 'hyp-hena-ti-on' + ]); + $this->assertEquals( + 'ap-ply explicit hyp-hena-ti-on pat-terns', + $this->object->hyphenateText('apply explicit hyphenation patterns') + ); + } + /** * @return void */