【问题标题】:Finding index of Korean Alphabet (any Unicode char) in Korean Word (any Unicode Word) in SQL server在 SQL Server 中查找韩语单词(任何 Unicode 字符)中韩语字母(任何 Unicode 字符)的索引
【发布时间】:2012-09-16 19:04:51
【问题描述】:

我需要按姓名搜索人员。这里的人名可以是英文、韩文或中文。为此,我使用Like 条件在Name 的基础上进行搜索,如下所示:

select * from [MyTable] where Name like N'%t%'

上面的声明是给所有包含t的用户。但这不适用于韩语或中文。就像我用韩文字母 ㅈ 搜索一样,它应该给出包含这个字母的所有名称,例如 **정수연, 재훈아이팟, 정원혁 테스트 7**。我尝试了以下方法,但结果为零

select * from [MyTable] where Name like N'%ㅈ%' - No Results
select PATINDEX(N'%ㅈ%',N'정수연(Mohan)') - giving value as ZERO
select Charindex(N'ㅈ',N'정수연') - giving value as ZERO

有没有办法在SQL server中查找其他语言的字母表?

我知道如何使用编码技术在 C# 单词中找到其他语言中存在的字母,但在 SQL Server 中却不知道。请在这方面帮助我。

提前致谢。

编辑 C# 代码

public static string DecomposeSyllabels(string unicodeString) {
      try {
        //Consonant consonant only used
        string[] JLT = { "ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ" };

        // Only used a collection of neutral
        string[] JVT = { "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅘ", "ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", "ㅣ" };

        // Initial and coda consonants used in
        string[] JTT = { "", "ㄱ", "ㄲ", "ㄳ", "ㄴ", "ㄵ", "ㄶ", "ㄷ", "ㄹ", "ㄺ", "ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅁ", "ㅂ", "ㅄ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ" };

        double SBase = 0xAC00;
        long SCount = 11172;
        int TCount = 28;
        int NCount = 588;
        string syllables = string.Empty;

        foreach (char c in unicodeString) {
          double SIndex = (int)c - SBase;
          if (0 > SIndex || SIndex >= SCount) {
            syllables = syllables + c;
            continue;
          }

          int LIndex = (int)Math.Floor(SIndex / NCount);
          int VIndex = (int)(Math.Floor((SIndex % NCount) / TCount));
          int TIndex = (int)(SIndex % TCount);
          syllables = syllables + (JLT[LIndex] + JVT[VIndex] + JTT[TIndex]);
        }

        return syllables;
      }
      catch {
        return unicodeString;
      }
    }

【问题讨论】:

  • 问题是 PATINDEX 和 CHARINDEX 搜索字符。而ㅈ 可以被认为是정 的一部分;在 Unicode 级别 U+3148 不是 U+C815 的“部分” - 它们是单独的字符。
  • 这个问题与Unicode规范化有关。在除 Macintosh 之外的大多数平台上,韩语由音节块编码。 Mac 按字母编码,渲染器将它们排列成音节块。使用编程语言,您将能够找到一些代码来进行NFC 和NFD 规范化,但是使用SQL,我不知道是否存在这样的功能。可能值得添加特殊列。

标签: sql-server unicode sql-server-2008-r2 cjk


【解决方案1】:

您必须分解韩语音节并将它们存储到 SQL 数据库中的单独列中(例如 ㅈㅓㅇㅅㅜㅇㅕㄴ 用于정수연)。我建议您编写一个小型自定义应用程序来解析您的数据库,分解所有韩语音节,并将结果保存到单独的列中。

编辑

下面是一些分解韩文音节的 Python 代码:

#!/usr/local/bin/python
# -*- coding: utf8 -*-
import codecs, sys, os, math

JLT="ㄱ,ㄲ,ㄴ,ㄷ,ㄸ,ㄹ,ㅁ,ㅂ,ㅃ,ㅅ,ㅆ,ㅇ,ㅈ,ㅉ,ㅊ,ㅋ,ㅌ,ㅍ,ㅎ".split(",")
JTT=",ㄱ,ㄲ,ㄱㅅ,ㄴ,ㄴㅈ,ㄴㅎ,ㄷ,ㄹ,ㄹㄱ,ㄹㅁ,ㄹㅂ,ㄹㅅ,ㄹㅌ,ㄹㅍ,ㄹㅎ,ㅁ,ㅂ,ㅂㅅ,ㅅ,ㅆ,ㅇ,ㅈ,ㅊ,ㅋ,ㅌ,ㅍ,ㅎ".split(",")
JVT="ㅏ,ㅐ,ㅑ,ㅒ,ㅓ,ㅔ,ㅕ,ㅖ,ㅗ,ㅘ,ㅙ,ㅚ,ㅛ,ㅜ,ㅝ,ㅞ,ㅟ,ㅠ,ㅡ,ㅢ,ㅣ".split(",")
SBase=0xAC00
SCount=11172
TCount=28
NCount=588

def HangulName(a):
 b=a.decode('utf8')
 sound=''
 for i in b:
  cp=ord(i)
  SIndex = cp - SBase
  if (0 > SIndex or SIndex >= SCount):
#  "Not a Hangul Syllable"
    pass

  LIndex = int(math.floor(SIndex / NCount))
  VIndex = int(math.floor((SIndex % NCount) / TCount))
  TIndex = int(SIndex % TCount)
  sound=sound+(JLT[LIndex] + JVT[VIndex] + JTT[TIndex]).lower()
 return sound

print HangulName("정수연")

dda$ python test.py
ㅈㅓㅇㅅㅜㅇㅕㄴ

【讨论】:

  • 我有 C# 中的代码来分解韩语文本中的韩语音节。通过使用此代码,我创建了要在 DB 中使用的 SQLCLR 函数,但它需要太多时间来分解。有没有办法在 SQL 中进行分解?
  • 分解不应该花很长时间。事实上,它在 Javascript 中甚至更快。所以你可能做错了什么。
  • 感谢分享代码。这确实有助于分配创建 C# 函数。我还将 C# 代码添加到您的答案中并接受您的答案。
猜你喜欢
  • 2014-07-31
  • 2012-06-12
  • 2018-10-30
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2018-12-19
  • 1970-01-01
相关资源
最近更新 更多