漢字 包括多音字 轉換拼音和簡碼的處理

2022-02-03 09:54:01 字數 2556 閱讀 3911

將漢字轉換成拼音,由於漢字有多音字,所以轉換拼音是讓人很頭疼的乙個問題,我想到了乙個解決這個問題的辦法,就是將每個字的音節都讀出來,然後進行拼音重組,這樣總有一組拼音是正確的,然後在根據全文搜尋技術,就能找到想要的記錄了,本人不才自己寫了個方法,貼出了,跟大家分享一下,希望多批評指正!!

using system;

using system.collections.generic;

using system.linq;

using system.text;

using microsoft.international.converters.pinyinconverter;

namespace chineseconvertpinyin

}return true;

}///

/// 獲得字串全拼(包含多音字功能)

///

/// 轉換的字串

/// 0:給定的字串中包含其他字元 ,其他:拼音(格式 全拼/全拼/全拼@簡碼,簡碼,簡碼)

public string getpinyinlist(string chinesechar)

//獲取每個漢字拼音列表

list> list = topinyin(chinesechar);

//定義輸出列表,用於存在最終拼寫組合的結果

listlistout = new list();

//重組拼音

for (int i = 0; i < list.count; i++)

if (k != 0)//比較前後讀音是否是音調不同 例如 hang2 heng2 這樣就只記錄一次

//最後乙個記錄以及在第list[i].count-1  次完成判斷了

string s1 = system.text.regularexpressions.regex.replace(((system.collections.objectmodel.readonlycollection)list[i])[k - 1], @"[0-9]+", "");

string s2 = system.text.regularexpressions.regex.replace(((system.collections.objectmodel.readonlycollection)list[i])[k], @"[0-9]+", "");

if (s1 == s2)//把數字去掉,只比字母 相等則證明是音調不一樣

else

}else

}continue;

}//中間變數 存放臨時重組拼音結果

listtemp = new list();

//遍歷第i個字的所有讀音

for (int p = 0; p < list[i].count; p++)

}else //需要判斷聲調的問題

if (p == list[i].count - 1) //最後乙個記錄以及在第list[i].count-1  次完成判斷了

string t1 = system.text.regularexpressions.regex.replace(((system.collections.objectmodel.readonlycollection)list[i])[p - 1], @"[0-9]+", "");

string t2 = system.text.regularexpressions.regex.replace(((system.collections.objectmodel.readonlycollection)list[i])[p], @"[0-9]+", "");

if (t1 == t2)//音相同

else}}

}listout = temp;//將本次重組的結果複製給 定義輸出列表,用於存放最終拼寫組合的結果,

}return dealresult(listout);

}///

/// 處理最後結果(去掉聲調數字、和簡碼)

///

///

///

private static string dealresult(listlistout)

}if (!jm.contains(tempjm))

string temp = system.text.regularexpressions.regex.replace(listout[i], @"[0-9]+", "");//去掉拼音組的數字

results += temp + "/";

}if (results.lastindexof('/') >= 0)

return results + "@" + string.join(",", jm.toarray());

}//注意多音字  

///

/// 指定漢字查詢其所有的音節

///

/// 乙個漢字

/// 拼音列表

private list> topinyin(string str)

", chars.pinyins[0]);

屬性標識是不是多音字,                  

拼音的個數           }}

return list;}}

}

vue整合漢字轉拼音(附多音字解決方案)

1 結果顯示 輸出首字母 n 輸出拼音 nihaoma 2 js呼叫 import hanzitopinyin from hanzitopinyin export default class message extends vue 3 js實現 import vue from vue export ...

擷取字串(包括漢字)

程式設計 編寫乙個擷取字串的函式,輸入為乙個字串和位元組數,輸出為按位元組擷取的字串。但是要保證漢字不被截半個,如 我 abc 4,應該截為 我 ab 輸入 我 abc 漢 def 6,應該輸出為 我 abc 而不是 我abc 漢的半個 public class splitstr 計算輸入字串的總位...

SDI多音軌問題

音訊嵌入 包分配 對hanc,有三種不同的資料報可供選擇 音訊資料 包頭中以id為特徵的輔助資料,以及控制資料。所有這三種型別都有相似的結構,並具有乙個相同的特點 根據內容不同,長度可變。音訊資料被封裝在包中,每個包包括四個聲道或兩對立體聲道,加上額外資料 aes ebu訊號的c u v資料 這是音...