rphonetic 0.1.0

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.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/Cargo.lock +402 -0
  3. data/Cargo.toml +6 -0
  4. data/LICENSE +202 -0
  5. data/NOTICE +17 -0
  6. data/README.md +189 -0
  7. data/ext/rphonetic/Cargo.toml +13 -0
  8. data/ext/rphonetic/extconf.rb +8 -0
  9. data/ext/rphonetic/src/lib.rs +275 -0
  10. data/lib/rphonetic/beider_morse.rb +54 -0
  11. data/lib/rphonetic/caverphone1.rb +15 -0
  12. data/lib/rphonetic/caverphone2.rb +15 -0
  13. data/lib/rphonetic/cologne.rb +15 -0
  14. data/lib/rphonetic/daitch_mokotoff_soundex.rb +35 -0
  15. data/lib/rphonetic/double_metaphone.rb +52 -0
  16. data/lib/rphonetic/encoder.rb +93 -0
  17. data/lib/rphonetic/encoders.rb +19 -0
  18. data/lib/rphonetic/match_rating_approach.rb +18 -0
  19. data/lib/rphonetic/metaphone.rb +17 -0
  20. data/lib/rphonetic/nysiis.rb +19 -0
  21. data/lib/rphonetic/phonex.rb +18 -0
  22. data/lib/rphonetic/refined_soundex.rb +35 -0
  23. data/lib/rphonetic/soundex.rb +38 -0
  24. data/lib/rphonetic/version.rb +6 -0
  25. data/lib/rphonetic.rb +25 -0
  26. data/sig/rphonetic/beider_morse.rbs +14 -0
  27. data/sig/rphonetic/caverphone1.rbs +5 -0
  28. data/sig/rphonetic/caverphone2.rbs +5 -0
  29. data/sig/rphonetic/cologne.rbs +5 -0
  30. data/sig/rphonetic/daitch_mokotoff_soundex.rbs +9 -0
  31. data/sig/rphonetic/double_metaphone.rbs +11 -0
  32. data/sig/rphonetic/encoder.rbs +11 -0
  33. data/sig/rphonetic/match_rating_approach.rbs +5 -0
  34. data/sig/rphonetic/metaphone.rbs +7 -0
  35. data/sig/rphonetic/nysiis.rbs +7 -0
  36. data/sig/rphonetic/phonex.rbs +7 -0
  37. data/sig/rphonetic/refined_soundex.rbs +11 -0
  38. data/sig/rphonetic/soundex.rbs +12 -0
  39. data/sig/rphonetic.rbs +7 -0
  40. metadata +97 -0
data/README.md ADDED
@@ -0,0 +1,189 @@
1
+ # rphonetic
2
+
3
+ Ruby bindings for all twelve algorithms in the [rphonetic Rust crate](https://github.com/Dalvany/rphonetic).
4
+ The wrapper pins rphonetic 4.0.0 and uses Magnus. Supports CRuby 3.4 and 4.0.
5
+
6
+ ## Installation
7
+
8
+ ```ruby
9
+ gem "rphonetic", "~> 0.1.0"
10
+ ```
11
+
12
+ Build a local source gem with
13
+ `bundle exec rake build`. Installing the source gem needs Rust and a C toolchain.
14
+ The included CI workflow builds platform gems for Linux glibc (x64/ARM64),
15
+ macOS (Intel/Apple Silicon), and Windows x64 UCRT, with binaries for Ruby
16
+ 3.4 and 4.0 in each package. Those packages require no Rust toolchain
17
+ at installation. CI verifies builds and installed gems on each supported target.
18
+ Linux builds target glibc 2.35 or newer; Alpine/musl is not included.
19
+
20
+ ## API
21
+
22
+ Every encoder provides `encode(string)`, `encode_many(array)`, and
23
+ `match?(first, second)`. There are also class-level `encode` and `encode_many`
24
+ conveniences, which construct an encoder per call. Reuse instances when encoding
25
+ many names, especially when rules need parsing.
26
+
27
+ ```ruby
28
+ require "rphonetic"
29
+
30
+ encoder = RPhonetic::DoubleMetaphone.new(max_length: 4)
31
+ encoder.encode("Smith") # => "SM0"
32
+ encoder.codes("Smith") # => ["SM0", "XMT"]
33
+ encoder.alternate("Smith") # => "XMT"
34
+ encoder.encode_many(["Smith", "Schmidt"]) # => ["SM0", "XMT"]
35
+ RPhonetic::DoubleMetaphone.codes("Smith") # => ["SM0", "XMT"]
36
+ RPhonetic::Soundex.encode("Robert") # => "R163"
37
+ ```
38
+
39
+ `encode` consistently returns the upstream primary/single code as a String.
40
+ For Double Metaphone, `codes` always returns two strings, including duplicates
41
+ when primary and alternate are identical. For Daitch–Mokotoff, `codes` returns
42
+ all branching codes as an Array. Beider–Morse returns the upstream expression
43
+ string, preserving its parentheses, alternatives, and word groupings.
44
+
45
+ | Class under `RPhonetic` | Constructor options | Additional methods |
46
+ | --- | --- | --- |
47
+ | `Caverphone1` | None | |
48
+ | `Caverphone2` | None | |
49
+ | `Cologne` | None | |
50
+ | `MatchRatingApproach` | None | |
51
+ | `Metaphone` | `max_length: 4` (`nil` for unlimited) | |
52
+ | `DoubleMetaphone` | `max_length: 4` (`nil` for unlimited) | `codes`, `alternate` |
53
+ | `Nysiis` | `strict: true` | |
54
+ | `Phonex` | `max_length: 4` | |
55
+ | `Soundex` | `mapping: DEFAULT_MAPPING`, `special_case_h_w: true` | `difference` |
56
+ | `RefinedSoundex` | `mapping: DEFAULT_MAPPING` | `difference` |
57
+ | `DaitchMokotoffSoundex` | Required `rules:`, `ascii_folding: true` | `codes` |
58
+ | `BeiderMorse` | Required `rules_path:`, `name_type: :generic`, `rule_type: :approx`, `concat: true`, `max_phonemes: 20` | `encode(value, languages: nil)` |
59
+
60
+ Lengths must be positive integers; only Metaphone and Double Metaphone support
61
+ `nil`. Custom Soundex mappings contain 26 characters, corresponding to A–Z.
62
+ `Soundex::GENEALOGY_MAPPING` is also available. `difference` counts matching
63
+ positions in the codes. `match?` delegates to the crate: it compares primary
64
+ codes for Double Metaphone and uses the rating algorithm for Match Rating
65
+ Approach. To match either Double Metaphone code, compare the nonempty codes:
66
+
67
+ ```ruby
68
+ left = encoder.codes("Smith").reject(&:empty?)
69
+ right = encoder.codes("Schmidt").reject(&:empty?)
70
+ left.intersect?(right) # => true
71
+ ```
72
+
73
+ ## External rules
74
+
75
+ No default rule files are embedded in or shipped with the gem. Supply compatible
76
+ [Apache Commons Codec 1.15 rule data](https://github.com/apache/commons-codec/tree/rel/commons-codec-1.15/src/main/resources/org/apache/commons/codec/language)
77
+ with your application; the gem never downloads rules automatically.
78
+
79
+ ```ruby
80
+ dm = RPhonetic::DaitchMokotoffSoundex.new(rules: File.read("config/dmrules.txt"))
81
+ dm.codes("Rosochowaciec")
82
+ # => ["944744", "944745", "944754", "944755", "945744", "945745", "945754", "945755"]
83
+
84
+ bm = RPhonetic::BeiderMorse.new(rules_path: "config/bm")
85
+ bm.encode("Smith") # guess languages
86
+ bm.encode("Smith", languages: ["english"])
87
+ bm.encode("Smith", languages: :any)
88
+ ```
89
+
90
+ Beider–Morse needs the complete configuration directory, including files
91
+ referenced by other rules. `name_type` accepts `:generic`, `:ashkenazi`, or
92
+ `:sephardic`; `rule_type` accepts `:approx` or `:exact`. Language names must be
93
+ supported by the supplied rules. Both encoders parse their rules at construction
94
+ and own the parsed data. Files can be removed afterward without affecting an
95
+ existing encoder. Missing/malformed rules normally raise `RPhonetic::RuleError`.
96
+ An upstream panic is caught and surfaced as `RuntimeError`.
97
+
98
+ ## Text and threading
99
+
100
+ Inputs must be valid Ruby Strings and are transcoded to UTF-8. No automatic
101
+ transliteration is performed. Soundex and RefinedSoundex require ASCII input
102
+ because their upstream implementations use A–Z mapping tables. Other algorithms
103
+ retain upstream handling of non-ASCII text; they are not universal multilingual
104
+ phonetic encoders. Encoding currently holds Ruby's GVL; `encode_many` reduces
105
+ boundary crossings but does not enable parallel Ruby threads.
106
+
107
+ ## Development
108
+
109
+ All public classes and methods have YARD documentation. RBS signatures live in
110
+ `sig/` and ship with both source and platform gems. Type checkers using the gem's
111
+ signatures can load them with `library "rphonetic"` (for example in a Steepfile).
112
+ Signatures describe constructor keywords, inherited class conveniences, and
113
+ Double Metaphone's `[String, String]` result. Constraints such as positive lengths
114
+ and nonempty language arrays are checked at runtime.
115
+
116
+ Generate HTML API documentation with `bundle exec rake docs` (open
117
+ `doc/index.html`) and validate signatures with `bundle exec rake rbs`.
118
+
119
+ Install Rust, Ruby 3.4 or 4.0, a C compiler and libclang, then:
120
+
121
+ ```sh
122
+ bundle install
123
+ bundle exec rake spec
124
+ cargo fmt --check
125
+ cargo clippy --locked -- -D warnings
126
+ bundle exec rake build
127
+ ```
128
+
129
+ `Cargo.lock` is included in source releases for reproducible dependency versions.
130
+ The RSpec suite lives in `spec/`. `bundle exec rake spec` compiles the extension
131
+ before running the suite; `bundle exec rspec` runs it using an existing build.
132
+ `bundle exec rake test` remains an alias for `spec`. CI runs the same specs
133
+ against installed platform gems from outside the checkout, so RSpec's local
134
+ load path cannot accidentally select the source copy.
135
+ The Apache rule fixtures are used only by specs and excluded from all gems.
136
+
137
+ The [CI workflow](https://github.com/tzaid/rphonetic-ruby/actions/workflows/ci.yml)
138
+ compiles and tests each OS/CPU/Ruby combination,
139
+ combines the two Ruby binaries into each platform gem, then installs and
140
+ loads the packaged gem on each target. The package job uses Ruby 3.4 only to
141
+ assemble the archive; it includes the binaries compiled separately for both
142
+ Ruby versions. It uploads gem artifacts; it does not
143
+ publish to RubyGems. To assemble a platform package manually, place genuine
144
+ target binaries in `lib/rphonetic/{3.4,4.0}/rphonetic_ext.<DLEXT>` and run:
145
+
146
+ ```sh
147
+ ruby script/package.rb --platform arm64-darwin
148
+ ```
149
+
150
+ Never label a binary for an OS, CPU, or Ruby version it was not compiled for.
151
+ For local package verification with a single Ruby installation, run
152
+ `ruby script/stage_binary.rb`, then add `--ruby-abi 3.4` (or your supported
153
+ Ruby series) to the package command. This restricts the gem's Ruby requirement
154
+ to that series. Release CI packages both ABIs.
155
+
156
+ ## Publishing
157
+
158
+ The `Release` workflow runs when a `v*` tag is pushed. The tag must match
159
+ `RPhonetic::VERSION` (for example, `v0.1.0`). It calls the full CI workflow,
160
+ then verifies and publishes the exact six tested packages using RubyGems
161
+ Trusted Publishing. Branch pushes and pull requests only run CI.
162
+
163
+ For the first release, configure a pending trusted publisher on RubyGems with
164
+ gem name `rphonetic`, repository owner `tzaid`, repository name `rphonetic-ruby`,
165
+ workflow filename `release.yml`, and environment `release`. The optional
166
+ Workflow Repository fields stay blank. No RubyGems API key is needed.
167
+
168
+ Once the publisher is configured and release changes are committed and pushed:
169
+
170
+ ```sh
171
+ git tag v0.1.0
172
+ git push origin v0.1.0
173
+ ```
174
+
175
+ Published package versions cannot be overwritten. If publishing stops after
176
+ only some packages are uploaded, inspect the RubyGems release before retrying.
177
+
178
+ ## Versioning
179
+
180
+ The gem uses independent, three-part semantic versions, starting at `0.1.0`.
181
+ Before `1.0.0`, minor releases may introduce breaking API changes; patch releases
182
+ preserve compatibility. Use `~> 0.1.0` to stay on the `0.1` release series.
183
+ Upstream crate updates are assessed by their effect on Ruby consumers rather
184
+ than by copying the crate's version number. Gem `0.1.0` wraps rphonetic `4.0.0`.
185
+
186
+ ## License
187
+
188
+ Apache-2.0. See LICENSE and NOTICE. Upstream test fixture attribution is in
189
+ `spec/fixtures/NOTICE`.
@@ -0,0 +1,13 @@
1
+ [package]
2
+ name = "rphonetic_ext"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ publish = false
6
+
7
+ [lib]
8
+ name = "rphonetic_ext"
9
+ crate-type = ["cdylib"]
10
+
11
+ [dependencies]
12
+ magnus = "0.8"
13
+ rphonetic = { version = "=4.0.0", default-features = false }
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "rb_sys/mkmf"
5
+
6
+ create_rust_makefile("rphonetic/rphonetic_ext") do |config|
7
+ config.extra_cargo_args = ["--locked"]
8
+ end
@@ -0,0 +1,275 @@
1
+ use std::panic::{catch_unwind, AssertUnwindSafe};
2
+ use std::path::PathBuf;
3
+
4
+ use magnus::{function, method, prelude::*, Error, Ruby};
5
+ use rphonetic::{
6
+ BeiderMorseBuilder, Caverphone1, Caverphone2, Cologne, ConfigFiles, DaitchMokotoffSoundex,
7
+ DaitchMokotoffSoundexBuilder, DoubleMetaphone, Encoder, LanguageSet, MatchRatingApproach,
8
+ Metaphone, NameType, Nysiis, Phonex, RefinedSoundex, RuleType, Soundex, SoundexCommons,
9
+ };
10
+
11
+ struct BeiderConfig {
12
+ // Own the parsed rules. Build a cheap borrowing engine for each call,
13
+ // avoiding self-references, leaked rules, and Ruby GC lifetime hazards.
14
+ files: ConfigFiles,
15
+ name_type: NameType,
16
+ rule_type: RuleType,
17
+ concat: bool,
18
+ max_phonemes: usize,
19
+ }
20
+
21
+ impl BeiderConfig {
22
+ fn builder(&self) -> BeiderMorseBuilder<'_> {
23
+ BeiderMorseBuilder::new(&self.files)
24
+ .name_type(self.name_type)
25
+ .rule_type(self.rule_type)
26
+ .concat(self.concat)
27
+ .max_phonemes(self.max_phonemes)
28
+ }
29
+ }
30
+
31
+ enum Algorithm {
32
+ Simple(Box<dyn Encoder + Send>),
33
+ Double(DoubleMetaphone),
34
+ Soundex(Soundex),
35
+ Refined(RefinedSoundex),
36
+ Daitch(DaitchMokotoffSoundex),
37
+ Beider(Box<BeiderConfig>),
38
+ }
39
+
40
+ #[magnus::wrap(class = "RPhonetic::NativeEncoder", free_immediately, size)]
41
+ struct NativeEncoder(Algorithm);
42
+
43
+ fn argument_error(message: impl Into<String>) -> Error {
44
+ Error::new(Ruby::get().unwrap().exception_arg_error(), message.into())
45
+ }
46
+
47
+ fn rule_error(error: rphonetic::PhoneticError) -> Error {
48
+ let ruby = Ruby::get().unwrap();
49
+ let module = ruby
50
+ .class_object()
51
+ .const_get::<_, magnus::RModule>("RPhonetic")
52
+ .unwrap();
53
+ let class = module
54
+ .const_get::<_, magnus::ExceptionClass>("RuleError")
55
+ .unwrap();
56
+ Error::new(class, error.to_string())
57
+ }
58
+
59
+ // Malformed external rules or unsupported text must never unwind through Ruby.
60
+ fn checked<T>(f: impl FnOnce() -> Result<T, Error>) -> Result<T, Error> {
61
+ catch_unwind(AssertUnwindSafe(f)).unwrap_or_else(|_| {
62
+ Err(Error::new(
63
+ Ruby::get().unwrap().exception_runtime_error(),
64
+ "rphonetic could not process this input or rule configuration",
65
+ ))
66
+ })
67
+ }
68
+
69
+ fn mapping(value: String) -> Result<[char; 26], Error> {
70
+ value
71
+ .chars()
72
+ .collect::<Vec<_>>()
73
+ .try_into()
74
+ .map_err(|_| argument_error("mapping must contain exactly 26 characters (A through Z)"))
75
+ }
76
+
77
+ impl NativeEncoder {
78
+ fn simple(name: String) -> Result<Self, Error> {
79
+ let encoder: Box<dyn Encoder + Send> = match name.as_str() {
80
+ "Caverphone1" => Box::new(Caverphone1),
81
+ "Caverphone2" => Box::new(Caverphone2),
82
+ "Cologne" => Box::new(Cologne),
83
+ "MatchRatingApproach" => Box::new(MatchRatingApproach),
84
+ _ => return Err(argument_error("unknown algorithm")),
85
+ };
86
+ Ok(Self(Algorithm::Simple(encoder)))
87
+ }
88
+
89
+ fn metaphone(length: Option<usize>) -> Self {
90
+ Self(Algorithm::Simple(Box::new(Metaphone::new(length))))
91
+ }
92
+
93
+ fn double_metaphone(length: Option<usize>) -> Self {
94
+ Self(Algorithm::Double(DoubleMetaphone::new(length)))
95
+ }
96
+
97
+ fn nysiis(strict: bool) -> Self {
98
+ Self(Algorithm::Simple(Box::new(Nysiis::new(strict))))
99
+ }
100
+
101
+ fn phonex(length: usize) -> Self {
102
+ Self(Algorithm::Simple(Box::new(Phonex::new(length))))
103
+ }
104
+
105
+ fn soundex(value: String, special_case_h_w: bool) -> Result<Self, Error> {
106
+ Ok(Self(Algorithm::Soundex(Soundex::new(
107
+ mapping(value)?,
108
+ special_case_h_w,
109
+ ))))
110
+ }
111
+
112
+ fn refined_soundex(value: String) -> Result<Self, Error> {
113
+ Ok(Self(Algorithm::Refined(RefinedSoundex::new(mapping(
114
+ value,
115
+ )?))))
116
+ }
117
+
118
+ fn daitch_mokotoff(rules: String, folding: bool) -> Result<Self, Error> {
119
+ checked(|| {
120
+ DaitchMokotoffSoundexBuilder::with_rules(&rules)
121
+ .ascii_folding(folding)
122
+ .build()
123
+ .map(|encoder| Self(Algorithm::Daitch(encoder)))
124
+ .map_err(rule_error)
125
+ })
126
+ }
127
+
128
+ fn beider_morse(
129
+ path: String,
130
+ name: String,
131
+ rule: String,
132
+ concat: bool,
133
+ max: usize,
134
+ ) -> Result<Self, Error> {
135
+ let name_type = match name.as_str() {
136
+ "generic" => NameType::Generic,
137
+ "ashkenazi" => NameType::Ashkenazi,
138
+ "sephardic" => NameType::Sephardic,
139
+ _ => {
140
+ return Err(argument_error(
141
+ "name_type must be :generic, :ashkenazi, or :sephardic",
142
+ ))
143
+ }
144
+ };
145
+ let rule_type = match rule.as_str() {
146
+ "approx" => RuleType::Approx,
147
+ "exact" => RuleType::Exact,
148
+ _ => return Err(argument_error("rule_type must be :approx or :exact")),
149
+ };
150
+ checked(|| {
151
+ let files = ConfigFiles::new(&PathBuf::from(path)).map_err(rule_error)?;
152
+ Ok(Self(Algorithm::Beider(Box::new(BeiderConfig {
153
+ files,
154
+ name_type,
155
+ rule_type,
156
+ concat,
157
+ max_phonemes: max,
158
+ }))))
159
+ })
160
+ }
161
+
162
+ fn validate(&self, value: &str) -> Result<(), Error> {
163
+ // These upstream implementations index fixed A-Z mapping arrays.
164
+ if matches!(self.0, Algorithm::Soundex(_) | Algorithm::Refined(_)) && !value.is_ascii() {
165
+ return Err(argument_error(
166
+ "Soundex and RefinedSoundex require ASCII input; transliterate the name first",
167
+ ));
168
+ }
169
+ Ok(())
170
+ }
171
+
172
+ fn with_encoder<T>(&self, f: impl FnOnce(&dyn Encoder) -> T) -> T {
173
+ match &self.0 {
174
+ Algorithm::Simple(encoder) => f(encoder.as_ref()),
175
+ Algorithm::Double(encoder) => f(encoder),
176
+ Algorithm::Soundex(encoder) => f(encoder),
177
+ Algorithm::Refined(encoder) => f(encoder),
178
+ Algorithm::Daitch(encoder) => f(encoder),
179
+ Algorithm::Beider(config) => f(&config.builder().build()),
180
+ }
181
+ }
182
+
183
+ fn encode(&self, value: String) -> Result<String, Error> {
184
+ self.validate(&value)?;
185
+ checked(|| Ok(self.with_encoder(|encoder| encoder.encode(&value))))
186
+ }
187
+
188
+ fn encode_many(&self, values: Vec<String>) -> Result<Vec<String>, Error> {
189
+ for value in &values {
190
+ self.validate(value)?;
191
+ }
192
+ checked(|| {
193
+ Ok(self
194
+ .with_encoder(|encoder| values.iter().map(|value| encoder.encode(value)).collect()))
195
+ })
196
+ }
197
+
198
+ fn matches(&self, first: String, second: String) -> Result<bool, Error> {
199
+ self.validate(&first)?;
200
+ self.validate(&second)?;
201
+ checked(|| Ok(self.with_encoder(|encoder| encoder.is_encoded_equals(&first, &second))))
202
+ }
203
+
204
+ fn codes(&self, value: String) -> Result<Vec<String>, Error> {
205
+ checked(|| match &self.0 {
206
+ Algorithm::Double(encoder) => {
207
+ let result = encoder.double_metaphone(&value);
208
+ Ok(vec![result.primary(), result.alternate()])
209
+ }
210
+ Algorithm::Daitch(encoder) => Ok(encoder.inner_soundex(&value, true)),
211
+ _ => Err(argument_error(
212
+ "this encoder does not expose multiple codes",
213
+ )),
214
+ })
215
+ }
216
+
217
+ fn difference(&self, first: String, second: String) -> Result<usize, Error> {
218
+ self.validate(&first)?;
219
+ self.validate(&second)?;
220
+ checked(|| match &self.0 {
221
+ Algorithm::Soundex(encoder) => Ok(encoder.difference(&first, &second)),
222
+ Algorithm::Refined(encoder) => Ok(encoder.difference(&first, &second)),
223
+ _ => Err(argument_error("difference requires a Soundex encoder")),
224
+ })
225
+ }
226
+
227
+ fn encode_languages(&self, value: String, languages: Vec<String>) -> Result<String, Error> {
228
+ let selected = if languages == ["any"] {
229
+ LanguageSet::Any
230
+ } else {
231
+ LanguageSet::SomeLanguages(languages.into_iter().collect())
232
+ };
233
+ checked(|| match &self.0 {
234
+ Algorithm::Beider(config) => Ok(config
235
+ .builder()
236
+ .build()
237
+ .encode_with_languages(&value, &selected)),
238
+ _ => Err(argument_error("languages requires BeiderMorse")),
239
+ })
240
+ }
241
+ }
242
+
243
+ #[magnus::init]
244
+ fn init(ruby: &Ruby) -> Result<(), Error> {
245
+ let module = ruby.define_module("RPhonetic")?;
246
+ let class = module.define_class("NativeEncoder", ruby.class_object())?;
247
+ class.define_singleton_method("simple", function!(NativeEncoder::simple, 1))?;
248
+ class.define_singleton_method("metaphone", function!(NativeEncoder::metaphone, 1))?;
249
+ class.define_singleton_method(
250
+ "double_metaphone",
251
+ function!(NativeEncoder::double_metaphone, 1),
252
+ )?;
253
+ class.define_singleton_method("nysiis", function!(NativeEncoder::nysiis, 1))?;
254
+ class.define_singleton_method("phonex", function!(NativeEncoder::phonex, 1))?;
255
+ class.define_singleton_method("soundex", function!(NativeEncoder::soundex, 2))?;
256
+ class.define_singleton_method(
257
+ "refined_soundex",
258
+ function!(NativeEncoder::refined_soundex, 1),
259
+ )?;
260
+ class.define_singleton_method(
261
+ "daitch_mokotoff",
262
+ function!(NativeEncoder::daitch_mokotoff, 2),
263
+ )?;
264
+ class.define_singleton_method("beider_morse", function!(NativeEncoder::beider_morse, 5))?;
265
+ class.define_method("encode", method!(NativeEncoder::encode, 1))?;
266
+ class.define_method("encode_many", method!(NativeEncoder::encode_many, 1))?;
267
+ class.define_method("matches", method!(NativeEncoder::matches, 2))?;
268
+ class.define_method("codes", method!(NativeEncoder::codes, 1))?;
269
+ class.define_method("difference", method!(NativeEncoder::difference, 2))?;
270
+ class.define_method(
271
+ "encode_languages",
272
+ method!(NativeEncoder::encode_languages, 2),
273
+ )?;
274
+ Ok(())
275
+ }
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPhonetic
4
+ # Beider–Morse phonetic matching with caller-supplied configuration files.
5
+ # Results are expression strings preserving alternatives, parentheses, and
6
+ # word groupings. {#match?} compares complete expression strings.
7
+ #
8
+ # The full rules directory is parsed at construction and owned by the
9
+ # instance; later changes to those files do not affect it. No rules are
10
+ # bundled. Inherited {.encode}, {.encode_many}, and {#encode_many} guess
11
+ # languages for each name; use {#encode} to select languages explicitly.
12
+ #
13
+ # @example
14
+ # encoder = RPhonetic::BeiderMorse.new(rules_path: "config/bm")
15
+ # encoder.encode("Smith", languages: ["english"])
16
+ class BeiderMorse < Encoder
17
+ # Load a complete Apache Commons Codec Beider–Morse rules directory.
18
+ # Referenced rule files must also be present.
19
+ #
20
+ # @param rules_path [String] path to the configuration directory
21
+ # @param name_type [Symbol, String] generic, ashkenazi, or sephardic
22
+ # @param rule_type [Symbol, String] approx or exact
23
+ # @param concat [Boolean] whether to encode words together instead of separately
24
+ # @param max_phonemes [Integer] positive limit on generated phonemes
25
+ # @raise [RuleError] if files cannot be read or rule parsing fails
26
+ # @raise [TypeError] if rules_path is not a String
27
+ # @raise [ArgumentError] if an option has an invalid value
28
+ # @raise [RuntimeError] if the upstream parser panics
29
+ def initialize(rules_path:, name_type: :generic, rule_type: :approx, concat: true, max_phonemes: 20)
30
+ @native = NativeEncoder.beider_morse(
31
+ text(rules_path), name_type.to_s, rule_type.to_s,
32
+ boolean(concat, :concat), length_option(max_phonemes, :max_phonemes)
33
+ )
34
+ end
35
+
36
+ # Encode a name with guessed or explicitly selected languages.
37
+ # Language names must be supported by the supplied configuration.
38
+ # This keyword is available only on the instance method.
39
+ #
40
+ # @param value [String] name to encode
41
+ # @param languages [nil, :any, Array<String>] nil guesses languages; :any uses catch-all rules; a nonempty array selects language names
42
+ # @return [String] phonetic expression including alternatives and word groupings
43
+ # @raise [ArgumentError] if languages is not nil, :any, or a nonempty Array
44
+ # @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as Encoder#encode
45
+ def encode(value, languages: nil)
46
+ return super(value) if languages.nil?
47
+ unless languages == :any || (languages.is_a?(Array) && !languages.empty?)
48
+ raise ArgumentError, "languages must be nil, :any, or a nonempty Array of language names"
49
+ end
50
+ selected = languages == :any ? ["any"] : languages.map { |language| text(language) }
51
+ @native.encode_languages(text(value), selected)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPhonetic
4
+ # The original Caverphone algorithm for English names, developed for
5
+ # New Zealand pronunciation. Codes contain six characters, padded with `1`.
6
+ #
7
+ # @example
8
+ # RPhonetic::Caverphone1.encode("Thompson") # => "TMPSN1"
9
+ class Caverphone1 < Encoder
10
+ # Create a Caverphone1 encoder. This algorithm has no configuration options.
11
+ def initialize
12
+ @native = NativeEncoder.simple("Caverphone1")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPhonetic
4
+ # The revised Caverphone algorithm for English names. Codes contain ten
5
+ # characters, padded with `1`.
6
+ #
7
+ # @example
8
+ # RPhonetic::Caverphone2.encode("Thompson") # => "TMPSN11111"
9
+ class Caverphone2 < Encoder
10
+ # Create a Caverphone2 encoder. This algorithm has no configuration options.
11
+ def initialize
12
+ @native = NativeEncoder.simple("Caverphone2")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPhonetic
4
+ # Cologne phonetics (Kölner Phonetik) for German names, including umlauts.
5
+ # Returns a variable-length numeric code.
6
+ #
7
+ # @example
8
+ # RPhonetic::Cologne.encode("müller") # => "657"
9
+ class Cologne < Encoder
10
+ # Create a Cologne encoder. This algorithm has no configuration options.
11
+ def initialize
12
+ @native = NativeEncoder.simple("Cologne")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPhonetic
4
+ # Daitch–Mokotoff Soundex encodes names using caller-supplied rules.
5
+ # Inherited {#encode} returns one nonbranching code; {#codes} returns all
6
+ # branching six-digit codes. {#match?} compares the nonbranching codes.
7
+ # Rules are parsed once and owned by the instance. No rules are bundled.
8
+ #
9
+ # @example
10
+ # encoder = RPhonetic::DaitchMokotoffSoundex.new(rules: File.read("config/dmrules.txt"))
11
+ # encoder.codes("Rosochowaciec")
12
+ class DaitchMokotoffSoundex < Encoder
13
+ # Parse rule text and create a reusable Daitch–Mokotoff encoder.
14
+ # The rules must use the Apache Commons Codec rule format.
15
+ #
16
+ # @param rules [String] complete rule contents, not a file path
17
+ # @param ascii_folding [Boolean] whether to apply the folding substitutions defined in the rules
18
+ # @raise [RuleError] if rule parsing fails
19
+ # @raise [TypeError] if rules is not a String
20
+ # @raise [ArgumentError] if ascii_folding is not true or false
21
+ # @raise [RuntimeError] if the upstream parser panics
22
+ def initialize(rules:, ascii_folding: true)
23
+ @native = NativeEncoder.daitch_mokotoff(text(rules), boolean(ascii_folding, :ascii_folding))
24
+ end
25
+
26
+ # Return all codes generated by branching rules.
27
+ #
28
+ # @param value [String] name to encode
29
+ # @return [Array<String>] the distinct six-digit codes in upstream order
30
+ # @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
31
+ def codes(value)
32
+ @native.codes(text(value))
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RPhonetic
4
+ # Double Metaphone produces primary and alternate phonetic codes.
5
+ # Inherited {#encode} and {#match?} use only the primary code. Use {#codes}
6
+ # when comparing names across both pronunciations.
7
+ #
8
+ # @example
9
+ # RPhonetic::DoubleMetaphone.codes("Smith") # => ["SM0", "XMT"]
10
+ class DoubleMetaphone < Encoder
11
+ # Create a Double Metaphone encoder.
12
+ #
13
+ # @param max_length [Integer, nil] positive maximum length of each code; nil removes the limit
14
+ # @raise [ArgumentError] if max_length is neither positive nor nil
15
+ def initialize(max_length: 4)
16
+ @native = NativeEncoder.double_metaphone(length_option(max_length, :max_length, unlimited: true))
17
+ end
18
+
19
+ # Construct an encoder and return both codes for one name.
20
+ #
21
+ # @param value [String] name to encode
22
+ # @param options [Hash] constructor keywords
23
+ # @option options [Integer, nil] :max_length (4) positive maximum code length; nil removes the limit
24
+ # @return [Array(String, String)] primary and alternate codes, in that order
25
+ # @see #codes
26
+ # @see #initialize
27
+ def self.codes(value, **options)
28
+ new(**options).codes(value)
29
+ end
30
+
31
+ # Return the primary and alternate codes.
32
+ # The result always contains two strings, even when the codes are identical
33
+ # or empty. Both codes respect the configured max_length.
34
+ #
35
+ # @param value [String] name to encode
36
+ # @return [Array(String, String)] primary and alternate codes, in that order
37
+ # @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
38
+ def codes(value)
39
+ @native.codes(text(value))
40
+ end
41
+
42
+ # Return only the alternate code.
43
+ #
44
+ # @param value [String] name to encode
45
+ # @return [String] alternate code, possibly identical to the primary code or empty
46
+ # @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
47
+ # @see #codes
48
+ def alternate(value)
49
+ codes(value).last
50
+ end
51
+ end
52
+ end