Rails 文章助手 - “a”或“一个”

发布于 2024-10-24 08:25:35 字数 285 浏览 9 评论 0原文

有谁知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我家小可爱 2024-10-31 08:25:35

我不知道,但为此编写一个助手似乎很简单,对吗?
从我的头顶

def indefinite_articlerize(params_word)
    %w(a e i o u).include?(params_word[0].downcase) ? "an #{params_word}" : "a #{params_word}"
end

希望有助于

编辑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

def indefinite_articlerize(params_word)
    %w(a e i o u).include?(params_word[0].downcase) ? "an #{params_word}" : "a #{params_word}"
end

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

征棹 2024-10-31 08:25:35

现在有一个 gem:indefinite_article

There is now a gem for this: indefinite_article.

冷心人i 2024-10-31 08:25:35

似乎检查第一个字母是元音就能让你明白大部分内容,但也有一些边缘情况:

  • 有些人会说“一个历史性时刻”,但写“一个历史性时刻”。
  • 但这是“历史”!
  • 首字母缩略词和缩写词有问题(“NBC 记者”但“NATO 权威”)
  • 以元音开头但以辅音开头的单词(“a union”)
  • 其他?

来源

Seems like checking that the first letter is a vowel would get you most of the way there, but there are edge cases:

  • Some people will say "an historic moment" but write "a historic moment".
  • But, it's "a history"!
  • Acronyms and abbreviations are problematic ("An NBC reporter" but "A NATO authority")
  • Words starting with a vowel but pronounced with an initial consonant ("a union")
  • Others?

(source)

給妳壹絲溫柔 2024-10-31 08:25:35

我知道以下答案对于实际的简单实现来说太过分了,但以防万一有人想在一定规模的实现下准确地做到这一点。

该规则实际上非常简单,但问题在于该规则取决于发音,而不是拼写:

如果初始声音是元音声音(不一定是元音字母) 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.

送舟行 2024-10-31 08:25:35

查看 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.

べ映画 2024-10-31 08:25:35

如果你想要一个全面的解决方案,我喜欢gem。但如果你只是想让测试读得更好,猴子修补字符串以遵循标准 Rails

class String
  def articleize
    %w(a e i o u).include?(self[0].downcase) ? "an #{self}" : "a #{self}"
  end
end

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:

class String
  def articleize
    %w(a e i o u).include?(self[0].downcase) ? "an #{self}" : "a #{self}"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文