Example #1
0
        public RadKey()
        {
            InitializeComponent();

            // Initialize OS-specific components.
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                Windows_InitializeComponent();
                restoreHotkey = Windows_CreateRestoreHotkey();
            }
            else
            {
                Linux_InitializeComponent();
            }

            KanjiDictionary kanjiDictionary = new KanjiDictionary("kanjidic"); //loadKanjiDic(); // Making this non-static would mean KanjiData would need to store a static reference to it for sorting?

            Compound.SetKanjiDictionary(kanjiDictionary);

            KanjiToRadicalDictionary kanjiToRadicalDictionary = new KanjiToRadicalDictionary("kradfile");

            SelectionInfoHandler.SetKanjiToRadicalDictionary(kanjiToRadicalDictionary, kanjiDictionary);
            TextConverter.SetKanjiToRadicalDictionary(kanjiToRadicalDictionary);

            kanjiSearchManager = new KanjiSearchHandler(
                new KanjiSearch(
                    new RadkfileDictionary("radkfile")), kanjiDictionary);

            compoundSearchHandler = new CompoundSearchHandler(
                new CompoundSearch(
                    new CompoundDictionary("JMdict_e"), kanjiToRadicalDictionary));



            messageBox.Text = NameToRadicalDictionary.Load(radicalNameFile) + Environment.NewLine + messageBox.Text;
        }
Example #2
0
        /// <summary>
        /// Given a string containing radicals and English text, find the first occurence of English text,
        /// look up a radical associated with it, replace the English text with the radical, and return a Tuple
        /// containing the new string and position where the text was updated.
        /// </summary>
        /// <param name="input">Test.</param>
        /// <param name="originalStart"></param>
        /// <param name="success"></param>
        /// <returns></returns>
        private static Tuple <string, int> ConvertToRadical(string inputText, int originalSelectionStart, out bool success)
        {
            // If the input is empty, return it as is.
            if (inputText == "")
            {
                success = false;
                return(Tuple.Create(inputText, 0));
            }

            // Parse the input string to find the first occurence of English text, where English text is
            // defined as text NOT in the dictionary.
            int start = -1;
            int end   = -1;

            // First, get the start position.
            for (int x = 0; x < inputText.Length; x++)
            {
                // See if the character at the current position is English text or *.
                if (!Regex.IsMatch(inputText[x].ToString(), RegexPatterns.isSpecialPattern))
                {
                    if (Regex.IsMatch(inputText[x].ToString(), RegexPatterns.isLatinPattern))
                    {
                        start = x;
                        end   = x; // End is set here too. This will stop one-character radical names from being ignored.
                        break;
                    }
                }
            }

            // If the user only sent radicals/reserved characters, start will still be -1.
            // Return the original start position.
            if (start == -1)
            {
                success = false;
                return(Tuple.Create(inputText, originalSelectionStart));
            }

            // Next, get the end position.
            // NOTE TO SELF: Is there a more elegant way to do this?
            for (end = start; end < inputText.Length; end++)
            {
                if (Regex.IsMatch(inputText[end].ToString(), RegexPatterns.isNotLatinPattern))
                {
                    // The charcter at position 'end' is in the dictionary. Return one past the last valid English letter.
                    break;
                }

                else if (Regex.IsMatch(inputText[end].ToString(), RegexPatterns.isSpecialPattern))
                {
                    break;
                }
            }

            // Store the substring we want to use to look up the radical.
            // Need to add 1 here to get the correct length.
            string radicalLookup = inputText.Substring(start, end - start);

            // Attempt to look up that radical. This returns "" if nothing is found.
            string radical = NameToRadicalDictionary.Lookup(radicalLookup);

            // Set success flag indicating whether the radical was found.
            if (radical != "")
            {
                success = true;
            }
            else
            {
                success = false;
            }


            // Then, insert it into original string and return that.
            // If a radical was not found, this just removes the input text.
            string temp = string.Concat(inputText.Substring(0, start), radical);

            // If end = the last character of input, we need to handle it differently.
            int newSelectionStart;


            // If the cursor was before the characters being replaced, no need to change it.
            if (originalSelectionStart < start)
            {
                newSelectionStart = originalSelectionStart;
            }
            else
            {
                // Explanation: originalSelectionStart-(end-start) accounts for:
                // rad|* <- rad starts at 0, ends at 2, the cursor is at 3.
                // rad*| <- rad starts at 0, ends at 2, the cursor is at 4.
                // *rad*| <- rad starts at 1, ends at 3, the cursor is at 5.
                newSelectionStart = (originalSelectionStart - (end - start)) + radical.Length;
                // When no matching radical is found, newSelectionStart can go negative. Move it back to the starting position of the replaced text.
                if (newSelectionStart < 0)
                {
                    newSelectionStart = start;
                }
            }

            if (end == inputText.Length)
            {
                return(Tuple.Create(temp, newSelectionStart));
            }
            else
            {
                return(Tuple.Create(string.Concat(temp, inputText.Substring(end, inputText.Length - end)), newSelectionStart));
            }
        }
Example #3
0
 private void RadKey_KeyDown(object sender, KeyEventArgs e)
 {
     // Ctrl+F? Sort by freq/strokes.
     if (e.Control && e.KeyCode == Keys.F)
     {
         toggleSortByFreq();
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // Ctrl+J: Hide/show infrequent kanji.
     else if (e.Control && e.KeyCode == Keys.J)
     {
         toggleNoLowFreq();
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // Ctrl+shift+C: Copy the contents of the Selected Kanji box.
     else if (e.Control && e.Shift && e.KeyCode == Keys.C)
     {
         if (selectedKanjiBox.Text.Length > 0)
         {
             TextConverter.ConvertToKana(selectedKanjiBox);
             Clipboard.SetText(selectedKanjiBox.Text);
         }
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // Ctrl+shift+E: Copy the contents of the Selected Kanji box as shift-jis bytes.
     else if (e.Control && e.Shift && e.KeyCode == Keys.E)
     {
         if (selectedKanjiBox.Text.Length > 0)
         {
             Clipboard.SetText(TextConverter.ConvertToShiftJISBytes(selectedKanjiBox.Text));
         }
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // Ctrl+Shift+V: Append the contents of the buffer onto the Selected Kanji box.
     else if (e.Control && e.Shift && e.KeyCode == Keys.V)
     {
         selectedKanjiBox.Text = string.Concat(selectedKanjiBox.Text, Clipboard.GetText());
         e.SuppressKeyPress    = true;
         e.Handled             = true;
     }
     // +W? Give focus to the stroke count box.
     else if (e.Control && e.KeyCode == Keys.W)
     {
         strokeBox.Focus();
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // Pin the application.
     else if (e.Control && e.KeyCode == Keys.P)
     {
         this.TopMost       = this.TopMost == false;
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // F8 -- Open radicalNames.txt for reference/editing.
     else if (e.KeyCode == Keys.F8)
     {
         // THIS MAY HAVE ISSUES ON LINUX.
         NameToRadicalDictionary.ShowRadicalNamesFile();
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
     // F9 -- Reload radicalNames.txt
     else if (e.KeyCode == Keys.F9)
     {
         messageBox.Text    = NameToRadicalDictionary.Reload();
         e.SuppressKeyPress = true;
         e.Handled          = true;
     }
 }