kotoshu 1.0.2-x86_64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.kotoshu-baseline.json +1 -0
- data/.pre-commit-hooks.yaml +18 -0
- data/.rspec +3 -0
- data/.rubocop.yml +351 -0
- data/.yardopts +8 -0
- data/CHANGELOG.md +813 -0
- data/CLAUDE.md +176 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/Cargo.lock +359 -0
- data/Cargo.toml +15 -0
- data/LICENSE +31 -0
- data/README.adoc +1292 -0
- data/Rakefile +150 -0
- data/SECURITY.md +93 -0
- data/TODO.impl/00-cut-0.1.md +121 -0
- data/TODO.impl/00-cut-0.2.md +175 -0
- data/TODO.impl/00-vision.md +281 -0
- data/TODO.impl/01-hunspell-correctness.md +111 -0
- data/TODO.impl/02-cli-unification.md +74 -0
- data/TODO.impl/03-dynamic-download.md +107 -0
- data/TODO.impl/04-language-modules.md +89 -0
- data/TODO.impl/05-semantic-path.md +83 -0
- data/TODO.impl/06-cjk-support.md +105 -0
- data/TODO.impl/07-rtl-support.md +71 -0
- data/TODO.impl/08-grammar-engine.md +106 -0
- data/TODO.impl/09-integrity-security.md +90 -0
- data/TODO.impl/10-testing-ci.md +98 -0
- data/TODO.impl/100-full-feature-batch3.md +28 -0
- data/TODO.impl/101-int4-tier-experiment.md +32 -0
- data/TODO.impl/102-lid-chain.md +28 -0
- data/TODO.impl/103-oov-bucket-tables.md +27 -0
- data/TODO.impl/104-vscode-marketplace-readiness.md +32 -0
- data/TODO.impl/105-personal-dictionary-in-check.md +34 -0
- data/TODO.impl/106-native-lid-detection.md +36 -0
- data/TODO.impl/107-basic-support-every-staged-language.md +42 -0
- data/TODO.impl/108-ko-ne-full-feature.md +37 -0
- data/TODO.impl/109-ten-audit.md +26 -0
- data/TODO.impl/11-release-v1.md +95 -0
- data/TODO.impl/110-sr-latn-and-polish.md +33 -0
- data/TODO.impl/111-embedding-bakeoff.md +33 -0
- data/TODO.impl/112-deployability-and-gating.md +26 -0
- data/TODO.impl/113-language-packs.md +26 -0
- data/TODO.impl/114-typo-biencoder-thread.md +33 -0
- data/TODO.impl/115-hybrid-retrieval-pricing.md +22 -0
- data/TODO.impl/116-baseline-suggestion-skip.md +81 -0
- data/TODO.impl/117-deterministic-frequency-tiers.md +110 -0
- data/TODO.impl/118-server-gem-floor.md +33 -0
- data/TODO.impl/119-embedded-kelly-tiers.md +56 -0
- data/TODO.impl/120-setup-metadata-refresh.md +35 -0
- data/TODO.impl/121-jekyll-baseline-skip.md +33 -0
- data/TODO.impl/122-pack-compression-kpk2.md +66 -0
- data/TODO.impl/123-typo-corpus-synthesis.md +55 -0
- data/TODO.impl/124-owner-residue.md +19 -0
- data/TODO.impl/125-consumer-floor-guards.md +43 -0
- data/TODO.impl/126-tier-error-budgets.md +33 -0
- data/TODO.impl/127-suggestion-feedback-loop.md +34 -0
- data/TODO.impl/128-hybrid-pricing-verdict.md +47 -0
- data/TODO.impl/129-pt-fr-corpora.md +23 -0
- data/TODO.impl/130-news-truth-0912.md +23 -0
- data/TODO.impl/131-hybrid-productization.md +42 -0
- data/TODO.impl/132-techniques-page-and-prose.md +27 -0
- data/TODO.impl/133-rust-first-distribution.md +68 -0
- data/TODO.impl/20-encapsulation-cleanup.md +47 -0
- data/TODO.impl/21-autoload-migration.md +64 -0
- data/TODO.impl/22-url-pin-single-source-of-truth.md +120 -0
- data/TODO.impl/23-fix-url-defaults.md +57 -0
- data/TODO.impl/24-cli-auto-setup-prompt.md +76 -0
- data/TODO.impl/25-soft-onnxruntime-require.md +75 -0
- data/TODO.impl/26-onnx-vocab-json-generation.md +96 -0
- data/TODO.impl/27-end-to-end-smoke-spec.md +113 -0
- data/TODO.impl/28-readme-quickstart-verification.md +68 -0
- data/TODO.impl/29-status-command.md +84 -0
- data/TODO.impl/30-language-auto-detection.md +74 -0
- data/TODO.impl/31-download-progress.md +58 -0
- data/TODO.impl/32-sarif-output-verification.md +72 -0
- data/TODO.impl/33-audit-log-rotation.md +54 -0
- data/TODO.impl/34-cache-eviction.md +59 -0
- data/TODO.impl/35-shell-completion.md +61 -0
- data/TODO.impl/36-cleanup-regressions-0.3.1.md +134 -0
- data/TODO.impl/37-hunspell-correctness-tier2.md +203 -0
- data/TODO.impl/38-onnx-semantic-gating.md +218 -0
- data/TODO.impl/39-tier3-and-beyond.md +232 -0
- data/TODO.impl/40-spec-drift-cleanup.md +134 -0
- data/TODO.impl/50-structure-aware-document-api.md +194 -0
- data/TODO.impl/51-grammar-engine-expansion.md +75 -0
- data/TODO.impl/52-multi-language-documents.md +95 -0
- data/TODO.impl/53-document-plugin-architecture.md +94 -0
- data/TODO.impl/54-cjk-support.md +117 -0
- data/TODO.impl/55-rtl-support.md +109 -0
- data/TODO.impl/56-t4-t5-t6-quality-architecture-ecosystem.md +126 -0
- data/TODO.impl/57-t5.1-step3-pure-function-strategies.md +138 -0
- data/TODO.impl/58-t5.2-configuration-as-data.md +113 -0
- data/TODO.impl/59-yard-api-documentation.md +75 -0
- data/TODO.impl/60-lsp-server.md +174 -0
- data/TODO.impl/61-editor-ecosystem.md +167 -0
- data/TODO.impl/62-framework-integrations.md +159 -0
- data/TODO.impl/63-web-playground-wasm.md +186 -0
- data/TODO.impl/64-http-api-and-sdks.md +191 -0
- data/TODO.impl/65-universal-kotoshu.md +251 -0
- data/TODO.impl/66-kotoshu-core.md +169 -0
- data/TODO.impl/67-kotoshu-rs-and-access-libraries.md +137 -0
- data/TODO.impl/68-sota-adoption.md +152 -0
- data/TODO.impl/69-sota-typo-eval.md +74 -0
- data/TODO.impl/70-sota-confidence-cascade.md +59 -0
- data/TODO.impl/71-sota-nested-model.md +63 -0
- data/TODO.impl/72-model2vec-evaluation.md +49 -0
- data/TODO.impl/73-rl-reranker.md +42 -0
- data/TODO.impl/74-distilled-reranker.md +36 -0
- data/TODO.impl/75-llm-evaluator.md +35 -0
- data/TODO.impl/76-optional-llm-backend.md +37 -0
- data/TODO.impl/77-model-coverage-expansion.md +96 -0
- data/TODO.impl/78-in-browser-playground.md +69 -0
- data/TODO.impl/79-news-and-releases.md +60 -0
- data/TODO.impl/80-ecosystem-docs.md +56 -0
- data/TODO.impl/81-editor-reach.md +68 -0
- data/TODO.impl/82-doc-formats-and-ignores.md +81 -0
- data/TODO.impl/83-models-batch-2.md +56 -0
- data/TODO.impl/84-language-modules-wave2.md +75 -0
- data/TODO.impl/85-wasm-model-api.md +48 -0
- data/TODO.impl/86-site-truth-wave2.md +45 -0
- data/TODO.impl/87-lsp-personal-dictionary.md +36 -0
- data/TODO.impl/88-cli-directory-mode.md +54 -0
- data/TODO.impl/89-framework-integrations-exec.md +59 -0
- data/TODO.impl/90-gem-correctness-residues.md +44 -0
- data/TODO.impl/91-unicode-and-sv-aff.md +47 -0
- data/TODO.impl/92-tier-mirror-cors.md +88 -0
- data/TODO.impl/93-python-wheels.md +32 -0
- data/TODO.impl/94-site-integrations-docs.md +35 -0
- data/TODO.impl/95-editor-action-polish.md +27 -0
- data/TODO.impl/96-server-semantic-models.md +50 -0
- data/TODO.impl/97-yard-execution.md +73 -0
- data/TODO.impl/98-campaign-cleanup.md +78 -0
- data/TODO.impl/99-site-truth-performance-docs.md +31 -0
- data/conformance/vectors.jsonl +2630 -0
- data/data/en/grammar/rules.yaml +851 -0
- data/docs/STABILITY-1.0-AUDIT.md +251 -0
- data/docs/STABILITY-1.0.md +137 -0
- data/docs/cache-architecture.md +502 -0
- data/docs/verification-evidence/correct3/pr_body.md +37 -0
- data/docs/verification-evidence/correct3/rspec_final.log +5588 -0
- data/docs/verification-evidence/correct3/rspec_full.log +4504 -0
- data/docs/verification-evidence/correct3/sv.aff +856 -0
- data/docs/verification-evidence/correct3/sv.dic +153717 -0
- data/examples/01_basic_word_checking.rb +38 -0
- data/examples/02_text_document_checking.rb +77 -0
- data/examples/03_dictionary_backends.rb +137 -0
- data/examples/04_trie_data_structure.rb +146 -0
- data/examples/05_suggestion_algorithms.rb +239 -0
- data/examples/06_configuration_advanced.rb +287 -0
- data/examples/07_multi_language_dictionaries.rb +278 -0
- data/exe/kotoshu +6 -0
- data/lib/kotoshu/algorithms/capitalization.rb +276 -0
- data/lib/kotoshu/algorithms/edit_distance.rb +128 -0
- data/lib/kotoshu/algorithms/lookup/affix_form.rb +132 -0
- data/lib/kotoshu/algorithms/lookup/compound_checks.rb +209 -0
- data/lib/kotoshu/algorithms/lookup/compound_form.rb +50 -0
- data/lib/kotoshu/algorithms/lookup.rb +841 -0
- data/lib/kotoshu/algorithms/ngram_suggest.rb +367 -0
- data/lib/kotoshu/algorithms/permutations.rb +283 -0
- data/lib/kotoshu/algorithms/phonet_suggest.rb +162 -0
- data/lib/kotoshu/algorithms/soundex.rb +92 -0
- data/lib/kotoshu/algorithms/suggest.rb +794 -0
- data/lib/kotoshu/algorithms.rb +23 -0
- data/lib/kotoshu/analyzers/semantic_analyzer.rb +405 -0
- data/lib/kotoshu/analyzers.rb +8 -0
- data/lib/kotoshu/baseline/application.rb +18 -0
- data/lib/kotoshu/baseline/entry.rb +33 -0
- data/lib/kotoshu/baseline/store.rb +175 -0
- data/lib/kotoshu/baseline.rb +20 -0
- data/lib/kotoshu/cache/base_cache.rb +778 -0
- data/lib/kotoshu/cache/cache.rb +91 -0
- data/lib/kotoshu/cache/eviction_policy.rb +66 -0
- data/lib/kotoshu/cache/frequency_cache.rb +283 -0
- data/lib/kotoshu/cache/language_cache.rb +511 -0
- data/lib/kotoshu/cache/lookup_cache.rb +166 -0
- data/lib/kotoshu/cache/model_cache.rb +1147 -0
- data/lib/kotoshu/cache/model_registry.rb +109 -0
- data/lib/kotoshu/cache/staged_languages.rb +45 -0
- data/lib/kotoshu/cache/suggestion_cache.rb +113 -0
- data/lib/kotoshu/cache.rb +49 -0
- data/lib/kotoshu/cli/auto_setup.rb +71 -0
- data/lib/kotoshu/cli/baseline_command.rb +119 -0
- data/lib/kotoshu/cli/batch_reporter.rb +321 -0
- data/lib/kotoshu/cli/cache_command.rb +282 -0
- data/lib/kotoshu/cli/completions_command.rb +210 -0
- data/lib/kotoshu/cli/dict_command.rb +64 -0
- data/lib/kotoshu/cli/directory_check.rb +146 -0
- data/lib/kotoshu/cli/directory_walker.rb +132 -0
- data/lib/kotoshu/cli/display_formatter.rb +429 -0
- data/lib/kotoshu/cli/errors.rb +36 -0
- data/lib/kotoshu/cli/ignore_matcher.rb +205 -0
- data/lib/kotoshu/cli/interactive_reviewer.rb +314 -0
- data/lib/kotoshu/cli/language_resolver.rb +91 -0
- data/lib/kotoshu/cli/navigation_manager.rb +273 -0
- data/lib/kotoshu/cli/personal_command.rb +113 -0
- data/lib/kotoshu/cli/progress_reporter.rb +114 -0
- data/lib/kotoshu/cli/status_report.rb +130 -0
- data/lib/kotoshu/cli.rb +880 -0
- data/lib/kotoshu/components/passthrough_spell_checker.rb +70 -0
- data/lib/kotoshu/components/pos_tagger.rb +98 -0
- data/lib/kotoshu/components/spell_checker.rb +73 -0
- data/lib/kotoshu/components/synthesizer.rb +60 -0
- data/lib/kotoshu/components/tokenizer.rb +58 -0
- data/lib/kotoshu/components/whitespace_tokenizer.rb +94 -0
- data/lib/kotoshu/components.rb +13 -0
- data/lib/kotoshu/configuration/builder.rb +207 -0
- data/lib/kotoshu/configuration/resolver.rb +124 -0
- data/lib/kotoshu/configuration.rb +829 -0
- data/lib/kotoshu/conformance_exporter.rb +256 -0
- data/lib/kotoshu/conformance_runner.rb +176 -0
- data/lib/kotoshu/core/exceptions.rb +186 -0
- data/lib/kotoshu/core/indexed_dictionary.rb +293 -0
- data/lib/kotoshu/core/models/affix_rule.rb +262 -0
- data/lib/kotoshu/core/models/result/document_result.rb +132 -0
- data/lib/kotoshu/core/models/result/word_result.rb +131 -0
- data/lib/kotoshu/core/models/word.rb +131 -0
- data/lib/kotoshu/core/trie/builder.rb +119 -0
- data/lib/kotoshu/core/trie/node.rb +94 -0
- data/lib/kotoshu/core/trie/trie.rb +249 -0
- data/lib/kotoshu/core.rb +28 -0
- data/lib/kotoshu/data/common_words/de.yml +1800 -0
- data/lib/kotoshu/data/common_words/en.yml +1215 -0
- data/lib/kotoshu/data/common_words/es.yml +750 -0
- data/lib/kotoshu/data/common_words/fr.yml +1015 -0
- data/lib/kotoshu/data/common_words/pt.yml +870 -0
- data/lib/kotoshu/data/common_words/ru.yml +484 -0
- data/lib/kotoshu/data/common_words_loader.rb +141 -0
- data/lib/kotoshu/data/frozen_kelly/en.json +1154 -0
- data/lib/kotoshu/data/frozen_kelly.rb +38 -0
- data/lib/kotoshu/data.rb +8 -0
- data/lib/kotoshu/data_structures/bloom_filter.rb +188 -0
- data/lib/kotoshu/data_structures.rb +8 -0
- data/lib/kotoshu/debug_logger.rb +146 -0
- data/lib/kotoshu/debug_mode.rb +136 -0
- data/lib/kotoshu/defaults.rb +84 -0
- data/lib/kotoshu/dictionaries/catalog.rb +814 -0
- data/lib/kotoshu/dictionaries.rb +8 -0
- data/lib/kotoshu/dictionary/base.rb +324 -0
- data/lib/kotoshu/dictionary/cspell.rb +243 -0
- data/lib/kotoshu/dictionary/custom.rb +225 -0
- data/lib/kotoshu/dictionary/hunspell.rb +520 -0
- data/lib/kotoshu/dictionary/plain_text.rb +302 -0
- data/lib/kotoshu/dictionary/repository.rb +246 -0
- data/lib/kotoshu/dictionary/unified.rb +236 -0
- data/lib/kotoshu/dictionary/unix_words.rb +218 -0
- data/lib/kotoshu/dictionary.rb +15 -0
- data/lib/kotoshu/documents/document.rb +120 -0
- data/lib/kotoshu/documents/plain_text_document.rb +73 -0
- data/lib/kotoshu/documents/source_position.rb +45 -0
- data/lib/kotoshu/documents/source_range.rb +76 -0
- data/lib/kotoshu/documents/suppressions/scanner.rb +149 -0
- data/lib/kotoshu/documents/suppressions/suppression.rb +75 -0
- data/lib/kotoshu/documents/suppressions.rb +44 -0
- data/lib/kotoshu/documents/text_node.rb +49 -0
- data/lib/kotoshu/documents.rb +178 -0
- data/lib/kotoshu/embeddings/embedding_pipeline.rb +238 -0
- data/lib/kotoshu/embeddings/lru_cache.rb +244 -0
- data/lib/kotoshu/embeddings/onnx_runtime_model.rb +411 -0
- data/lib/kotoshu/embeddings/protocol.rb +96 -0
- data/lib/kotoshu/embeddings/protocols.rb +23 -0
- data/lib/kotoshu/embeddings/registry.rb +184 -0
- data/lib/kotoshu/embeddings/search.rb +204 -0
- data/lib/kotoshu/embeddings/similarity_engine.rb +278 -0
- data/lib/kotoshu/embeddings/similarity_search.rb +328 -0
- data/lib/kotoshu/embeddings/vocabulary.rb +306 -0
- data/lib/kotoshu/embeddings.rb +87 -0
- data/lib/kotoshu/fluent_checker.rb +94 -0
- data/lib/kotoshu/grammar/pattern_matchers/base_matcher.rb +48 -0
- data/lib/kotoshu/grammar/pattern_matchers/double_negative_matcher.rb +179 -0
- data/lib/kotoshu/grammar/pattern_matchers/phrase_matcher.rb +86 -0
- data/lib/kotoshu/grammar/pattern_matchers/possessive_context_matcher.rb +75 -0
- data/lib/kotoshu/grammar/pattern_matchers/possessive_contraction_matcher.rb +87 -0
- data/lib/kotoshu/grammar/pattern_matchers/sentence_start_matcher.rb +84 -0
- data/lib/kotoshu/grammar/pattern_matchers/vowel_sound_matcher.rb +81 -0
- data/lib/kotoshu/grammar/pattern_matchers/word_list_matcher.rb +66 -0
- data/lib/kotoshu/grammar/rule.rb +99 -0
- data/lib/kotoshu/grammar/rule_engine.rb +96 -0
- data/lib/kotoshu/grammar/rule_loader.rb +30 -0
- data/lib/kotoshu/grammar.rb +24 -0
- data/lib/kotoshu/integrity/audit_log.rb +167 -0
- data/lib/kotoshu/integrity/manifest.rb +116 -0
- data/lib/kotoshu/integrity/net_http.rb +51 -0
- data/lib/kotoshu/integrity/rotation_policy.rb +88 -0
- data/lib/kotoshu/integrity.rb +26 -0
- data/lib/kotoshu/jekyll.rb +101 -0
- data/lib/kotoshu/keyboard/layout.rb +116 -0
- data/lib/kotoshu/keyboard/layouts/arabic.rb +59 -0
- data/lib/kotoshu/keyboard/layouts/azerty.rb +55 -0
- data/lib/kotoshu/keyboard/layouts/bulgarian.rb +57 -0
- data/lib/kotoshu/keyboard/layouts/devanagari_inscript.rb +52 -0
- data/lib/kotoshu/keyboard/layouts/dubeolsik.rb +56 -0
- data/lib/kotoshu/keyboard/layouts/dvorak.rb +54 -0
- data/lib/kotoshu/keyboard/layouts/greek.rb +52 -0
- data/lib/kotoshu/keyboard/layouts/hebrew.rb +57 -0
- data/lib/kotoshu/keyboard/layouts/hrsl.rb +94 -0
- data/lib/kotoshu/keyboard/layouts/jcuken.rb +57 -0
- data/lib/kotoshu/keyboard/layouts/latin.rb +266 -0
- data/lib/kotoshu/keyboard/layouts/persian.rb +58 -0
- data/lib/kotoshu/keyboard/layouts/qwerty.rb +52 -0
- data/lib/kotoshu/keyboard/layouts/qwertz.rb +55 -0
- data/lib/kotoshu/keyboard/layouts/serbian.rb +59 -0
- data/lib/kotoshu/keyboard/layouts/turkish_q.rb +52 -0
- data/lib/kotoshu/keyboard/layouts/ukrainian.rb +51 -0
- data/lib/kotoshu/keyboard/registry.rb +201 -0
- data/lib/kotoshu/keyboard.rb +96 -0
- data/lib/kotoshu/kotoshu_native.so +0 -0
- data/lib/kotoshu/language/detector.rb +242 -0
- data/lib/kotoshu/language/identifier.rb +376 -0
- data/lib/kotoshu/language/languages/base.rb +271 -0
- data/lib/kotoshu/language/lid_detector.rb +188 -0
- data/lib/kotoshu/language/normalizer/arabic.rb +113 -0
- data/lib/kotoshu/language/normalizer/base.rb +133 -0
- data/lib/kotoshu/language/normalizer/greek.rb +63 -0
- data/lib/kotoshu/language/normalizer/hebrew.rb +72 -0
- data/lib/kotoshu/language/normalizer/persian.rb +36 -0
- data/lib/kotoshu/language/normalizer/turkish.rb +38 -0
- data/lib/kotoshu/language/registry.rb +209 -0
- data/lib/kotoshu/language/resources/ar/common_words.txt +6753 -0
- data/lib/kotoshu/language/resources/ar/confusion_sets.txt +11 -0
- data/lib/kotoshu/language/resources/de/common_words.txt +10003 -0
- data/lib/kotoshu/language/resources/de/confusion_sets.txt +246 -0
- data/lib/kotoshu/language/resources/en/common_words.txt +9979 -0
- data/lib/kotoshu/language/resources/en/confusion_sets.txt +871 -0
- data/lib/kotoshu/language/resources/es/common_words.txt +9992 -0
- data/lib/kotoshu/language/resources/es/confusion_sets.txt +17 -0
- data/lib/kotoshu/language/resources/fr/common_words.txt +9993 -0
- data/lib/kotoshu/language/resources/fr/confusion_sets.txt +76 -0
- data/lib/kotoshu/language/resources/pt/common_words.txt +9977 -0
- data/lib/kotoshu/language/resources/pt/confusion_sets.txt +18 -0
- data/lib/kotoshu/language/resources/ru/common_words.txt +9951 -0
- data/lib/kotoshu/language/resources/ru/confusion_sets.txt +5 -0
- data/lib/kotoshu/language/script.rb +128 -0
- data/lib/kotoshu/language/segmenter.rb +122 -0
- data/lib/kotoshu/language/suika.rb +38 -0
- data/lib/kotoshu/language/tokenizer/base.rb +187 -0
- data/lib/kotoshu/language/tokenizer/cyrillic_tokenizer.rb +76 -0
- data/lib/kotoshu/language/tokenizer/devanagari_tokenizer.rb +70 -0
- data/lib/kotoshu/language/tokenizer/french_tokenizer.rb +172 -0
- data/lib/kotoshu/language/tokenizer/german_tokenizer.rb +41 -0
- data/lib/kotoshu/language/tokenizer/greek_tokenizer.rb +72 -0
- data/lib/kotoshu/language/tokenizer/hangul_tokenizer.rb +70 -0
- data/lib/kotoshu/language/tokenizer/japanese_tokenizer.rb +55 -0
- data/lib/kotoshu/language/tokenizer/latin_tokenizer.rb +153 -0
- data/lib/kotoshu/language/tokenizer/portuguese_tokenizer.rb +160 -0
- data/lib/kotoshu/language/tokenizer/russian_tokenizer.rb +93 -0
- data/lib/kotoshu/language/tokenizer/script_tokenizer.rb +62 -0
- data/lib/kotoshu/language/tokenizer/spanish_tokenizer.rb +122 -0
- data/lib/kotoshu/language.rb +127 -0
- data/lib/kotoshu/languages/ar/language.rb +98 -0
- data/lib/kotoshu/languages/bg/language.rb +37 -0
- data/lib/kotoshu/languages/ca/language.rb +22 -0
- data/lib/kotoshu/languages/cs/language.rb +22 -0
- data/lib/kotoshu/languages/da/language.rb +22 -0
- data/lib/kotoshu/languages/de/language.rb +554 -0
- data/lib/kotoshu/languages/el/language.rb +50 -0
- data/lib/kotoshu/languages/en/language.rb +470 -0
- data/lib/kotoshu/languages/es/language.rb +476 -0
- data/lib/kotoshu/languages/et/language.rb +24 -0
- data/lib/kotoshu/languages/fa/language.rb +91 -0
- data/lib/kotoshu/languages/fr/language.rb +505 -0
- data/lib/kotoshu/languages/he/language.rb +98 -0
- data/lib/kotoshu/languages/hr/language.rb +24 -0
- data/lib/kotoshu/languages/hu/language.rb +22 -0
- data/lib/kotoshu/languages/id/language.rb +25 -0
- data/lib/kotoshu/languages/it/language.rb +22 -0
- data/lib/kotoshu/languages/ja/language.rb +476 -0
- data/lib/kotoshu/languages/ko/language.rb +56 -0
- data/lib/kotoshu/languages/latin_base.rb +53 -0
- data/lib/kotoshu/languages/lt/language.rb +24 -0
- data/lib/kotoshu/languages/lv/language.rb +24 -0
- data/lib/kotoshu/languages/nb/language.rb +25 -0
- data/lib/kotoshu/languages/ne/language.rb +60 -0
- data/lib/kotoshu/languages/nl/language.rb +22 -0
- data/lib/kotoshu/languages/nn/language.rb +26 -0
- data/lib/kotoshu/languages/pl/language.rb +22 -0
- data/lib/kotoshu/languages/pt/language.rb +439 -0
- data/lib/kotoshu/languages/ro/language.rb +22 -0
- data/lib/kotoshu/languages/ru/language.rb +420 -0
- data/lib/kotoshu/languages/sk/language.rb +25 -0
- data/lib/kotoshu/languages/sl/language.rb +25 -0
- data/lib/kotoshu/languages/sr/language.rb +39 -0
- data/lib/kotoshu/languages/sr-Latn/language.rb +35 -0
- data/lib/kotoshu/languages/sv/language.rb +22 -0
- data/lib/kotoshu/languages/tr/language.rb +36 -0
- data/lib/kotoshu/languages/uk/language.rb +38 -0
- data/lib/kotoshu/languages/vi/language.rb +22 -0
- data/lib/kotoshu/languages.rb +64 -0
- data/lib/kotoshu/metrics_collector.rb +238 -0
- data/lib/kotoshu/metrics_module.rb +121 -0
- data/lib/kotoshu/models/context.rb +119 -0
- data/lib/kotoshu/models/embedding_model.rb +181 -0
- data/lib/kotoshu/models/fasttext_model.rb +212 -0
- data/lib/kotoshu/models/nearest_neighbor.rb +96 -0
- data/lib/kotoshu/models/onnx_model.rb +330 -0
- data/lib/kotoshu/models/semantic_error.rb +204 -0
- data/lib/kotoshu/models/suggestion.rb +106 -0
- data/lib/kotoshu/models/word_embedding.rb +97 -0
- data/lib/kotoshu/models.rb +27 -0
- data/lib/kotoshu/multi_language_checker.rb +127 -0
- data/lib/kotoshu/native.rb +83 -0
- data/lib/kotoshu/native_backend.rb +131 -0
- data/lib/kotoshu/paths.rb +79 -0
- data/lib/kotoshu/personal_dictionary.rb +105 -0
- data/lib/kotoshu/plugins/plugin.rb +61 -0
- data/lib/kotoshu/plugins/registry.rb +118 -0
- data/lib/kotoshu/plugins.rb +9 -0
- data/lib/kotoshu/project_config.rb +76 -0
- data/lib/kotoshu/readers/aff_data.rb +530 -0
- data/lib/kotoshu/readers/aff_reader.rb +393 -0
- data/lib/kotoshu/readers/condition_checker.rb +125 -0
- data/lib/kotoshu/readers/dic_reader.rb +208 -0
- data/lib/kotoshu/readers/file_reader.rb +377 -0
- data/lib/kotoshu/readers/lookup_builder.rb +584 -0
- data/lib/kotoshu/readers/readers.rb +17 -0
- data/lib/kotoshu/readers.rb +25 -0
- data/lib/kotoshu/resource_bundle.rb +55 -0
- data/lib/kotoshu/resource_manager.rb +600 -0
- data/lib/kotoshu/results/result.rb +165 -0
- data/lib/kotoshu/results.rb +8 -0
- data/lib/kotoshu/rspec.rb +163 -0
- data/lib/kotoshu/scripts/fasttext_to_onnx.py +275 -0
- data/lib/kotoshu/source_registry.rb +104 -0
- data/lib/kotoshu/spellchecker/parallel_checker.rb +90 -0
- data/lib/kotoshu/spellchecker.rb +406 -0
- data/lib/kotoshu/string_metrics.rb +153 -0
- data/lib/kotoshu/suggestions/context.rb +43 -0
- data/lib/kotoshu/suggestions/frequency_provider.rb +80 -0
- data/lib/kotoshu/suggestions/generator.rb +169 -0
- data/lib/kotoshu/suggestions/pipeline.rb +135 -0
- data/lib/kotoshu/suggestions/semantic_cascade.rb +73 -0
- data/lib/kotoshu/suggestions/strategies/base_strategy.rb +315 -0
- data/lib/kotoshu/suggestions/strategies/composite_strategy.rb +197 -0
- data/lib/kotoshu/suggestions/strategies/edit_distance_strategy.rb +631 -0
- data/lib/kotoshu/suggestions/strategies/keyboard_proximity_strategy.rb +207 -0
- data/lib/kotoshu/suggestions/strategies/ngram_strategy.rb +165 -0
- data/lib/kotoshu/suggestions/strategies/phonetic_strategy.rb +284 -0
- data/lib/kotoshu/suggestions/strategies/semantic_strategy.rb +317 -0
- data/lib/kotoshu/suggestions/strategies/symspell_strategy.rb +270 -0
- data/lib/kotoshu/suggestions/strategies/word_index.rb +78 -0
- data/lib/kotoshu/suggestions/suggestion.rb +154 -0
- data/lib/kotoshu/suggestions/suggestion_set.rb +270 -0
- data/lib/kotoshu/suggestions/sweep_index.rb +137 -0
- data/lib/kotoshu/suggestions.rb +29 -0
- data/lib/kotoshu/tasks/check_task.rb +104 -0
- data/lib/kotoshu/tasks.rb +11 -0
- data/lib/kotoshu/validators/spelling_validator.rb +101 -0
- data/lib/kotoshu/validators.rb +9 -0
- data/lib/kotoshu/version.rb +5 -0
- data/lib/kotoshu.rb +538 -0
- data/rakelib/compile.rake +10 -0
- data/script/validate_all_dictionaries.rb +444 -0
- data/scripts/build_native_gem.rb +45 -0
- data/scripts/extract_eval_grids.rb +37 -0
- data/scripts/generate_frozen_tiers.rb +44 -0
- data/sig/kotoshu.rbs +435 -0
- data/test_oop.rb +79 -0
- metadata +546 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
|
|
5
|
+
module Kotoshu
|
|
6
|
+
module Models
|
|
7
|
+
module Result
|
|
8
|
+
# Result object for checking a single word.
|
|
9
|
+
#
|
|
10
|
+
# Serialized via lutaml-model. The +suggestions+ attribute is a
|
|
11
|
+
# collection of {Suggestions::Suggestion}. For rich query semantics
|
|
12
|
+
# (filtering by source, confidence, distance), wrap with
|
|
13
|
+
# {Suggestions::SuggestionSet} via +#to_suggestion_set+.
|
|
14
|
+
class WordResult < Lutaml::Model::Serializable
|
|
15
|
+
# What absorbed a suppressed misspelling (plan 82): an inline
|
|
16
|
+
# `kotoshu:disable-*` directive or a baseline entry.
|
|
17
|
+
SUPPRESSED_BY_INLINE = "inline"
|
|
18
|
+
SUPPRESSED_BY_BASELINE = "baseline"
|
|
19
|
+
attribute :word, :string, default: ""
|
|
20
|
+
attribute :correct, :boolean, default: true
|
|
21
|
+
attribute :position, :integer
|
|
22
|
+
attribute :suggestions, Suggestions::Suggestion, collection: true
|
|
23
|
+
attribute :metadata, :hash, default: {}
|
|
24
|
+
# Suppression bookkeeping: a suppressed word is a real misspelling
|
|
25
|
+
# absorbed by an inline ignore directive or a baseline entry.
|
|
26
|
+
# Suppressed errors leave DocumentResult#errors and surface via
|
|
27
|
+
# DocumentResult#suppressed_errors.
|
|
28
|
+
attribute :suppressed, :boolean, default: false
|
|
29
|
+
attribute :suppressed_by, :string
|
|
30
|
+
|
|
31
|
+
# lutaml-model calls +new(**attrs)+ when deserializing via
|
|
32
|
+
# +from_hash+. The signature accepts every attribute as a
|
|
33
|
+
# keyword plus a splat for framework-private keywords (e.g.
|
|
34
|
+
# +lutaml_register+) so the round-trip works without lutaml
|
|
35
|
+
# having to poke ivars directly.
|
|
36
|
+
#
|
|
37
|
+
# +suggestions+ accepts a {Suggestions::SuggestionSet},
|
|
38
|
+
# an Array, or nil for ergonomic construction; the SuggestionSet
|
|
39
|
+
# case is unwrapped to its underlying Array.
|
|
40
|
+
def initialize(word: "", correct: true, suggestions: nil, position: nil, metadata: {}, suppressed: false, suppressed_by: nil, **kwargs)
|
|
41
|
+
suggestions_array =
|
|
42
|
+
case suggestions
|
|
43
|
+
when Suggestions::SuggestionSet then suggestions.suggestions
|
|
44
|
+
when Array then suggestions
|
|
45
|
+
when nil then []
|
|
46
|
+
else raise ArgumentError, "suggestions must be SuggestionSet, Array, or nil"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
super(
|
|
50
|
+
word: word.to_s,
|
|
51
|
+
correct: correct,
|
|
52
|
+
position: position,
|
|
53
|
+
suggestions: suggestions_array,
|
|
54
|
+
metadata: metadata,
|
|
55
|
+
suppressed: suppressed,
|
|
56
|
+
suppressed_by: suppressed_by,
|
|
57
|
+
**kwargs
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def correct?
|
|
62
|
+
correct
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def incorrect?
|
|
66
|
+
!correct
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# True when an inline directive or a baseline absorbed this
|
|
70
|
+
# misspelling (see +suppressed_by+).
|
|
71
|
+
def suppressed?
|
|
72
|
+
suppressed
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def has_suggestions?
|
|
76
|
+
!suggestions.empty?
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def suggestion_count
|
|
80
|
+
suggestions.size
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def top_suggestions(n = 3)
|
|
84
|
+
suggestions.first(n).map(&:word)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def first_suggestion
|
|
88
|
+
suggestions.first&.word
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Wrap the suggestions array in a {Suggestions::SuggestionSet}
|
|
92
|
+
# for rich query (filter by source, confidence, distance).
|
|
93
|
+
#
|
|
94
|
+
# @return [Suggestions::SuggestionSet]
|
|
95
|
+
def to_suggestion_set
|
|
96
|
+
Suggestions::SuggestionSet.new(suggestions)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def ==(other)
|
|
100
|
+
return false unless other.is_a?(WordResult)
|
|
101
|
+
|
|
102
|
+
word == other.word && correct == other.correct
|
|
103
|
+
end
|
|
104
|
+
alias eql? ==
|
|
105
|
+
|
|
106
|
+
def hash
|
|
107
|
+
[word, correct].hash
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def to_s
|
|
111
|
+
if correct
|
|
112
|
+
word
|
|
113
|
+
elsif has_suggestions?
|
|
114
|
+
"#{word} (did you mean #{top_suggestions(3).join(', ')}?)"
|
|
115
|
+
else
|
|
116
|
+
"#{word} (no suggestions)"
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
alias inspect to_s
|
|
120
|
+
|
|
121
|
+
def self.correct(word, position: nil)
|
|
122
|
+
new(word: word, correct: true, position: position)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def self.incorrect(word, suggestions: nil, position: nil)
|
|
126
|
+
new(word: word, correct: false, suggestions: suggestions, position: position)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kotoshu
|
|
4
|
+
module Models
|
|
5
|
+
# Word model representing a dictionary word with metadata.
|
|
6
|
+
#
|
|
7
|
+
# This is a value object that represents a word in the dictionary
|
|
8
|
+
# along with its morphological information (flags and data).
|
|
9
|
+
#
|
|
10
|
+
# @note This class is immutable and frozen on initialization.
|
|
11
|
+
#
|
|
12
|
+
# @example Creating a word
|
|
13
|
+
# word = Models::Word.new("hello", flags: ["noun"], morphological_data: { root: "hell" })
|
|
14
|
+
# word.text # => "hello"
|
|
15
|
+
# word.valid? # => true
|
|
16
|
+
class Word
|
|
17
|
+
# @return [String] The word text
|
|
18
|
+
attr_reader :text
|
|
19
|
+
|
|
20
|
+
# @return [Array<String>] Morphological flags (e.g., "noun", "verb")
|
|
21
|
+
attr_reader :flags
|
|
22
|
+
|
|
23
|
+
# @return [Hash] Additional morphological data
|
|
24
|
+
attr_reader :morphological_data
|
|
25
|
+
|
|
26
|
+
# Create a new Word.
|
|
27
|
+
#
|
|
28
|
+
# @param text [String] The word text
|
|
29
|
+
# @param flags [Array<String>] Morphological flags (optional)
|
|
30
|
+
# @param morphological_data [Hash] Additional morphological data (optional)
|
|
31
|
+
def initialize(text, flags: [], morphological_data: {})
|
|
32
|
+
raise ArgumentError, "Text cannot be empty" if text.nil? || text.empty?
|
|
33
|
+
|
|
34
|
+
@text = text.dup.freeze
|
|
35
|
+
@flags = flags.dup.freeze
|
|
36
|
+
@morphological_data = morphological_data.dup.freeze
|
|
37
|
+
|
|
38
|
+
freeze
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Check if the word is valid (has content).
|
|
42
|
+
#
|
|
43
|
+
# @return [Boolean] True if the word is valid
|
|
44
|
+
def valid?
|
|
45
|
+
!@text.nil? && !@text.empty?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Check if the word has a specific flag.
|
|
49
|
+
#
|
|
50
|
+
# @param flag [String] The flag to check
|
|
51
|
+
# @return [Boolean] True if the word has the flag
|
|
52
|
+
def has_flag?(flag)
|
|
53
|
+
@flags.include?(flag)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Check if the word has any flags.
|
|
57
|
+
#
|
|
58
|
+
# @return [Boolean] True if the word has flags
|
|
59
|
+
def has_flags?
|
|
60
|
+
!@flags.empty?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Get the length of the word.
|
|
64
|
+
#
|
|
65
|
+
# @return [Integer] Word length
|
|
66
|
+
def length
|
|
67
|
+
@text.length
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Check if the word is empty.
|
|
71
|
+
#
|
|
72
|
+
# @return [Boolean] True if the word is empty
|
|
73
|
+
def empty?
|
|
74
|
+
@text.empty?
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Convert to string.
|
|
78
|
+
#
|
|
79
|
+
# @return [String] The word text
|
|
80
|
+
def to_s
|
|
81
|
+
@text
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Check equality based on text.
|
|
85
|
+
#
|
|
86
|
+
# @param other [Word, String] The other object
|
|
87
|
+
# @return [Boolean] True if equal
|
|
88
|
+
def ==(other)
|
|
89
|
+
return false unless other.is_a?(Word)
|
|
90
|
+
|
|
91
|
+
@text == other.text
|
|
92
|
+
end
|
|
93
|
+
alias eql? ==
|
|
94
|
+
|
|
95
|
+
# Hash based on text.
|
|
96
|
+
#
|
|
97
|
+
# @return [Integer] Hash code
|
|
98
|
+
def hash
|
|
99
|
+
@text.hash
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Compare words by text.
|
|
103
|
+
#
|
|
104
|
+
# @param other [Word] The other word
|
|
105
|
+
# @return [Integer] Comparison result
|
|
106
|
+
def <=>(other)
|
|
107
|
+
return nil unless other.is_a?(Word)
|
|
108
|
+
|
|
109
|
+
@text <=> other.text
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Create a word from a Hunspell dictionary line.
|
|
113
|
+
#
|
|
114
|
+
# @param line [String] Dictionary line (e.g., "hello/flag" or "hello")
|
|
115
|
+
# @return [Word] New word instance
|
|
116
|
+
#
|
|
117
|
+
# @example
|
|
118
|
+
# Word.from_dic_line("hello/N") # => Word with text "hello" and flag "N"
|
|
119
|
+
# Word.from_dic_line("hello") # => Word with text "hello" and no flags
|
|
120
|
+
def self.from_dic_line(line)
|
|
121
|
+
return nil if line.nil? || line.empty?
|
|
122
|
+
|
|
123
|
+
parts = line.split("/", 2)
|
|
124
|
+
text = parts[0]
|
|
125
|
+
flags = parts[1] ? parts[1].split("") : []
|
|
126
|
+
|
|
127
|
+
new(text, flags: flags)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kotoshu
|
|
4
|
+
module Core
|
|
5
|
+
module Trie
|
|
6
|
+
# Builder class for constructing Trie objects.
|
|
7
|
+
# Provides a fluent interface for building tries from various sources.
|
|
8
|
+
class Builder
|
|
9
|
+
def initialize
|
|
10
|
+
@trie = Trie.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Add a single word to the trie.
|
|
14
|
+
#
|
|
15
|
+
# @param word [String] The word to add
|
|
16
|
+
# @param payload [Object] Optional payload
|
|
17
|
+
# @return [Builder] Self for chaining
|
|
18
|
+
def add_word(word, payload = nil)
|
|
19
|
+
@trie.insert(word, payload)
|
|
20
|
+
self
|
|
21
|
+
end
|
|
22
|
+
alias << add_word
|
|
23
|
+
|
|
24
|
+
# Add multiple words to the trie.
|
|
25
|
+
#
|
|
26
|
+
# @param words [Array<String>] Array of words to add
|
|
27
|
+
# @return [Builder] Self for chaining
|
|
28
|
+
def add_words(words)
|
|
29
|
+
words.each { |word| add_word(word) }
|
|
30
|
+
self
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Build a trie from a hash (word => payload mapping).
|
|
34
|
+
#
|
|
35
|
+
# @param hash [Hash] Hash of words to payloads
|
|
36
|
+
# @return [Builder] Self for chaining
|
|
37
|
+
def from_hash(hash)
|
|
38
|
+
hash.each { |word, payload| add_word(word, payload) }
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Build a trie from an array of words.
|
|
43
|
+
#
|
|
44
|
+
# @param array [Array<String>] Array of words
|
|
45
|
+
# @return [Builder] Self for chaining
|
|
46
|
+
def from_array(array)
|
|
47
|
+
add_words(array)
|
|
48
|
+
self
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Build a trie from a file (one word per line).
|
|
52
|
+
#
|
|
53
|
+
# @param path [String] Path to the file
|
|
54
|
+
# @return [Builder] Self for chaining
|
|
55
|
+
def from_file(path)
|
|
56
|
+
File.foreach(path, chomp: true) do |line|
|
|
57
|
+
next if line.empty? || line.start_with?("#")
|
|
58
|
+
|
|
59
|
+
add_word(line)
|
|
60
|
+
end
|
|
61
|
+
self
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Build a trie from a string (newline-separated words).
|
|
65
|
+
#
|
|
66
|
+
# @param text [String] String containing words
|
|
67
|
+
# @return [Builder] Self for chaining
|
|
68
|
+
def from_string(text)
|
|
69
|
+
text.each_line do |line|
|
|
70
|
+
word = line.strip
|
|
71
|
+
next if word.empty? || word.start_with?("#")
|
|
72
|
+
|
|
73
|
+
add_word(word)
|
|
74
|
+
end
|
|
75
|
+
self
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Get the built trie.
|
|
79
|
+
#
|
|
80
|
+
# @return [Trie] The constructed trie
|
|
81
|
+
def build
|
|
82
|
+
@trie.freeze
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Build a trie from a file path (class method).
|
|
86
|
+
#
|
|
87
|
+
# @param path [String] Path to the file
|
|
88
|
+
# @return [Trie] The constructed trie
|
|
89
|
+
def self.from_file(path)
|
|
90
|
+
new.from_file(path).build
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Build a trie from an array of words (class method).
|
|
94
|
+
#
|
|
95
|
+
# @param words [Array<String>] Array of words
|
|
96
|
+
# @return [Trie] The constructed trie
|
|
97
|
+
def self.from_array(words)
|
|
98
|
+
new.from_array(words).build
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Build a trie from a hash (class method).
|
|
102
|
+
#
|
|
103
|
+
# @param hash [Hash] Hash of words to payloads
|
|
104
|
+
# @return [Trie] The constructed trie
|
|
105
|
+
def self.from_hash(hash)
|
|
106
|
+
new.from_hash(hash).build
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Build a trie from a string (class method).
|
|
110
|
+
#
|
|
111
|
+
# @param text [String] String containing words
|
|
112
|
+
# @return [Trie] The constructed trie
|
|
113
|
+
def self.from_string(text)
|
|
114
|
+
new.from_string(text).build
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kotoshu
|
|
4
|
+
module Core
|
|
5
|
+
module Trie
|
|
6
|
+
# Node in the Trie data structure.
|
|
7
|
+
# Each node represents a character and its children.
|
|
8
|
+
class Node
|
|
9
|
+
attr_reader :character, :children, :terminal, :payload
|
|
10
|
+
|
|
11
|
+
def initialize(character = "")
|
|
12
|
+
@character = character
|
|
13
|
+
@children = {}
|
|
14
|
+
@terminal = false
|
|
15
|
+
@payload = nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Add a child node for the given character.
|
|
19
|
+
#
|
|
20
|
+
# @param character [String] The character to add
|
|
21
|
+
# @return [Node] The new or existing child node
|
|
22
|
+
def add_child(character)
|
|
23
|
+
@children[character] ||= Node.new(character)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Get child node for the given character.
|
|
27
|
+
#
|
|
28
|
+
# @param character [String] The character to look up
|
|
29
|
+
# @return [Node, nil] The child node or nil if not found
|
|
30
|
+
def child(character)
|
|
31
|
+
@children[character]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Check if this node has a child for the given character.
|
|
35
|
+
#
|
|
36
|
+
# @param character [String] The character to check
|
|
37
|
+
# @return [Boolean] True if child exists
|
|
38
|
+
def has_child?(character)
|
|
39
|
+
@children.key?(character)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Mark this node as terminal (end of a word).
|
|
43
|
+
#
|
|
44
|
+
# @param payload [Object] Optional payload to store at this node
|
|
45
|
+
def mark_terminal(payload = nil)
|
|
46
|
+
@terminal = true
|
|
47
|
+
@payload = payload
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Check if this node is terminal.
|
|
51
|
+
#
|
|
52
|
+
# @return [Boolean] True if this is the end of a word
|
|
53
|
+
def terminal?
|
|
54
|
+
@terminal
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Get all children of this node.
|
|
58
|
+
#
|
|
59
|
+
# @return [Hash] Hash of character to node mappings
|
|
60
|
+
def all_children
|
|
61
|
+
@children
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Check if this node has any children.
|
|
65
|
+
#
|
|
66
|
+
# @return [Boolean] True if there are children
|
|
67
|
+
def has_children?
|
|
68
|
+
!@children.empty?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Get the number of children.
|
|
72
|
+
#
|
|
73
|
+
# @return [Integer] Number of child nodes
|
|
74
|
+
def child_count
|
|
75
|
+
@children.size
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Convert node to string representation.
|
|
79
|
+
#
|
|
80
|
+
# @return [String] String representation
|
|
81
|
+
def to_s
|
|
82
|
+
"Node('#{@character}', terminal: #{@terminal}, children: #{@children.keys})"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Inspect the node.
|
|
86
|
+
#
|
|
87
|
+
# @return [String] Inspection string
|
|
88
|
+
def inspect
|
|
89
|
+
to_s
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|