Rails 文章助手 - “a”或“一个”
有谁知道 Rails Helper 可以自动将适当的文章添加到给定的字符串前面?例如,如果我将“apple”传递给函数,它将返回“一个苹果”,而如果我要发送“香蕉”,它将返回“一个香蕉”
我已经检查了 Rails TextHelper 模块 但找不到任何内容。如果这是重复的,我们深表歉意,但不可否认,这是一个很难搜索的答案......
Does anyone know of a Rails Helper which can automatically prepend the appropriate article to a given string? For instance, if I pass in "apple" to the function it would turn out "an apple", whereas if I were to send in "banana" it would return "a banana"
I already checked the Rails TextHelper module but could not find anything. Apologies if this is a duplicate but it is admittedly a hard answer to search for...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道,但为此编写一个助手似乎很简单,对吗?
从我的头顶
希望有助于
编辑1:还找到了这个带有补丁的线程,可以帮助您进一步防弹 https://rails.lighthouseapp.com/projects/8994/tickets/2566-add-aan-inflector-indefinitize
None that I know of but it seems simple enough to write a helper for this right?
Off the top of my head
hope that helps
edit 1: Also found this thread with a patch that might help you bulletproof this more https://rails.lighthouseapp.com/projects/8994/tickets/2566-add-aan-inflector-indefinitize
现在有一个 gem:indefinite_article。
There is now a gem for this: indefinite_article.
似乎检查第一个字母是元音就能让你明白大部分内容,但也有一些边缘情况:
(来源)
Seems like checking that the first letter is a vowel would get you most of the way there, but there are edge cases:
(source)
我知道以下答案对于实际的简单实现来说太过分了,但以防万一有人想在一定规模的实现下准确地做到这一点。
该规则实际上非常简单,但问题在于该规则取决于发音,而不是拼写:
如果初始声音是元音声音(不一定是元音字母) strong>),然后在前面加上“an”,否则在前面加上“a”。
参考约翰的例子:
“an hour”是因为这里的“h”是元音,而“a Historical”是因为这里的“h”是辅音。 “an NBC”,因为这里的“N”读作“en”,而“a NATO”,因为这里的“N”读作“n”。
因此,问题归结为找出:“某些字母何时发音为元音”。为此,您需要以某种方式访问具有每个单词的语音表示的字典,并检查其初始音素。
I know the following answer goes too much for a practical simple implementation, but in case someone wants to do it with accuracy under some scale of implementation.
The rule is actually pretty much simple, but the problem is that the rule is dependent on the pronounciation, not spelling:
If the initial sound is a vowel sound (not necessarily a vowel letter), then prepend 'an', otherwise prepend 'a'.
Referring to John's examples:
'an hour' because the 'h' here is a vowel sound, whereas 'a historic' because the 'h' here is a consonant sound. 'an NBC' because the 'N' here is read as 'en', whereas 'a NATO' because the 'N' here is read as 'n'.
So the question is reduced to finding out: "when are certain letters pronounced as vowel sounds". In order to do that, you somehow need to access a dictionary that has phonological representations for each word, and check its initial phoneme.
查看 https://deveiate.org/code/linguistics/ - 它提供了对不定冠词的处理还有更多。我已经在很多项目中成功地使用了它。
Look into https://deveiate.org/code/linguistics/ - it provides handling of indefinite articles and much more. I've used it successfully on many projects.
如果你想要一个全面的解决方案,我喜欢gem。但如果你只是想让测试读得更好,猴子修补字符串以遵循标准 Rails
I love the gem if you want a comprehensive solution. But if you just want tests to read more nicely, it is helpful to monkey patch String to follow the standard Rails inflector pattern: