Skip to content

Commit

Permalink
addHyphenations
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderlee committed Jan 13, 2025
1 parent f13791c commit 9077602
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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.
Expand Down
29 changes: 22 additions & 7 deletions src/Syllable.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Syllable
private $patterns = null;
private $maxPattern = null;
private $hyphenation = null;
private $userHyphenations = array();

/**
* Character encoding to use.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 = [];
Expand Down
18 changes: 18 additions & 0 deletions tests/src/SyllableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 9077602

Please sign in to comment.