ToolGood.Words.Pinyin icon indicating copy to clipboard operation
ToolGood.Words.Pinyin copied to clipboard

分界符能否仅加在中文字符之间或中文字符与其他字符之间

Open taooceros opened this issue 4 years ago • 7 comments

如题所示 是否可以让如WordsHelper.GetPinyin("你好China"," ") 这种中英文都有的字符串翻译出来成为"Ni Hao China",而不是现在的"Ni Hao C H I N A)。 或者是否能提供一个方法指示所有中文字符存在的索引。 谢谢

taooceros avatar Dec 11 '20 13:12 taooceros

顺便小问一下,为什么重启翻译出来时ZhongQi hhhhhh

taooceros avatar Dec 11 '20 14:12 taooceros

提供一个可行的方案,但是有一点丑陋

 var resultList = WordsHelper.GetPinyinList(content);

List<int> chineseIndexs = new List<int>();

for (int i = 0; i < content.Length; i++)
{
         if (resultList[i].Length != 1 || !(resultList[i][0] == content[i]))
         chineseIndexs.Add(i);
}
int currentChineseIndex = 0;
int lastChineseIndex = -1;
for (int i = 0; i < resultList.Length; i++)
{
    if (currentChineseIndex < chineseIndexs.Count && chineseIndexs[currentChineseIndex] == i)
    {
        resultBuilder.Append(' ');

        resultBuilder.Append(resultList[i]);
        currentChineseIndex++;
        lastChineseIndex = i;
    }
 else
  {
     if (i == lastChineseIndex + 1)
     {
         resultBuilder.Append(' ');
     }
     resultBuilder.Append(resultList[i]);
   }
 }

taooceros avatar Dec 11 '20 14:12 taooceros

顺便小问一下,为什么重启翻译出来时ZhongQi hhhhhh

拼音库不全,所以会出现这个问题

toolgood avatar Dec 11 '20 14:12 toolgood

        var pys = PinyinDict.GetPinyinList(text, tone ? 1 : 0);
        StringBuilder stringBuilder = new StringBuilder();
        bool pre = false;

        for (int i = 0; i < text.Length; i++) {
            var py = pys[i];
            if (py.Length > 1 || (text[i] >= 0x3400 && text[i]<= 0x9FD5)) { 
                if (stringBuilder.Length > 0) {
                    stringBuilder.Append(' ');
                }
                stringBuilder.Append(py);
                pre = true;
            } else {
                if (pre) {
                    stringBuilder.Append(' ');
                }
                stringBuilder.Append(text[i]);
                pre = false;
            }
        }
        return stringBuilder.ToString();

toolgood avatar Dec 11 '20 14:12 toolgood

if (py.Length > 1 || (text[i] >= 0x3400 && text[i]<= 0x9FD5)) { 可以再次简化 if (text[i] >= 0x3400 && text[i]<= 0x9FD5) {

toolgood avatar Dec 11 '20 14:12 toolgood

谢谢!

taooceros avatar Dec 12 '20 01:12 taooceros

是不是没有合并到主版本啊,我的也会这样,英文被拆成了单个字母。

giszh86 avatar Apr 21 '22 02:04 giszh86