Customer search: bundled nickname data and sound-alike matching

- Nicknames now come from the carltonnorthern/nicknames data set
  (Apache 2.0, ~2,800 pairs, bundled in data/nicknames with its license),
  plus a few extra groups in EXTRA_NICKNAME_GROUPS. Names match if
  directly linked, or if both are short for the same longer name, so
  Bill/William and Bill/Will match but William/Robert don't (the data
  lists "bill" for both)
- Double Metaphone (text gem) matches names that sound alike but are
  spelled differently: Jon/John, Smyth/Smith, Cathy/Kathy,
  Steven/Stephen, Jeffrey/Geoffrey
- Faster ranking: per-search word-pair scores are cached, whole-name
  comparison only runs when it can succeed, and prepared search data is
  kept in process memory until a customer changes (~40 ms per search at
  5,000 customers)

Adds gem 'text', '~> 1.3' to the Gemfile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-28 22:22:00 -04:00
1 parent 0523e8dd24
commit c4ab97b75e
5 files changed
+3172 -93

No files matched your search

+3
View File
@@ -6,3 +6,6 @@ gem 'roxml'
gem 'will_paginate'
gem 'rexml'
gem 'combine_pdf'
# Double Metaphone sound-alike matching for customer search (CustomerSearchService)
gem 'text', '~> 1.3'
+131 -93
View File
@@ -8,84 +8,37 @@
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'csv'
require 'did_you_mean'
require 'text'
# Finds customers by name and/or phone, tolerant of the ways duplicates sneak in:
# - nicknames: "Bill Smith" finds "William Smith", "Bob" finds "Robert"
# - typos: "Willaim Smtih" finds "William Smith"
# - word order: "Smith William" or "Smith, William" finds "William Smith"
# - partial: "will smi" finds "William Smith" while typing
# - phone: any 4+ digits, in any format, matched against both phone numbers
# - nicknames: "Bill Smith" finds "William Smith", "Bob" finds "Robert" (about 2,800 pairs, see data/nicknames)
# - sound-alikes: "Jon" finds "John", "Smyth" finds "Smith", "Cathy" finds "Kathy" (Double Metaphone)
# - typos: "Willaim Smtih" finds "William Smith"
# - word order: "Smith, William" finds "William Smith"
# - partial: "will smi" finds "William Smith" while typing
# - phone: any 4+ digits, in any format, matched against both phone numbers
#
# Results are ranked best match first. Only id, name and phone numbers are loaded (and cached until
# a customer changes), so searching stays fast as the customer list grows.
# Results are ranked best match first. Only id, name and phone numbers are loaded, and the prepared
# search data is cached until a customer changes, so searching stays fast as the customer list grows.
class CustomerSearchService
DEFAULT_LIMIT = 20
# Lowest score (0..1) a customer needs to be returned
MIN_SCORE = 0.72
# Groups of interchangeable first names. A name may appear in more than one group (e.g. "chris").
NICKNAME_GROUPS = [
%w[william bill billy will willy willie liam],
%w[robert bob bobby rob robbie bert],
%w[richard rick ricky rich richie dick],
%w[james jim jimmy jamie],
%w[john jack johnny jon],
%w[jonathan jon jonny],
%w[michael mike mikey mick],
%w[thomas tom tommy],
%w[joseph joe joey],
%w[charles charlie chuck chas],
%w[edward ed eddie ted ned],
%w[anthony tony],
%w[christopher chris kit],
%w[christine christina chris tina],
%w[daniel dan danny],
%w[david dave davey],
%w[matthew matt],
%w[andrew andy drew],
%w[steven stephen steve stevie],
%w[benjamin ben benny],
%w[samuel sam sammy],
%w[samantha sam],
%w[alexander alex al xander],
%w[alexandra alex lexi],
%w[albert al bert],
%w[nicholas nick nicky],
%w[patrick pat],
%w[patricia pat patty trish],
%w[peter pete],
%w[timothy tim timmy],
%w[kenneth ken kenny],
%w[ronald ron ronnie],
%w[donald don donnie],
%w[gregory greg],
%w[jeffrey jeff],
%w[gerald jerry gerry],
%w[lawrence larry],
%w[raymond ray],
%w[frederick fred freddie],
%w[theodore ted teddy theo],
%w[douglas doug],
%w[joshua josh],
%w[zachary zach zack],
%w[nathaniel nathan nate],
%w[elizabeth liz lizzie beth betty eliza],
%w[margaret maggie meg peggy],
%w[katherine catherine kathryn kate katie kathy cathy kat],
%w[jennifer jen jenny],
%w[rebecca becky becca],
%w[susan sue susie],
%w[deborah debra deb debbie],
%w[victoria vicky tori],
%w[jessica jess jessie]
].freeze
# Bundled nickname data (Apache 2.0, see data/nicknames/README.md)
NICKNAMES_CSV = File.expand_path('../../data/nicknames/names.csv', __dir__)
# name => indexes of the groups it belongs to
NICKNAME_INDEX = NICKNAME_GROUPS.each_with_index.with_object(Hash.new { |h, k| h[k] = [] }) do |(group, index), map|
group.each { |name| map[name] << index }
end.freeze
# Groups missing from the bundled data. Add more here: every name in a group matches every other.
EXTRA_NICKNAME_GROUPS = [
%w[william liam],
%w[alexander xander],
%w[alexandra lexi],
%w[theodore theo],
%w[christopher kit]
].freeze
# Words that don't help tell customers apart
STOP_WORDS = %w[and the llc inc co corp company].freeze
@@ -98,17 +51,32 @@ class CustomerSearchService
Customer.where(id: ids).in_order_of(:id, ids)
end
# [[id, name, phone_number, mobile_phone_number], ...], cached until any customer changes
# Prepared search rows for every customer, cached until any customer changes. Kept in this process's
# memory as well, so each keystroke doesn't reload it from the Rails cache.
def self.candidates
key = ['customer_search_candidates', Customer.count, Customer.maximum(:updated_at).to_i]
Rails.cache.fetch(key, expires_in: 12.hours) do
Customer.pluck(:id, :name, :phone_number, :mobile_phone_number)
key = ['customer_search_candidates', 2, Customer.count, Customer.maximum(:updated_at).to_i]
cached_key, cached_rows = @candidates
return cached_rows if cached_key == key
rows = Rails.cache.fetch(key, expires_in: 12.hours) do
prepare(Customer.pluck(:id, :name, :phone_number, :mobile_phone_number))
end
@candidates = [key, rows]
rows
end
# [[id, name, phone, mobile], ...] -> [[id, name, phone_digits, tokens, sound_codes], ...]
def self.prepare(rows)
rows.map do |id, name, phone_number, mobile_number|
words = tokens(name)
phones = [phone_number, mobile_number].compact.map { |n| n.to_s.gsub(/\D/, '') }.reject(&:empty?)
[id, name.to_s, phones, words, words.map { |w| sound_codes(w) }]
end
end
# Pure ranking (no database), so it can be tested on its own.
# Pure ranking over prepared rows (no database), so it can be tested on its own.
# Returns [[id, score], ...] best first. A query of mostly digits is also treated as a phone number.
def self.rank(rows, name: nil, phone: nil, limit: DEFAULT_LIMIT)
def self.rank(prepared, name: nil, phone: nil, limit: DEFAULT_LIMIT)
name = name.to_s.strip
phone_digits = phone.to_s.gsub(/\D/, '')
@@ -119,15 +87,18 @@ class CustomerSearchService
name = ''
end
query_tokens = tokens(name)
return [] if query_tokens.empty? && phone_digits.length < 4
query_words = tokens(name)
return [] if query_words.empty? && phone_digits.length < 4
scored = rows.filter_map do |id, customer_name, phone_number, mobile_number|
query = query_words.map { |w| [w, sound_codes(w)] }
memo = {} # word-pair scores for this search; customer names repeat words a lot ("smith", "john")
scored = prepared.filter_map do |id, customer_name, phones, words, codes|
score = [
phone_digits.length >= 4 ? phone_score(phone_digits, [phone_number, mobile_number]) : 0.0,
query_tokens.any? ? name_score(query_tokens, customer_name) : 0.0
phone_digits.length >= 4 ? phone_score(phone_digits, phones) : 0.0,
query.any? ? name_score(query, words, codes, memo) : 0.0
].max
[id, score, customer_name.to_s] if score >= MIN_SCORE
[id, score, customer_name] if score >= MIN_SCORE
end
scored.sort_by { |id, score, customer_name| [-score, customer_name.downcase, id] }
@@ -144,8 +115,15 @@ class CustomerSearchService
meaningful.empty? ? words : meaningful
end
def self.phone_score(digits, numbers)
numbers.compact.map { |n| n.to_s.gsub(/\D/, '') }.reject(&:empty?).map do |number|
# Double Metaphone codes (primary and alternate) for a word; empty for very short or numeric words
def self.sound_codes(word)
return [] if word.length < 3 || word.match?(/\d/)
Text::Metaphone.double_metaphone(word).compact.reject(&:empty?).uniq
end
def self.phone_score(digits, phones)
phones.map do |number|
if number == digits || number.end_with?(digits) && digits.length >= 7 then 1.0
elsif number.include?(digits) then 0.9
else 0.0
@@ -155,41 +133,101 @@ class CustomerSearchService
# Each query word is matched to its best word in the customer's name; the score is the average.
# A query word with no match at all halves the score, so "bill jones" won't match "william smith".
def self.name_score(query_tokens, customer_name)
name_tokens = tokens(customer_name)
return 0.0 if name_tokens.empty?
def self.name_score(query, words, codes, memo = {})
return 0.0 if words.empty?
best = query_tokens.map { |q| name_tokens.map { |c| token_score(q, c) }.max }
best = query.map do |q_word, q_codes|
scores = (memo[q_word] ||= {})
words.each_with_index.map do |c_word, i|
scores.fetch(c_word) { scores[c_word] = word_score(q_word, q_codes, c_word, codes[i]) }
end.max
end
score = best.sum / best.size
score *= 0.5 if best.any?(&:zero?)
# Also compare the names with spaces removed, for "Mc Donald" vs "McDonald"
joined = DidYouMean::JaroWinkler.distance(query_tokens.join, name_tokens.join)
return score if score >= 0.9 # already a strong match; skip the whole-name comparison
# Also compare the names with spaces removed, for "Mc Donald" vs "McDonald". Only worth it when the
# strings could reach the 0.92 bar at all: same first letter and nearly the same length.
query_joined = query.map(&:first).join
name_joined = words.join
return score unless query_joined[0] == name_joined[0] && (query_joined.length - name_joined.length).abs <= 2
joined = DidYouMean::JaroWinkler.distance(query_joined, name_joined)
[score, joined >= 0.92 ? joined * 0.9 : 0.0].max
end
def self.token_score(query, candidate)
def self.word_score(query, query_codes, candidate, candidate_codes)
return 1.0 if query == candidate
return 0.95 if nicknames?(query, candidate)
return 0.9 if query.length >= 2 && candidate.start_with?(query) # still typing: "wil" -> "william"
return 0.85 if query.length >= 2 && nickname_prefix?(query, candidate) # "bil" -> "william" via "bill"
similarity = DidYouMean::JaroWinkler.distance(query, candidate)
# Sounds the same ("jon"/"john", "smyth"/"smith"). The similarity check keeps unrelated words that
# happen to share a code (e.g. "john" and "ann" via the Spanish-J alternate) from matching.
return 0.88 if similarity >= 0.7 && (query_codes & candidate_codes).any?
return 0.8 if query.length >= 4 && candidate.include?(query) # "donald" in "mcdonald"
return 0.7 if query.length == 1 && candidate.start_with?(query) # an initial
# Typos. Short words need to be closer to count.
similarity = DidYouMean::JaroWinkler.distance(query, candidate)
threshold = [query.length, candidate.length].min <= 4 ? 0.9 : 0.85
similarity >= threshold ? similarity * 0.9 : 0.0
end
# Nickname matching treats the data as connections between names (the file lists both directions,
# e.g. "william -> bill" and "bill -> william"). Two names match if they are connected directly, or both
# connect to a LONGER name they're short for: "bill" and "will" via "william". Requiring the shared
# name to be longer stops unrelated full names linking through a short one: "bill" is listed for both
# William and Robert, but that doesn't make William match Robert.
def self.nicknames?(a, b)
(NICKNAME_INDEX.fetch(a, []) & NICKNAME_INDEX.fetch(b, [])).any?
related_names(a).include?(b)
end
# A partly typed nickname of the candidate's name, e.g. "bil" for "william"
# A partly typed nickname of the candidate's name, e.g. "bil" for "william" (via "bill")
def self.nickname_prefix?(query, candidate)
NICKNAME_INDEX.fetch(candidate, []).any? do |index|
NICKNAME_GROUPS[index].any? { |name| name != candidate && name.start_with?(query) }
related_names(candidate).any? { |name| name.start_with?(query) }
end
# All names interchangeable with name, cached per process
def self.related_names(name)
cache = (@related_names_cache ||= {})
cache[name] ||= begin
graph = nickname_graph
direct = graph.fetch(name, EMPTY)
through_full_names = direct.select { |full| full.length > name.length }.flat_map do |full|
graph.fetch(full, EMPTY).select { |other| other.length < full.length }
end
((direct + through_full_names).uniq - [name]).freeze
end
end
EMPTY = [].freeze
# name => [connected names], built once per process from the bundled data plus EXTRA_NICKNAME_GROUPS
def self.nickname_graph
@nickname_graph ||= begin
graph = Hash.new { |h, k| h[k] = [] }
connect = lambda do |a, b|
next if a.empty? || b.empty? || a == b
graph[a] << b
graph[b] << a
end
if File.exist?(NICKNAMES_CSV)
CSV.foreach(NICKNAMES_CSV, headers: true) do |row|
connect.call(row['name1'].to_s.strip.downcase, row['name2'].to_s.strip.downcase)
end
end
EXTRA_NICKNAME_GROUPS.each do |group|
group.combination(2) { |a, b| connect.call(a, b) }
end
graph.transform_values { |names| names.uniq.freeze }.to_h.freeze
end
end
end
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+9
View File
@@ -0,0 +1,9 @@
# Nickname data
`names.csv` is from the [nicknames](https://github.com/carltonnorthern/nicknames) project by
Carlton Northern and contributors (commit `ed160b3`), used under the Apache License 2.0
(see `LICENSE.txt`). It is unmodified.
Each row is `name1,has_nickname,name2`. `CustomerSearchService` uses it so that a search for
"Bill Smith" finds "William Smith". To update it, replace `names.csv` with a newer copy from the
project; extra name groups can also be added in `CustomerSearchService::EXTRA_NICKNAME_GROUPS`.
File diff suppressed because it is too large. Load diff