动态添加变音符号到角色 [英] Dynamicly add diacritics to characters

查看:58
本文介绍了动态添加变音符号到角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友!

我正在尝试创建一个将变音符号添加到字符的函数:

Hey friends!

I''m trying to create a function that adds diacritics to a character :

private static char AddDiacritics(char Letter, char Character)
{
   return (modified letter);
}



例如,如果我调用函数AddDiacritics(''e'', ''\"'');,我希望该函数返回字符ë.
如果我调用函数AddDiacritics(''u'', ''^'');,我希望函数返回û

框架中对此有某种支持吗?

谢谢,

Eduard



For example, if I call the function AddDiacritics(''e'', ''\"''); I want the function to return the character ë.
If I call the function AddDiacritics(''u'', ''^''); I want the function to return û

Is there some kind of support in the framework for this?

Thanks,

Eduard

推荐答案

AFAIK,对此没有内置支持.
通常,应该希望在输入信息时由用户键盘来处理.如果必须回顾性地使用它,那就太麻烦了,因为您将不得不考虑哪些字符可以添加哪个变音符号.
AFAIK, there is no built in support for this.

Normally, it would be expected that the user keyboard takes care of it when the info is entered. If you have to retrospectively work it, it becomes quite fiddly, as you will have to take account of which characters can have which diacritic added.


我不知道内置的解决方案为了这.如格里夫所说,通常这种事情将由IME完成.

您可能需要创建一个查找表,该表在逻辑上是一个二维映射,您可能会实现为Dictionary< char,Dictionary< char,char>.像这样在静态构造器中填充它:
I''m not aware of an inbuilt solution for this. Normally such a thing would be done by the IME as Griff says.

You probably need to create a lookup table, which is logically a two-dimensional map and which you probably would implement as Dictionary<char, Dictionary<char,char>>. Populate it in a static constructor like so:
Dictionary<char, Dictionary<char,char>> lookupTable = new Dictionary<char, Dictionary<char,char>>;
static MyClass(){
 lookupTable[''a''] = new Dictionary<char,char>();
 lookupTable[''a''][''"''] = ''ä'';
 lookupTable[''a''][''o''] = ''å'';
 // ... etc



我不确定ASCII集中是否有一套标准的变音标记,使用Unicode中的变音标记也没有多大意义,因为如果用户可以输入它们,则他们可以直接输入他们想要的字符.使用ANSI(西欧)时需要的是坟墓(è),尖锐的(é),变音符(ä),回旋符(â),圆环(å),斜线(ø),caron(š),波浪号(ñ)和塞地拉(ç);用于标记它们的明智字符将是',',,^,o,v,〜和c.在ANSI集中还包括eth和thorn,它们并不是正常字符的变音符号; oe和ae



I''m not sure if there is even a standard set of diacritical marks from the ASCII set, and there''s not much point using the ones in Unicode since if the user can enter those, they can enter the character they want directly. Those you need for ANSI (Western Europe) are grave (è), acute (é), umlaut (ä), circumflex (â), ring (å), slash (ø), caron (š), tilde (ñ) and cedila (ç); sensible characters to use to mark them would be `, '', ", ^, o, v, ~ and c. In the ANSI set there are also eth and thorn, which are not really diacritics of a normal character; oe and ae diphthongs; and the German ß, to think about.


OriginalGriff是正确的,没有能够处理所有情况的算法,唯一的选择是一个一个地处理所有可能性.

您可以使用enum列出所有变音符号以及每个受支持字母的字典.例如:

OriginalGriff is right there is no algorithm able to deal with all cases. The only option is to deal with all the possibilities one by one.

You could use an enum to list all diacritics, and dictionaries for each supported letter. For example:

enum Diacritic
{
    //"é"
    Acute,
    //"è"
    Grave,
    //"ê"
    Circonflexe,
    ...
}

//supported letters for e
static Dictionary<Diacritic, char> letterE;
//supported letters for a
static Dictionary<Diacritic, char> letterA;
//supported letters
static Dictionary<char, Dictionary<Diacritic, char>> letters;

//call this function to initialize dictionaries
//or put its code it a static constructor
static void Init()
{
    //all possibilities for "e"
    letterE = new Dictionary<Diacritic, char>();
    letterE[Diacritic.Acute] = 'é';
    letterE[Diacritic.Grave] = 'è';
    letterE[Diacritic.Circonflexe] = 'ê';
    ...

    //all possibilities for "a"
    letterA = new Dictionary<Diacritic, char>();
    letterA[Diacritic.Grave] = 'à';
    letterA[Diacritic.Circonflexe] = 'â';
    ...

    //supported letters
    letters = new Dictionary<char, Dictionary<Diacritic, char>>();
    letters.Add('a', letterA);
    letters.Add('e', letterE);
    ...
}

//will throw a KeyNotFoundException if the requested character doesn't exist
static char AddDiacritics(char letter, Diacritic diacritic)
{
    return letters[letter][diacritic];
}



这段代码不是很优雅,但是我不确定是否存在针对该问题的更优雅的解决方案...



This code is not very elegant but I am not sure if a more elegant solution exists for that problem...


这篇关于动态添加变音符号到角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆