public int CompareTo(object o) { KanjiYomi other = (KanjiYomi)o; if (other.kanjiLength == kanjiLength) { if (okurigana > 0 && other.okurigana == 0) { return(-1); } else if (okurigana == 0 && other.okurigana > 0) { return(1); } else { return(Equals(other) ? 0 : objectIndex < other.objectIndex ? -1 : 1); } } else { return(other.kanjiLength < kanjiLength ? -1 : 1); } }
public bool ToKanji(KakasiReader input, TextWriter output) { char key = itaijiDictionary.Get((char)input.Get()); SortedSet <KanjiYomi> .Enumerator iterator = kanwaDictionary.Lookup(key); string rest = null; int restLength = 0; int resultLength = 0; while (iterator.MoveNext()) { KanjiYomi kanjiYomi = iterator.Current; int length = kanjiYomi.Length; if (rest == null) { char[] chars = new char[length + 1]; restLength = input.More(chars); for (int index = 0; index < restLength; index++) { chars[index] = itaijiDictionary.Get(chars[index]); } rest = new string(chars, 0, restLength); } if (length < resultLength) { break; } if (length > restLength) { continue; } if (kanjiYomi.GetYomiFor(rest) != null) { resultLength = length; break; } } if (resultLength > 0 && restLength > resultLength && rest[resultLength - 1] == '\u3063') { char nextCh = rest[resultLength]; UnicodeBlock block = UnicodeBlock.Of(nextCh); if (block.Equals(UnicodeBlock.Hiragana)) { ++resultLength; } } input.Consume(resultLength + 1); output.Write(key); if (resultLength > 0) { output.Write(rest.ToCharArray(), 0, resultLength); } return(true); }
public void AddItem(string kanji, string yomi, char okurigana) { UnicodeBlock kanjiBlock = UnicodeBlock.Of(kanji[0]); if (!kanjiBlock.Equals(UnicodeBlock.CJKUnifiedIdeographs)) { //System.err.println("KanwaDictionary: Ignored item:" + // " kanji=" + kanji + " yomi=" + yomi); return; } int kanjiLength = kanji.Length; StringBuilder kanjiBuffer = new StringBuilder(kanjiLength); for (int index = 0; index < kanjiLength; index++) { char ch = kanji[index]; //if (ch < '\u0100') { // System.err.println("KanwaDictionary: Ignored item:" + // " kanji=" + kanji + " yomi=" + yomi); // return; //} kanjiBuffer.Append(ItaijiDictionary.GetInstance().Get(ch)); } char key = kanjiBuffer[0]; kanji = kanjiBuffer.ToString().Substring(1); int yomiLength = yomi.Length; StringBuilder yomiBuffer = new StringBuilder(yomiLength); for (int index = 0; index < yomiLength; index++) { char ch = yomi[index]; UnicodeBlock block = UnicodeBlock.Of(ch); if (!block.Equals(UnicodeBlock.Hiragana) && !block.Equals(UnicodeBlock.Katakana)) { Console.Error.WriteLine("KanwaDictionary: Ignored item:" + " kanji=" + kanjiBuffer + " yomi=" + yomi); return; } if ((ch >= '\u30a1' && ch <= '\u30f3') || ch == '\u30fd' || ch == '\u30fe') { yomiBuffer.Append((char)(ch - 0x60)); } else if (ch == '\u30f4') { // 'vu' yomiBuffer.Append('\u3046'); yomiBuffer.Append('\u309b'); } else { yomiBuffer.Append(ch); } } yomi = yomiBuffer.ToString(); KanjiYomi kanjiYomi = new KanjiYomi(kanji, yomi, okurigana); if (!contentsTable.ContainsKey(key)) { contentsTable.Add(key, new SortedSet <KanjiYomi>()); } SortedSet <KanjiYomi> list = contentsTable[key]; list.Add(kanjiYomi); }
internal bool ToHiragana(KakasiReader input, TextWriter output) { char key = itaijiDictionary.Get((char)input.Get()); SortedSet <KanjiYomi> .Enumerator iterator = kanwaDictionary.Lookup(key); HashSet <string> yomiSet = new HashSet <string>(); string rest = null; int restLength = 0; int resultLength = 0; while (iterator.MoveNext()) { KanjiYomi kanjiYomi = iterator.Current; if (rest == null) { char[] chars = new char[kanjiYomi.Length + 1]; restLength = input.More(chars); for (int index = 0; index < restLength; index++) { chars[index] = itaijiDictionary.Get(chars[index]); } rest = new string(chars, 0, restLength); } Logger.Log("kanjiYomi: " + kanjiYomi.Kanji + "," + kanjiYomi.Yomi + "," + kanjiYomi.Okurigana + "," + kanjiYomi.Length + "," + restLength); if (kanjiYomi.Length < resultLength) { break; } if (kanjiYomi.Length > restLength) { continue; } string yomi = kanjiYomi.GetYomiFor(rest); if (yomi == null) { continue; } yomiSet.Add(yomi); resultLength = kanjiYomi.Length; if (!HeikiMode) { break; } } if (yomiSet.Count == 0) { return(false); } char additionalChar = (char)0; if (resultLength > 0 && restLength > resultLength && rest[resultLength - 1] == '\u3063') { char nextCh = rest[resultLength]; UnicodeBlock block = UnicodeBlock.Of(nextCh); if (block.Equals(UnicodeBlock.Hiragana)) { ++resultLength; additionalChar = nextCh; } } input.Consume(resultLength + 1); if (FuriganaMode) { output.Write(key); if (resultLength > 0) { output.Write(rest.ToCharArray(), 0, resultLength); } output.Write('['); } if (yomiSet.Count == 1) { output.Write(yomiSet.FirstOrDefault()); if (additionalChar > 0) { output.Write(additionalChar); } } else if (yomiSet.Count > 1) { HashSet <string> .Enumerator iter = yomiSet.GetEnumerator(); output.Write('{'); bool bar = false; while (iter.MoveNext()) { if (bar) { output.Write('|'); } output.Write(iter.Current); if (additionalChar > 0) { output.Write(additionalChar); } bar = true; } output.Write('}'); } if (FuriganaMode) { output.Write(']'); } return(true); }