本文介绍了postgres unaccent 函数 vs RoR 音译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的 RoR 项目中,我们使用 postgres unaccent 函数来检索我们的模型名称属性之一的 unaccent 版本.name 属性可以包含来自各种语言的任何重音字符.然后我们将其保存为 unaccent_name 属性.我不喜欢这个解决方案,因为我们需要确保已经安装并可以访问 postgres 扩展程序 UNACCENT(在测试、移动/清理数据库等时).

In our RoR project we use postgres unaccent function to retrieve unaccent version of one of our models name attribute. The name attribute can contain any accented characters from various languages. We then save it as unaccent_name attribute. I don't like this solution because we need to be sure to have installed and accessible postgres extension UNACCENT (when testing, moving/cleaning database, and so on).

在 RoR 中有 ActiveSupport::Inflector.transliterate 方法,它应该做一些非常相似的事情.

In RoR there is ActiveSupport::Inflector.transliterate method, which should do something very similar.

我发现它主要以相同的方式翻译重音字符,但也有一些不同:

I've found that it mostly translates accented characters the same way, but there is also some difference:

相同的结果:

SELECT unaccent('ľščťžý') AS unaccent_name;
=> "lsctzy"
ActiveSupport::Inflector.transliterate('ľščťžý')
=> "lsctzy"

不同的结果:

SELECT unaccent('ß') AS unaccent_name;
=> "S"
ActiveSupport::Inflector.transliterate('ß')
=> "ss"

我知道这两种方法都可以接受带有自定义字母替换的字典,但我只对它们的一般/默认用法感兴趣.

I know both of these methods can accept dictionaries with custom letter replacements, but I'm only interested in their general/default usage.

音译方法的主要目的是否与 postgres unaccent 函数相同?我们可以用它作为替代品吗?

Is the main purpose of transliterate method same as postgres unaccent function? Can we use it as a replacement?

推荐答案

很老的帖子,但我正在解决一个类似于 OP 的问题.我们希望能够搜索名称并进行音译以提供更好的结果.但是,在我们的 Postgres 和 rails 版本中,字符将相同的音译为ss".

Very old post but I am working through a problem similar to the OP. We want to be able to search for a name and transliterate to give better results. However, with our versions of Postgres and rails the character transliterates the same to 'ss'.

只是想分享我的发现,以防它对偶然发现这篇文章的其他人有用.

Just wanted to share my findings in case it may be useful to others who stumble across this post.

在 Rails 5.2 中:

In rails 5.2:

irb(main):001:0> ActiveSupport::Inflector.transliterate('ß')
=> "ss"

在 postgres 9.6 中我得到:

In postgres 9.6 I get:

db-test=# SELECT unaccent('ß') AS unaccent_name;
 unaccent_name 
---------------
 ss
(1 row)

这篇关于postgres unaccent 函数 vs RoR 音译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 15:26