我正在寻找代码来返回输入的缩写,即"联邦调查局"应该返回FBI(最好没有o),也适用于低等级联邦调查局"。我该怎么做呢? 感谢
答案 0 :(得分:1)
这样的事情可能有用:
("Federal bureau of Investigation")
:gsub("of","") -- remove "of"
:gsub("(%w)%S+%s*","%1") -- leave first character of a word
:upper() -- convert to uppercase
这将返回" FBI"
答案 1 :(得分:1)
这个怎么样?
Content-Length: 0
它处理简单的案例:
do
-- default list of words to exclude or reshape
local stopwords = { }
for w in ("a an and for of the to"):gmatch "%w+" do stopwords[w] = "" end
-- abbreviating a phrase:
function TLA( phrase, subst )
subst = subst or stopwords
-- first replace each word (incl. "'") by its abbreviation...
-- (will leave spaces etc. in the string)
phrase = phrase:gsub( "[%w']+", function( word )
if not word:find "%U" then return word end -- OPTIONAL keep abbrevs
word = word:lower()
if subst[word] then return subst[word] end -- from substitution list
return word:sub( 1, 1 ):upper( ) -- others: to first letter
end )
-- ...then remove all non-word characters
return (phrase:gsub( "%W", "" ))
end
end
可以处理一些特殊情况:
TLA "Ministry Of Information" --> "MI"
TLA "floating-point exception" --> "FPE"
并调整替换列表/将非空字符串放入也可能有用:
TLA "augmented BNF" --> "ABNF"
像往常一样,
TLA "one way or the other" --> "OWOO"
TLA( "one way or the other", {} ) --> "OWOTO"
TLA( "Ministry Of Information", { of = "of" } ) --> "MofI"
local custom_subst = {
["for"] = "4", to = "2", ["and"] = "", one = "1", two = "2", -- ...
}
TLA "Ministry for Fear, Uncertainity and Doubt" --> "MFUD"
TLA( "Ministry for Fear, Uncertainity and Doubt", custom_subst ) --> "M4FUD"
TLA( "Two-factor authentication", custom_subst ) --> "2FA"
和
TLA( "there ain't no such thing as a free lunch", {} ) --> "TANSTAAFL"
- 所以除了替换列表之外,你可能还想在代码中调整很多东西。
答案 2 :(得分:0)
显然,除非你使用特定的字典,否则没有完美的通用解决方案,因为许多缩写不符合一致的规则。
例如,考虑BASIC =初学者的通用符号指令代码。
(应该是BAPSIC而不是BASIC)
因此,如上所述,有一些限制,这是另一个可能的缩写词生成器,似乎适用于大多数“正常”的情况。
function acronym(s,ignore)
ignore = ignore or
{ --default list of words to ignore
['a'] = true, ['an'] = true, ['and'] = true, ['in'] = true, ['for'] = true,
['of'] = true, ['the'] = true, ['to'] = true, ['or'] = true,
}
local ans = {}
for w in s:gmatch '[%w\']+' do
if not ignore[w:lower()] then ans[#ans+1] = w:sub(1,1):upper() end
end
return table.concat(ans)
end