Matching plural words

32 views
Skip to first unread message

Josh V

unread,
Sep 29, 2012, 2:46:11 PM9/29/12
to treet...@googlegroups.com

I originally asked this on StackOverflow, but didn't get much of a response. Is there a way to programmatically match plural words using Treetop. The Linguistics gem will pluralize a word, but how can that be inserted back into the parser.

Here's an example of what I'm trying to do:

#!/usr/bin/env ruby
require
'treetop'
require
'linguistics'
include
Linguistics::EN
Treetop.load_from_string DATA.read

parser
= RecipeParser.new

p parser
.parse('cans')

__END__
grammar
Recipe
   rule units
      unit
&{|s| plural(s[0].text_value) }  
   
end
   rule unit
     
'can'
   
end
end

markus

unread,
Sep 29, 2012, 6:43:28 PM9/29/12
to treet...@googlegroups.com
Josh --

Perhaps I'm being dense, but it's not clear to me what you are trying to
accomplish. I'm not familiar with the linguistics gem, but (looking at
the tests

https://github.com/ged/linguistics/blob/master/spec/linguistics/en/pluralization_spec.rb

at least) it appears that plural should be called as:

"fish".en.plural

or

:box".en.plural(n)

And in any case appears to return a string. (One possible point of
confusion may be if I've found a different linguistics gem than you are
using).

You example looks like it should accept 'can', but only if the the
plural of 'can' is not nil or false, which is a complex way of saying it
looks like it only accepts "can".

If you are wanting to match a fixed set of words and accept their plural
or singular forms interchangeably, your best bet is to probably to just
include them as alternatives:

rule r1
"can" / "cans"
end

or for regular cases, form them with an optional "s" / "es":

rule r1
"can" "s"?
end

rule r2
"box" "es"?
end

I suspect, though, that this is not the sort of answer you're looking
for.

Really, it all depends on what you are trying to accomplish. If you
give us a bit more detail about that, we'll likely be able to help more
effectively.

-- MarkusQ


Josh V

unread,
Oct 9, 2012, 9:31:27 AM10/9/12
to treet...@googlegroups.com
Hi Markus, 
Thanks for your reply. I'm really looking for a solution to avoid having to include all the alternatives like (es, s, ies, etc.). 

Is there a way that you can match "can", save the result, run `plural` on it, and then attempt to match "cans" as well. 

I'm having a little bit of trouble describing it, but essentially I want to match both "can" and "cans" without having to explicitly specify its plural alternative.

-Josh

markus

unread,
Oct 9, 2012, 11:10:20 AM10/9/12
to treet...@googlegroups.com
Josh --

The most concise suggestion I can think of would be to have rules like:

rule s
's' / ''
end
rule ies
'ies' / 'y'
end
rule es
'es'/''
end

which would let you write things like this:

rule container
'can' s / 'box' es / 'bagg' ies
end

-- Markus



Reply all
Reply to author
Forward
0 new messages