我认为,即使对于任意用户输入,这里也有一些强大的功能。首先,将每个团队(我使用 3 个字母的代码作为每个团队的规范名称)映射到一个完整拼写的版本,其中包含城市和团队名称以及城市和团队名称之间括号中的任何昵称。
Scan[(fullname[First@#] = #[[2]])&, {
{"ari", "Arizona Cardinals"}, {"atl", "Atlanta Falcons"},
{"bal", "Baltimore Ravens"}, {"buf", "Buffalo Bills"},
{"car", "Carolina Panthers"}, {"chi", "Chicago Bears"},
{"cin", "Cincinnati Bengals"}, {"clv", "Cleveland Browns"},
{"dal", "Dallas Cowboys"}, {"den", "Denver Broncos"},
{"det", "Detroit Lions"}, {"gbp", "Green Bay Packers"},
{"hou", "Houston Texans"}, {"ind", "Indianapolis Colts"},
{"jac", "Jacksonville Jaguars"}, {"kan", "Kansas City Chiefs"},
{"mia", "Miami Dolphins"}, {"min", "Minnesota Vikings"},
{"nep", "New England Patriots"}, {"nos", "New Orleans Saints"},
{"nyg", "New York Giants NYG"}, {"nyj", "New York Jets NYJ"},
{"oak", "Oakland Raiders"}, {"phl", "Philadelphia Eagles"},
{"pit", "Pittsburgh Steelers"}, {"sdc", "San Diego Chargers"},
{"sff", "San Francisco 49ers forty-niners"}, {"sea", "Seattle Seahawks"},
{"stl", "St Louis Rams"}, {"tam", "Tampa Bay Buccaneers"},
{"ten", "Tennessee Titans"}, {"wsh", "Washington Redskins"}}]
然后,对于任何给定的字符串,找到每个团队全名的最长公共子序列。为了优先匹配开头或结尾的字符串(例如,“car”应该匹配“carolina panthers”而不是“arizona cardinals”)将输入字符串和全名夹在空格之间。无论哪个团队的全名具有输入字符串的 [sic:] 最长最长公共子序列,都是我们返回的团队。这是该算法的 Mathematica 实现:
teams = keys@fullnames;
(* argMax[f, domain] returns the element of domain for which f of that element is
maximal -- breaks ties in favor of first occurrence. *)
SetAttributes[argMax, HoldFirst];
argMax[f_, dom_List] := Fold[If[f[#1] >= f[#2], #1, #2] &, First@dom, Rest@dom]
canonicalize[s_] := argMax[StringLength@LongestCommonSubsequence[" "<>s<>" ",
" "<>fullname@#<>" ", IgnoreCase->True]&, teams]