Ejemplo n.º 1
0
        public string turnRuby(string input)
        {
            StringBuilder output = new StringBuilder();
            // Analyze the Japanese text according to the specified algorithm.
            IReadOnlyList <JapanesePhoneme> words = JapanesePhoneticAnalyzer.GetWords(input, false);

            foreach (JapanesePhoneme word in words)
            {
                //// Put each phrase on its own line.
                //if (output.Length != 0 && word.IsPhraseStart)
                //{
                //    output.AppendLine();
                //}



                // DisplayText is the display text of the word, which has same characters as the input of GetWords().
                // YomiText is the reading text of the word, as known as Yomi, which typically consists of Hiragana characters.
                // However, please note that the reading can contains some non-Hiragana characters for some display texts such as emoticons or symbols.
                if (StringHelper.isContainKanji(word.DisplayText))
                {
                    output.AppendFormat("<ruby>{0}<rt>{1}</rt></ruby>", word.DisplayText, word.YomiText);
                }
                else
                {
                    output.Append(word.DisplayText);
                }
            }

            // Display the result.
            string outputString = output.ToString();

            return(outputString);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// データベースレコードの作成
        /// </summary>
        public void Create()
        {
            var row = new DataBase.Tables.ItemSet {
                DirectoryPath = this.DirectoryPath,
                OrdinalRegex  = this.OrdinalRegex.Value
            };

            lock (this._database) {
                this._database.ItemSets.Add(row);
                this._database.SaveChanges();
            }

            this.TitleYomi.Value = JapanesePhoneticAnalyzer.GetWords(this.Title.Value).Select(x => x.YomiText).Aggregate((x, y) => x + y);
            this._itemSetId      = row.ItemSetId;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This is the click handler for the 'Start' button.
        /// </summary>
        /// <param name="sender">'Start' button</param>
        /// <param name="e">Event arguments</param>
        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            MainPage.Current.NotifyUser("Analyzing...", NotifyType.StatusMessage);
            string input    = this.InputTextBox.Text;
            bool   monoRuby = this.MonoRubyCheckBox.IsChecked != null && this.MonoRubyCheckBox.IsChecked.Value;
            string output   = String.Empty;

            // Split the Japanese text input in the text field into multiple words.
            // Based on the second boolean parameter of GetWords(), the unit of segmentation changes.
            // When the second parameter is true, each element in the result corresponds to one or more characters which have an atomic reading.
            // When the second parameter is false, each element in the result corresponds to a single Japanese word.
            IReadOnlyList <JapanesePhoneme> words = JapanesePhoneticAnalyzer.GetWords(input, monoRuby);

            foreach (JapanesePhoneme word in words)
            {
                // IsPhraseStart indicates whether the word is the first word of a segment or not.
                if (output != "" && word.IsPhraseStart)
                {
                    // Output a delimiter before each segment.
                    output += "/";
                }

                // DisplayText property has the display text of the word, which has same characters as the input of GetWords().
                // YomiText property has the reading text of the word, as known as Yomi, which basically consists of Hiragana characters.
                // However, please note that the reading can contains some non-Hiragana characters for some display texts such as emoticons or symbols.
                output += word.DisplayText + "(" + word.YomiText + ")";
            }

            // Display the result.
            this.OutputTextBox.Text = output;
            if (input != "" && output == "")
            {
                // If the result from GetWords() is empty but the input is not empty,
                // it means the given input is too long to analyze.
                MainPage.Current.NotifyUser("Failed to get words from the input text.  The input text is too long to analyze.", NotifyType.ErrorMessage);
            }
            else
            {
                // Otherwise, the analysis has done successfully.
                MainPage.Current.NotifyUser("Got words from the input text successfully.", NotifyType.StatusMessage);
            }
        }
        private void AnalyzeButton_Click(object sender, RoutedEventArgs e)
        {
            string        input  = this.InputTextBox.Text;
            StringBuilder output = new StringBuilder();

            // monoRuby = false means that each element in the result corresponds to a single Japanese word.
            // monoRuby = true means that each element in the result corresponds to one or more characters which are pronounced as a single unit.
            bool monoRuby = MonoRubyRadioButton.IsChecked == true;

            // Analyze the Japanese text according to the specified algorithm.
            // The maximum length of the input string is 100 characters.
            IReadOnlyList <JapanesePhoneme> words = JapanesePhoneticAnalyzer.GetWords(input, monoRuby);

            foreach (JapanesePhoneme word in words)
            {
                // Put each phrase on its own line.
                if (output.Length != 0 && word.IsPhraseStart)
                {
                    output.AppendLine();
                }
                // DisplayText is the display text of the word, which has same characters as the input of GetWords().
                // YomiText is the reading text of the word, as known as Yomi, which typically consists of Hiragana characters.
                // However, please note that the reading can contains some non-Hiragana characters for some display texts such as emoticons or symbols.
                output.AppendFormat("{0}({1})", word.DisplayText, word.YomiText);
            }

            // Display the result.
            string outputString = output.ToString();

            this.OutputTextBlock.Text = outputString;
            if (input != "" && words.Count == 0)
            {
                // If the result from GetWords() is empty but the input is not empty,
                // it means the given input is too long to analyze.
                MainPage.Current.NotifyUser("Failed to get words from the input text.  The input text is too long to analyze.", NotifyType.ErrorMessage);
            }
            else
            {
                // Otherwise, the analysis has been done successfully.
                MainPage.Current.NotifyUser("Got words from the input text successfully.", NotifyType.StatusMessage);
            }
        }
Ejemplo n.º 5
0
        private void PerformSpacing()
        {
            if (!IsInitialized)
            {
                return;
            }

            Int32 operationMode = modeComboBox.SelectedIndex;

            String inputText = inputTextBox.Text.Trim();
            // Perform very rigorous sentence splitter by splitting based on non-letter characters such as period and parentheses and etc.,
            // because JapanesePhoneticAnalyzer doesn't accept a sentence w/ length greater than 100.
            // Thus, a sentence may be split further into multiple chunks.
            List <String> wordChunks         = new List <String>();
            Int32         chunkStartingIndex = 0;

            for (Int32 i = 0; i < inputText.Length; ++i)
            {
                if (!Char.IsLetterOrDigit(inputText[i])) // Sentence split detected
                {
                    String newChunk = inputText.Substring(chunkStartingIndex, i - chunkStartingIndex + 1);
                    wordChunks.Add(newChunk);
                    chunkStartingIndex = i + 1;
                }
            }
            if (chunkStartingIndex < inputText.Length) // Clean up needed
            {
                String newChunk = inputText.Substring(chunkStartingIndex, inputText.Length - chunkStartingIndex);
                wordChunks.Add(newChunk);
            }

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("<meta charset=\"utf-8\"/>");
            foreach (String chunk in wordChunks)
            {
                var             phonemes        = JapanesePhoneticAnalyzer.GetWords(chunk, false).ToList();
                JapanesePhoneme previousPhoneme = null;
                foreach (var phoneme in phonemes)
                {
                    if (phoneme.IsPhraseStart)
                    {
                        if (Char.IsLetter(phoneme.DisplayText[0]) &&
                            Char.IsLetter(previousPhoneme == null? '-' : previousPhoneme.DisplayText.Last())) // Make sure we don't add spaces after non-letter characters.
                        {
                            stringBuilder.Append("<ruby> </ruby>");
                        }
                    }
                    previousPhoneme = phoneme;

                    switch (operationMode)
                    {
                    case 0: // Add Spaces Only
                        stringBuilder.Append($"<ruby><rb>{phoneme.DisplayText}</rb></ruby>");
                        break;

                    case 1: // Show Furigana
                        // Original text part
                        stringBuilder.Append($"<ruby><rb>{phoneme.DisplayText}</rb>");
                        // Furigana part
                        if (phoneme.DisplayText != phoneme.YomiText &&
                            !katakanaSet.Contains(phoneme.DisplayText[0]) && Char.IsLetter(phoneme.DisplayText[0])) // Furigana needs to be displayed
                        {
                            stringBuilder.Append($"<rt>{phoneme.YomiText}</rt>");
                        }
                        stringBuilder.Append("</ruby>");
                        break;

                    case 2: // Show in Hiragana
                        stringBuilder.Append($"<ruby><rb>{phoneme.YomiText}</rb></ruby>");
                        break;
                    }
                }
            }
            stringBuilder.Append("</p>");
            String resultString = stringBuilder.ToString();

            resultBrowser.DocumentText = resultString;
        }