rphonetic 0.1.0-x86_64-linux-gnu
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/LICENSE +202 -0
- data/NOTICE +17 -0
- data/README.md +189 -0
- data/lib/rphonetic/3.4/rphonetic_ext.so +0 -0
- data/lib/rphonetic/4.0/rphonetic_ext.so +0 -0
- data/lib/rphonetic/beider_morse.rb +54 -0
- data/lib/rphonetic/caverphone1.rb +15 -0
- data/lib/rphonetic/caverphone2.rb +15 -0
- data/lib/rphonetic/cologne.rb +15 -0
- data/lib/rphonetic/daitch_mokotoff_soundex.rb +35 -0
- data/lib/rphonetic/double_metaphone.rb +52 -0
- data/lib/rphonetic/encoder.rb +93 -0
- data/lib/rphonetic/encoders.rb +19 -0
- data/lib/rphonetic/match_rating_approach.rb +18 -0
- data/lib/rphonetic/metaphone.rb +17 -0
- data/lib/rphonetic/nysiis.rb +19 -0
- data/lib/rphonetic/phonex.rb +18 -0
- data/lib/rphonetic/refined_soundex.rb +35 -0
- data/lib/rphonetic/soundex.rb +38 -0
- data/lib/rphonetic/version.rb +6 -0
- data/lib/rphonetic.rb +25 -0
- data/sig/rphonetic/beider_morse.rbs +14 -0
- data/sig/rphonetic/caverphone1.rbs +5 -0
- data/sig/rphonetic/caverphone2.rbs +5 -0
- data/sig/rphonetic/cologne.rbs +5 -0
- data/sig/rphonetic/daitch_mokotoff_soundex.rbs +9 -0
- data/sig/rphonetic/double_metaphone.rbs +11 -0
- data/sig/rphonetic/encoder.rbs +11 -0
- data/sig/rphonetic/match_rating_approach.rbs +5 -0
- data/sig/rphonetic/metaphone.rbs +7 -0
- data/sig/rphonetic/nysiis.rbs +7 -0
- data/sig/rphonetic/phonex.rbs +7 -0
- data/sig/rphonetic/refined_soundex.rbs +11 -0
- data/sig/rphonetic/soundex.rbs +12 -0
- data/sig/rphonetic.rbs +7 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 449b6e953aaeceb66556800ba593ea8c7f2d5dc49d6cd07b3561ece6cfb519f2
|
|
4
|
+
data.tar.gz: 4c2410c33c212c88395b90e4c98a378c3efc58e761a4a65bc1a01b1eb1b44f39
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 79083d4036915a24f70d482efa7a744515e73db51bbae0cb8218619dbe88f02cde5397a2804a4236ff70e721a94724c25ecffc7083dde46de014030ffff79dc6
|
|
7
|
+
data.tar.gz: 7514b23c7142251d4400f35178c56ea85d198b2b0c95a5eaec9d388d35465abec58e1c92f209fe35528fb2bacf7e8a743f7c1382a27157fb2af3dd7e0d083e93
|
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/NOTICE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
rphonetic Ruby bindings
|
|
2
|
+
Copyright 2026 Tariq
|
|
3
|
+
|
|
4
|
+
This project uses the rphonetic Rust crate by dalvany and Crabe de France,
|
|
5
|
+
licensed under Apache-2.0: https://github.com/Dalvany/rphonetic
|
|
6
|
+
|
|
7
|
+
The Rust implementation derives from Apache Commons Codec.
|
|
8
|
+
Apache Commons Codec
|
|
9
|
+
Copyright 2002-2020 The Apache Software Foundation
|
|
10
|
+
This product includes software developed at The Apache Software Foundation
|
|
11
|
+
(https://www.apache.org/).
|
|
12
|
+
The Beider-Morse implementation derives from work by
|
|
13
|
+
Copyright (c) 2008 Alexander Beider & Stephen P. Morse.
|
|
14
|
+
|
|
15
|
+
Test fixtures under spec/fixtures are derived from Apache Commons Codec 1.15
|
|
16
|
+
via rphonetic 3.0.6. They are licensed under Apache-2.0 and are not included
|
|
17
|
+
in the distributed gems. See spec/fixtures/NOTICE for upstream attribution.
|
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`.
|
|
Binary file
|
|
Binary file
|
|
@@ -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
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# Shared interface for reusable native phonetic encoders.
|
|
5
|
+
# Each instance owns its native encoder. Class convenience methods accept
|
|
6
|
+
# the same keywords as the concrete encoder's constructor.
|
|
7
|
+
#
|
|
8
|
+
# @abstract Instantiate a concrete algorithm, such as DoubleMetaphone.
|
|
9
|
+
class Encoder
|
|
10
|
+
class << self
|
|
11
|
+
# Construct an encoder and encode one name.
|
|
12
|
+
# Reuse an instance when encoding repeatedly, especially with external rules.
|
|
13
|
+
#
|
|
14
|
+
# @param value [String] name to encode
|
|
15
|
+
# @param options [Hash] keyword arguments accepted by the subclass constructor
|
|
16
|
+
# @return [String] the single or primary code, or Beider–Morse expression
|
|
17
|
+
# @see #encode
|
|
18
|
+
def encode(value, **options)
|
|
19
|
+
new(**options).encode(value)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Construct an encoder and encode several names in input order.
|
|
23
|
+
#
|
|
24
|
+
# @param values [Array<String>] names to encode
|
|
25
|
+
# @param options [Hash] keyword arguments accepted by the subclass constructor
|
|
26
|
+
# @return [Array<String>] one result per input name
|
|
27
|
+
# @see #encode_many
|
|
28
|
+
def encode_many(values, **options)
|
|
29
|
+
new(**options).encode_many(values)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Encode one name using this instance's configuration.
|
|
34
|
+
# Strings are transcoded to UTF-8; no transliteration is performed.
|
|
35
|
+
#
|
|
36
|
+
# @param value [String] name to encode
|
|
37
|
+
# @return [String] the single or primary code, or Beider–Morse expression
|
|
38
|
+
# @raise [TypeError] if value is not a String
|
|
39
|
+
# @raise [ArgumentError] if the string contains invalid bytes, or an ASCII-only encoder receives non-ASCII text
|
|
40
|
+
# @raise [EncodingError] if the string cannot be transcoded to UTF-8
|
|
41
|
+
# @raise [RuntimeError] if the upstream encoder panics
|
|
42
|
+
def encode(value)
|
|
43
|
+
@native.encode(text(value))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Encode an array in one native call, preserving input order.
|
|
47
|
+
# Returns an empty array for empty input. Beider–Morse guesses languages
|
|
48
|
+
# for each name. This call holds Ruby's GVL.
|
|
49
|
+
#
|
|
50
|
+
# @param values [Array<String>] names to encode
|
|
51
|
+
# @return [Array<String>] one result per input name
|
|
52
|
+
# @raise [TypeError] if values is not an Array or contains a non-String
|
|
53
|
+
# @raise [ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
|
|
54
|
+
# @see #encode
|
|
55
|
+
def encode_many(values)
|
|
56
|
+
raise TypeError, "values must be an Array" unless values.is_a?(Array)
|
|
57
|
+
@native.encode_many(values.map { |value| text(value) })
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Compare two names using the upstream algorithm.
|
|
61
|
+
# Match Rating Approach uses its similarity rating. Other encoders compare
|
|
62
|
+
# {#encode} results: Double Metaphone uses primary codes, Daitch–Mokotoff
|
|
63
|
+
# uses nonbranching codes, and Beider–Morse compares entire expressions.
|
|
64
|
+
# This does not check for overlap between alternative codes.
|
|
65
|
+
#
|
|
66
|
+
# @param first [String] first name
|
|
67
|
+
# @param second [String] second name
|
|
68
|
+
# @return [Boolean] whether the algorithm considers the names equivalent
|
|
69
|
+
# @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
|
|
70
|
+
def match?(first, second)
|
|
71
|
+
@native.matches(text(first), text(second))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def text(value)
|
|
77
|
+
raise TypeError, "value must be a String" unless value.is_a?(String)
|
|
78
|
+
raise ArgumentError, "value contains invalid bytes" unless value.valid_encoding?
|
|
79
|
+
value.encode(Encoding::UTF_8)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def length_option(value, name, unlimited: false)
|
|
83
|
+
return nil if unlimited && value.nil?
|
|
84
|
+
raise ArgumentError, "#{name} must be a positive Integer#{' or nil' if unlimited}" unless value.is_a?(Integer) && value.positive?
|
|
85
|
+
value
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def boolean(value, name)
|
|
89
|
+
raise ArgumentError, "#{name} must be true or false" unless value == true || value == false
|
|
90
|
+
value
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "encoder"
|
|
4
|
+
require_relative "caverphone1"
|
|
5
|
+
require_relative "caverphone2"
|
|
6
|
+
require_relative "cologne"
|
|
7
|
+
require_relative "match_rating_approach"
|
|
8
|
+
require_relative "metaphone"
|
|
9
|
+
require_relative "double_metaphone"
|
|
10
|
+
require_relative "nysiis"
|
|
11
|
+
require_relative "phonex"
|
|
12
|
+
require_relative "soundex"
|
|
13
|
+
require_relative "refined_soundex"
|
|
14
|
+
require_relative "daitch_mokotoff_soundex"
|
|
15
|
+
require_relative "beider_morse"
|
|
16
|
+
|
|
17
|
+
module RPhonetic
|
|
18
|
+
private_constant :NativeEncoder
|
|
19
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# Match Rating Approach encodes names in at most six characters.
|
|
5
|
+
# Unlike the other encoders, {#match?} applies the algorithm's similarity
|
|
6
|
+
# rating rather than requiring identical codes.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# encoder = RPhonetic::MatchRatingApproach.new
|
|
10
|
+
# encoder.encode("Smith") # => "SMTH"
|
|
11
|
+
# encoder.match?("Smith", "Smyth") # => true
|
|
12
|
+
class MatchRatingApproach < Encoder
|
|
13
|
+
# Create a MatchRatingApproach encoder. This algorithm has no configuration options.
|
|
14
|
+
def initialize
|
|
15
|
+
@native = NativeEncoder.simple("MatchRatingApproach")
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# Metaphone encodes English pronunciation as a single phonetic code.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# RPhonetic::Metaphone.encode("Joanne") # => "JN"
|
|
8
|
+
class Metaphone < Encoder
|
|
9
|
+
# Create a Metaphone encoder.
|
|
10
|
+
#
|
|
11
|
+
# @param max_length [Integer, nil] positive maximum code length; nil removes the limit
|
|
12
|
+
# @raise [ArgumentError] if max_length is neither positive nor nil
|
|
13
|
+
def initialize(max_length: 4)
|
|
14
|
+
@native = NativeEncoder.metaphone(length_option(max_length, :max_length, unlimited: true))
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# New York State Identification and Intelligence System phonetic encoding.
|
|
5
|
+
# Strict mode limits the result to six characters.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# RPhonetic::Nysiis.encode("WESTERLUND") # => "WASTAR"
|
|
9
|
+
# RPhonetic::Nysiis.encode("WESTERLUND", strict: false) # => "WASTARLAD"
|
|
10
|
+
class Nysiis < Encoder
|
|
11
|
+
# Create a NYSIIS encoder.
|
|
12
|
+
#
|
|
13
|
+
# @param strict [Boolean] whether to truncate the code to six characters
|
|
14
|
+
# @raise [ArgumentError] if strict is not true or false
|
|
15
|
+
def initialize(strict: true)
|
|
16
|
+
@native = NativeEncoder.nysiis(boolean(strict, :strict))
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# Phonex combines Soundex-style codes with pronunciation normalization.
|
|
5
|
+
# Codes are padded with zeroes to the configured length.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# RPhonetic::Phonex.encode("William") # => "W450"
|
|
9
|
+
class Phonex < Encoder
|
|
10
|
+
# Create a Phonex encoder.
|
|
11
|
+
#
|
|
12
|
+
# @param max_length [Integer] positive code length; nil is not supported
|
|
13
|
+
# @raise [ArgumentError] if max_length is not a positive Integer
|
|
14
|
+
def initialize(max_length: 4)
|
|
15
|
+
@native = NativeEncoder.phonex(length_option(max_length, :max_length))
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# Refined Soundex uses more consonant groups than Soundex and returns
|
|
5
|
+
# variable-length codes. Input names must be ASCII; non-ASCII names raise
|
|
6
|
+
# ArgumentError.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# RPhonetic::RefinedSoundex.encode("jumped") # => "J408106"
|
|
10
|
+
class RefinedSoundex < Encoder
|
|
11
|
+
# Default code mapping for the letters A–Z.
|
|
12
|
+
# @return [String]
|
|
13
|
+
DEFAULT_MAPPING = "01360240043788015936020505"
|
|
14
|
+
|
|
15
|
+
# Create a Refined Soundex encoder.
|
|
16
|
+
#
|
|
17
|
+
# @param mapping [String] exactly 26 characters, one per letter A–Z
|
|
18
|
+
# @raise [TypeError] if mapping is not a String
|
|
19
|
+
# @raise [ArgumentError] if the mapping is invalid
|
|
20
|
+
def initialize(mapping: DEFAULT_MAPPING)
|
|
21
|
+
@native = NativeEncoder.refined_soundex(text(mapping))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Count positions at which the two encoded names have the same character.
|
|
25
|
+
# This is a similarity count, not an edit distance.
|
|
26
|
+
#
|
|
27
|
+
# @param first [String] first ASCII name
|
|
28
|
+
# @param second [String] second ASCII name
|
|
29
|
+
# @return [Integer] number of equal positions, from zero through the shorter code's length
|
|
30
|
+
# @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
|
|
31
|
+
def difference(first, second)
|
|
32
|
+
@native.difference(text(first), text(second))
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RPhonetic
|
|
4
|
+
# Soundex encodes a name as its initial letter followed by three digits.
|
|
5
|
+
# Input names must be ASCII; non-ASCII names raise ArgumentError.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# RPhonetic::Soundex.encode("Robert") # => "R163"
|
|
9
|
+
class Soundex < Encoder
|
|
10
|
+
# Default code mapping for the letters A–Z.
|
|
11
|
+
# @return [String]
|
|
12
|
+
DEFAULT_MAPPING = "01230120022455012623010202"
|
|
13
|
+
# Genealogical mapping; hyphens mark letters that are ignored.
|
|
14
|
+
# @return [String]
|
|
15
|
+
GENEALOGY_MAPPING = "-123-12--22455-12623-1-2-2"
|
|
16
|
+
|
|
17
|
+
# Create a Soundex encoder with a standard or custom A–Z mapping.
|
|
18
|
+
#
|
|
19
|
+
# @param mapping [String] exactly 26 characters, one per letter A–Z
|
|
20
|
+
# @param special_case_h_w [Boolean] whether H and W leave the preceding code unchanged
|
|
21
|
+
# @raise [TypeError] if mapping is not a String
|
|
22
|
+
# @raise [ArgumentError] if the mapping is invalid or special_case_h_w is not a Boolean
|
|
23
|
+
def initialize(mapping: DEFAULT_MAPPING, special_case_h_w: true)
|
|
24
|
+
@native = NativeEncoder.soundex(text(mapping), boolean(special_case_h_w, :special_case_h_w))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Count positions at which the two encoded names have the same character.
|
|
28
|
+
# This is a similarity count, not an edit distance.
|
|
29
|
+
#
|
|
30
|
+
# @param first [String] first ASCII name
|
|
31
|
+
# @param second [String] second ASCII name
|
|
32
|
+
# @return [Integer] number of equal positions, from zero through the shorter code's length
|
|
33
|
+
# @raise [TypeError, ArgumentError, EncodingError, RuntimeError] for the same input or upstream errors as #encode
|
|
34
|
+
def difference(first, second)
|
|
35
|
+
@native.difference(text(first), text(second))
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/rphonetic.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rbconfig"
|
|
4
|
+
require_relative "rphonetic/version"
|
|
5
|
+
|
|
6
|
+
# Ruby bindings for the rphonetic Rust phonetic algorithms.
|
|
7
|
+
# Encoders are reusable and accept valid Strings, transcoded to UTF-8.
|
|
8
|
+
module RPhonetic
|
|
9
|
+
# Raised when caller-supplied rules cannot be loaded or parsed.
|
|
10
|
+
class RuleError < ArgumentError; end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
series = RUBY_VERSION.split(".").first(2).join(".")
|
|
14
|
+
binary = File.expand_path("rphonetic/#{series}/rphonetic_ext.#{RbConfig::CONFIG.fetch('DLEXT')}", __dir__)
|
|
15
|
+
local_binary = File.expand_path("rphonetic/rphonetic_ext.#{RbConfig::CONFIG.fetch('DLEXT')}", __dir__)
|
|
16
|
+
if File.file?(local_binary)
|
|
17
|
+
# A fresh development build takes precedence over staged release artifacts.
|
|
18
|
+
require local_binary
|
|
19
|
+
elsif File.file?(binary)
|
|
20
|
+
require binary
|
|
21
|
+
else
|
|
22
|
+
require "rphonetic/rphonetic_ext"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
require_relative "rphonetic/encoders"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module RPhonetic
|
|
2
|
+
class BeiderMorse < Encoder
|
|
3
|
+
type name_type = :generic | :ashkenazi | :sephardic | "generic" | "ashkenazi" | "sephardic"
|
|
4
|
+
type rule_type = :approx | :exact | "approx" | "exact"
|
|
5
|
+
type languages = Array[String] | :any | nil
|
|
6
|
+
|
|
7
|
+
def initialize: (rules_path: String, ?name_type: name_type, ?rule_type: rule_type, ?concat: bool, ?max_phonemes: Integer) -> void
|
|
8
|
+
def self.encode: (String value, rules_path: String, ?name_type: name_type, ?rule_type: rule_type, ?concat: bool, ?max_phonemes: Integer) -> String
|
|
9
|
+
def self.encode_many: (Array[String] values, rules_path: String, ?name_type: name_type, ?rule_type: rule_type, ?concat: bool, ?max_phonemes: Integer) -> Array[String]
|
|
10
|
+
|
|
11
|
+
# Only instance encoding accepts explicit languages.
|
|
12
|
+
def encode: (String value, ?languages: languages) -> String
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module RPhonetic
|
|
2
|
+
class DaitchMokotoffSoundex < Encoder
|
|
3
|
+
def initialize: (rules: String, ?ascii_folding: bool) -> void
|
|
4
|
+
def self.encode: (String value, rules: String, ?ascii_folding: bool) -> String
|
|
5
|
+
def self.encode_many: (Array[String] values, rules: String, ?ascii_folding: bool) -> Array[String]
|
|
6
|
+
|
|
7
|
+
def codes: (String value) -> Array[String]
|
|
8
|
+
end
|
|
9
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module RPhonetic
|
|
2
|
+
class DoubleMetaphone < Encoder
|
|
3
|
+
def initialize: (?max_length: Integer?) -> void
|
|
4
|
+
def self.encode: (String value, ?max_length: Integer?) -> String
|
|
5
|
+
def self.encode_many: (Array[String] values, ?max_length: Integer?) -> Array[String]
|
|
6
|
+
|
|
7
|
+
def self.codes: (String value, ?max_length: Integer?) -> [String, String]
|
|
8
|
+
def codes: (String value) -> [String, String]
|
|
9
|
+
def alternate: (String value) -> String
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module RPhonetic
|
|
2
|
+
# Abstract shared interface; instantiate a concrete algorithm.
|
|
3
|
+
class Encoder
|
|
4
|
+
def self.encode: (String value) -> String
|
|
5
|
+
def self.encode_many: (Array[String] values) -> Array[String]
|
|
6
|
+
|
|
7
|
+
def encode: (String value) -> String
|
|
8
|
+
def encode_many: (Array[String] values) -> Array[String]
|
|
9
|
+
def match?: (String first, String second) -> bool
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module RPhonetic
|
|
2
|
+
class RefinedSoundex < Encoder
|
|
3
|
+
DEFAULT_MAPPING: String
|
|
4
|
+
|
|
5
|
+
def initialize: (?mapping: String) -> void
|
|
6
|
+
def self.encode: (String value, ?mapping: String) -> String
|
|
7
|
+
def self.encode_many: (Array[String] values, ?mapping: String) -> Array[String]
|
|
8
|
+
|
|
9
|
+
def difference: (String first, String second) -> Integer
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module RPhonetic
|
|
2
|
+
class Soundex < Encoder
|
|
3
|
+
DEFAULT_MAPPING: String
|
|
4
|
+
GENEALOGY_MAPPING: String
|
|
5
|
+
|
|
6
|
+
def initialize: (?mapping: String, ?special_case_h_w: bool) -> void
|
|
7
|
+
def self.encode: (String value, ?mapping: String, ?special_case_h_w: bool) -> String
|
|
8
|
+
def self.encode_many: (Array[String] values, ?mapping: String, ?special_case_h_w: bool) -> Array[String]
|
|
9
|
+
|
|
10
|
+
def difference: (String first, String second) -> Integer
|
|
11
|
+
end
|
|
12
|
+
end
|
data/sig/rphonetic.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rphonetic
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: x86_64-linux-gnu
|
|
6
|
+
authors:
|
|
7
|
+
- tzaid
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Reusable encoders for all twelve rphonetic algorithms, with caller-supplied
|
|
13
|
+
rules and precompiled native gem support.
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- LICENSE
|
|
19
|
+
- NOTICE
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/rphonetic.rb
|
|
22
|
+
- lib/rphonetic/3.4/rphonetic_ext.so
|
|
23
|
+
- lib/rphonetic/4.0/rphonetic_ext.so
|
|
24
|
+
- lib/rphonetic/beider_morse.rb
|
|
25
|
+
- lib/rphonetic/caverphone1.rb
|
|
26
|
+
- lib/rphonetic/caverphone2.rb
|
|
27
|
+
- lib/rphonetic/cologne.rb
|
|
28
|
+
- lib/rphonetic/daitch_mokotoff_soundex.rb
|
|
29
|
+
- lib/rphonetic/double_metaphone.rb
|
|
30
|
+
- lib/rphonetic/encoder.rb
|
|
31
|
+
- lib/rphonetic/encoders.rb
|
|
32
|
+
- lib/rphonetic/match_rating_approach.rb
|
|
33
|
+
- lib/rphonetic/metaphone.rb
|
|
34
|
+
- lib/rphonetic/nysiis.rb
|
|
35
|
+
- lib/rphonetic/phonex.rb
|
|
36
|
+
- lib/rphonetic/refined_soundex.rb
|
|
37
|
+
- lib/rphonetic/soundex.rb
|
|
38
|
+
- lib/rphonetic/version.rb
|
|
39
|
+
- sig/rphonetic.rbs
|
|
40
|
+
- sig/rphonetic/beider_morse.rbs
|
|
41
|
+
- sig/rphonetic/caverphone1.rbs
|
|
42
|
+
- sig/rphonetic/caverphone2.rbs
|
|
43
|
+
- sig/rphonetic/cologne.rbs
|
|
44
|
+
- sig/rphonetic/daitch_mokotoff_soundex.rbs
|
|
45
|
+
- sig/rphonetic/double_metaphone.rbs
|
|
46
|
+
- sig/rphonetic/encoder.rbs
|
|
47
|
+
- sig/rphonetic/match_rating_approach.rbs
|
|
48
|
+
- sig/rphonetic/metaphone.rbs
|
|
49
|
+
- sig/rphonetic/nysiis.rbs
|
|
50
|
+
- sig/rphonetic/phonex.rbs
|
|
51
|
+
- sig/rphonetic/refined_soundex.rbs
|
|
52
|
+
- sig/rphonetic/soundex.rbs
|
|
53
|
+
homepage: https://github.com/tzaid/rphonetic-ruby
|
|
54
|
+
licenses:
|
|
55
|
+
- Apache-2.0
|
|
56
|
+
metadata:
|
|
57
|
+
source_code_uri: https://github.com/tzaid/rphonetic-ruby
|
|
58
|
+
bug_tracker_uri: https://github.com/tzaid/rphonetic-ruby/issues
|
|
59
|
+
rdoc_options: []
|
|
60
|
+
require_paths:
|
|
61
|
+
- lib
|
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '3.4'
|
|
67
|
+
- - "<"
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '4.1'
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubygems_version: 3.6.9
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: Ruby bindings for the rphonetic Rust phonetic algorithms
|
|
79
|
+
test_files: []
|