Merge pull request #229 from jpirnay/feat-swedish-hyphenation

feat: Add Swedish hyphenation (upstream PR 1637 by steka)
This commit is contained in:
jpirnay
2026-05-16 21:14:59 +02:00
committed by GitHub
7 changed files with 6524 additions and 4 deletions
+14 -2
View File
@@ -8,15 +8,21 @@
#include "LanguageHyphenator.h"
#include "LanguageRegistry.h"
struct Iso639Mapping {
const char* iso639_2;
const char* iso639_1;
};
static constexpr Iso639Mapping kIso639Mappings[] = {{"eng", "en"}, {"fra", "fr"}, {"fre", "fr"}, {"deu", "de"},
{"ger", "de"}, {"rus", "ru"}, {"spa", "es"}, {"ita", "it"},
{"ukr", "uk"}, {"swe", "sv"}, {"pol", "pl"}};
const LanguageHyphenator* Hyphenator::cachedHyphenator_ = nullptr;
namespace {
// Maps a BCP-47 language tag to a language-specific hyphenator.
const LanguageHyphenator* hyphenatorForLanguage(const std::string& langTag) {
if (langTag.empty()) return nullptr;
// Extract primary subtag and normalize to lowercase (e.g., "en-US" -> "en").
std::string primary;
primary.reserve(langTag.size());
for (char c : langTag) {
@@ -26,6 +32,12 @@ const LanguageHyphenator* hyphenatorForLanguage(const std::string& langTag) {
}
if (primary.empty()) return nullptr;
for (const auto& mapping : kIso639Mappings) {
if (primary == mapping.iso639_2) {
return getLanguageHyphenatorForPrimaryTag(mapping.iso639_1);
}
}
return getLanguageHyphenatorForPrimaryTag(primary);
}
@@ -11,6 +11,7 @@
#include "generated/hyph-it.trie.h"
#include "generated/hyph-pl.trie.h"
#include "generated/hyph-ru.trie.h"
#include "generated/hyph-sv.trie.h"
#include "generated/hyph-uk.trie.h"
namespace {
@@ -22,10 +23,11 @@ LanguageHyphenator germanHyphenator(de_patterns, isLatinLetter, toLowerLatin);
LanguageHyphenator russianHyphenator(ru_patterns, isCyrillicLetter, toLowerCyrillic);
LanguageHyphenator spanishHyphenator(es_patterns, isLatinLetter, toLowerLatin);
LanguageHyphenator italianHyphenator(it_patterns, isLatinLetter, toLowerLatin);
LanguageHyphenator swedishHyphenator(sv_patterns, isLatinLetter, toLowerLatin);
LanguageHyphenator ukrainianHyphenator(uk_patterns, isCyrillicLetter, toLowerCyrillic);
LanguageHyphenator polishHyphenator(pl_patterns, isLatinLetter, toLowerLatin);
using EntryArray = std::array<LanguageEntry, 8>;
using EntryArray = std::array<LanguageEntry, 9>;
const EntryArray& entries() {
static const EntryArray kEntries = {{{"english", "en", &englishHyphenator},
@@ -34,6 +36,7 @@ const EntryArray& entries() {
{"russian", "ru", &russianHyphenator},
{"spanish", "es", &spanishHyphenator},
{"italian", "it", &italianHyphenator},
{"swedish", "sv", &swedishHyphenator},
{"polish", "pl", &polishHyphenator},
{"ukrainian", "uk", &ukrainianHyphenator}}};
return kEntries;
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -73,7 +73,7 @@ constexpr SerializedHyphenationPatterns {patterns_symbol} = {{
sizeof({data_symbol}),
}};
"""
path.write_text(content)
path.write_text(content, encoding='utf-8')
def main() -> None:
+1
View File
@@ -23,3 +23,4 @@ process es
process ru
process it
process uk
process sv
@@ -44,6 +44,7 @@ const std::vector<LanguageConfig> kSupportedLanguages = {
{"russian", "test/hyphenation_eval/resources/russian_hyphenation_tests.txt", "ru"},
{"spanish", "test/hyphenation_eval/resources/spanish_hyphenation_tests.txt", "es"},
{"italian", "test/hyphenation_eval/resources/italian_hyphenation_tests.txt", "it"},
{"swedish", "test/hyphenation_eval/resources/swedish_hyphenation_tests.txt", "sv"},
};
std::vector<size_t> expectedPositionsFromAnnotatedWord(const std::string& annotated) {
File diff suppressed because it is too large Load Diff