[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: check the ratio of numeric/alphanumric words in given string
- From: Rena <hyperhacker@...>
- Date: Wed, 21 Jan 2015 05:29:03 -0500
On Wed, Jan 21, 2015 at 2:33 AM, DouOlivia <olivia2046@hotmail.com> wrote:
> Hi there,
>
> Should have performed a search in the list first, but I’m afraid I cannot
> come out with a good query. Here’s the question: Is there a library that can
> calculate the ratio of numberic or alphanumeric words in a given string (the
> actual scenario is to use a document’s content so the string will be very
> long)? E.g for string “34 aaa A-7SXD bbb ccc”, the ratio is calculated as
> 0.4.
>
>
>
> Thanks.
>
>
Seems like a simple task for Lua's pattern matching:
function countMatches(str, pat)
local c = 0
for word in str:gmatch(pat) do c = c + 1 end
return c
end
testStr = "34 aaa A-7SXD bbb ccc"
local ratio = countMatches(testStr, '%d+') / countMatches(testStr, '[%a%p]+')
assert(ratio == 0.4)
--
Sent from my Game Boy.