西班牙语的复数和单数

发布于 2024-10-10 16:23:26 字数 489 浏览 11 评论 0原文

抱歉我的英语...

我有一个开发到西班牙的rails应用程序,因此,所有内容都是西班牙语,所以,我有一个搜索框可以在mysql数据库中搜索,所有行都是西班牙语,我想改进我的搜索允许用户以单数或复数形式搜索关键字,例如:

keyword: patatas
found: patata

keyword: veces
found: vez

keyword: vez
found: veces

keyword: actividades
found: actividad

在英语中,借助单数和复数方法这可能相对容易......

where `searching_field` like '%singularized_keyword%' or `searching_field` like '%pluralized_keyword%'

但是,对于西班牙语......

有帮助吗?

谢谢!

sorry for my english...

I have a rails application developed to spain, therefore, all content is in spanish, so, I have a search box to search in a mysql database, all rows are in spanish, I'd like to improve my search to allow to users to search keywords in singular or plural form, for example:

keyword: patatas
found: patata

keyword: veces
found: vez

keyword: vez
found: veces

keyword: actividades
found: actividad

In english, this could be relatively easy with help of singularize and pluralize methods ...

where `searching_field` like '%singularized_keyword%' or `searching_field` like '%pluralized_keyword%'

But, for spanish....

Some help?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

猫腻 2024-10-17 16:23:26

您现在可以定义自己的变化。

查看 config/initializers/inflections.rb 中的

根据您的问题

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'patata', 'patatas'
end

示例因此,

"patata".pluralize # => "patatas"
"patatas".singularize #=> "patata"

当然您需要提前知道关键字列表才能使用 config/inflections.rb 中的不规则方法。查看该文件中注释掉的示例。还有其他方法允许您使用正则表达式定义规则,并且您可以设计模式匹配来影响与已知模式匹配的任意关键字的变形。

You can define your own inflections now.

look in config/initializers/inflections.rb

an example based on your question

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'patata', 'patatas'
end

Thus

"patata".pluralize # => "patatas"
"patatas".singularize #=> "patata"

Of course you need to know the list of keywords in advance to use the irregular method in config/inflections.rb. Have a look at the commented out examples in that file. There are other methods that allow one to define rules using regular expressions and you could devise pattern matches to affect inflections for arbitrary keywords that match known patterns.

墨小墨 2024-10-17 16:23:26

您必须清除英语中的所有默认词形变化并用西班牙语创建新的词形变化。

添加config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.clear :all

  inflect.plural /([^djlnrs])([A-Z]|_|$)/, '\1s\2'
  inflect.plural /([djlnrs])([A-Z]|_|$)/, '\1es\2'
  inflect.plural /(.*)z([A-Z]|_|$)$/i, '\1ces\2'

  inflect.singular /([^djlnrs])s([A-Z]|_|$)/, '\1\2'
  inflect.singular /([djlnrs])es([A-Z]|_|$)/, '\1\2'
  inflect.singular /(.*)ces([A-Z]|_|$)$/i, '\1z\2'
end

You have to clear all default inflections in English and create new ones in Spanish.

Add in config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.clear :all

  inflect.plural /([^djlnrs])([A-Z]|_|$)/, '\1s\2'
  inflect.plural /([djlnrs])([A-Z]|_|$)/, '\1es\2'
  inflect.plural /(.*)z([A-Z]|_|$)$/i, '\1ces\2'

  inflect.singular /([^djlnrs])s([A-Z]|_|$)/, '\1\2'
  inflect.singular /([djlnrs])es([A-Z]|_|$)/, '\1\2'
  inflect.singular /(.*)ces([A-Z]|_|$)$/i, '\1z\2'
end
囚你心 2024-10-17 16:23:26

看来现在可以使用本地化的变形:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:es) do |inflect|
  inflect.plural /([^djlnrs])([A-Z]|_|$)/, '\1s\2'
  inflect.plural /([djlnrs])([A-Z]|_|$)/, '\1es\2'
  inflect.plural /(.*)z([A-Z]|_|$)$/i, '\1ces\2'

  inflect.singular /([^djlnrs])s([A-Z]|_|$)/, '\1\2'
  inflect.singular /([djlnrs])es([A-Z]|_|$)/, '\1\2'
  inflect.singular /(.*)ces([A-Z]|_|$)$/i, '\1z\2'
end

这样(并且在重新启动服务器后)您可以使用:

"trebol".pluralize(:es) #=> "treboles"

It seems that it is now possible to use localized inflections now:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:es) do |inflect|
  inflect.plural /([^djlnrs])([A-Z]|_|$)/, '\1s\2'
  inflect.plural /([djlnrs])([A-Z]|_|$)/, '\1es\2'
  inflect.plural /(.*)z([A-Z]|_|$)$/i, '\1ces\2'

  inflect.singular /([^djlnrs])s([A-Z]|_|$)/, '\1\2'
  inflect.singular /([djlnrs])es([A-Z]|_|$)/, '\1\2'
  inflect.singular /(.*)ces([A-Z]|_|$)$/i, '\1z\2'
end

With that (and after restarting the server) you can use:

"trebol".pluralize(:es) #=> "treboles"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文