postgres unaccent 函数 vs RoR 音译

编程入门 行业动态 更新时间:2024-10-10 15:27:06
本文介绍了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 音译

本文发布于:2023-10-18 15:21:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   postgres   unaccent   RoR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!