You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The good news is that I am right now using Calyx in a real-world project. The bad news is that doing so means you quickly encounter edge cases...
Sometimes, your program has to generate sentences without knowing in advance if they'd be plural or not.
classCatWatch < Calyx::Grammarstart"Today, I saw {number} cats on the way to work."endreporter=CatWatch.newreporter.generate(number: 1)#=>"Today, I saw 1 cats on my way to work."
I thought about doing some monkey-patching to handle this sort of pluralization, with plans to eventually refactor to a "best practice" solution using modifiers...
However, this monkey-patched solution doesn't work in Calyx.
classCatWatch < Calyx::Grammarstart"Today, I saw {number.pluralize('cat')} on the way to work."endreporter=CatWatch.newreporter.generate(number: 1)=>"Today, I saw {number.pluralize('cat')} on the way to work."
Refactoring to "best practice" doesn't work either.
modulePluralizedefpluralize(possible_word)ifself.to_i == 1"#{self}#{possible_word}"else"#{self}#{possible_word}s"endendendclassCatWatch < Calyx::GrammarmodifierPluralizestart"Today, I saw {number.pluralize('cat')} on the way to work."endreporter=CatWatch.newreporter.generate(number: 1)#=>Today, I saw {number.pluralize('cat')} on the way to work.
Even more oddly, if I do a slight change to the start method...it is no longer gets interpreted as part of the string...even though I still get incorrect output.
Now, since I'm on a tough deadline, I wind up doing this solution instead to handle pluralization.
classStringdefpluralize_catsifself.to_i == 1"#{self} cat"else"#{self} cats"endendendclassCatWatch < Calyx::Grammarstart"Today, I saw {number.pluralize_cats} on the way to work."endreporter=CatWatch.newreporter.generate(number: 1)#=>"Today, I saw 1 cat on the way to work."
...which "works", but is pretty hacky. There has to be a better solution (one that hopefully doesn't involve using meta programming to generate a bunch of unique methods for handling pluralization). It'll do for now though.
I decided to follow best practice by moving my pluralize_cats method over to a module, but I then encountered a different issue with modifiers (which I reported in #21).
The text was updated successfully, but these errors were encountered:
In case anyone was curious, this was my final solution to this problem. If I get time, I'm going to look at Inflecto to see whether I can incorporate that library into this solution in case I ever need to deal with pluralization again.
moduleMacrosdefgenerate_pluralization_method(singular:,plural:)method_suffix=plural.gsub(" ","_")define_method:"pluralize_#{method_suffix}"do |argument|
if(argument.to_f == 1)"#{argument}#{singular}"else"#{argument}#{plural}"endendendendmoduleCatWatchHelpersextendMacrosgenerate_pluralization_method(singular: "cat",plural: "cats")endclassCatWatch < Calyx::GrammarmodifierCatWatchHelpersstart"Today, I saw {number.pluralize_cats} on the way to work."endreporter=CatWatch.newreporter.generate(number: 1)#=>Today, I saw 1 cat on the way to work.
The good news is that I am right now using Calyx in a real-world project. The bad news is that doing so means you quickly encounter edge cases...
Sometimes, your program has to generate sentences without knowing in advance if they'd be plural or not.
I thought about doing some monkey-patching to handle this sort of pluralization, with plans to eventually refactor to a "best practice" solution using modifiers...
However, this monkey-patched solution doesn't work in Calyx.
Refactoring to "best practice" doesn't work either.
Even more oddly, if I do a slight change to the start method...it is no longer gets interpreted as part of the string...even though I still get incorrect output.
Now, since I'm on a tough deadline, I wind up doing this solution instead to handle pluralization.
...which "works", but is pretty hacky. There has to be a better solution (one that hopefully doesn't involve using meta programming to generate a bunch of unique methods for handling pluralization). It'll do for now though.
I decided to follow best practice by moving my
pluralize_cats
method over to a module, but I then encountered a different issue with modifiers (which I reported in #21).The text was updated successfully, but these errors were encountered: