Example #1
0
 private WordRecord GetWordRecordReal(string source, int st, int f2)
 {
     if (isHiragana(source[f2 - 1]) && f2 < source.Length && "ぁぃぅぇぉゃゅょ゜".IndexOf(source[f2]) >= 0)
         return null;
     string all = source.Substring(st, f2 - st);
     if (all.Length == 1 && "おかとなにねのはへやをがだでんもよぞ".IndexOf(all[0]) >= 0)
     {
         WordRecord res = new WordRecord();
         res.score = 1;
         return res;
     }
     if (all.Length <= 8)
     {
         string rep = all;
         foreach (Replacement rr in options.replacements)
         {
             if (rr.oldText == all)
             {
                 rep = rr.newText;
                 break;
             }
         }
         if (rep != all)
         {
             bool allABC = true;
             foreach (char ch in rep)
             {
                 var cat = char.GetUnicodeCategory(ch);
                 if (ch != '-' && cat != UnicodeCategory.SpaceSeparator && cat != UnicodeCategory.UppercaseLetter && cat != UnicodeCategory.LowercaseLetter)
                 {
                     allABC = false;
                     break;
                 }
             }
             if (allABC)
             {
                 WordRecord res = new WordRecord();
                 res.key = all;
                 res.reading = rep;
                 res.dict_key = all;
                 res.dict_reading = "-";
                 res.meaning = rep;
                 res.score = 1000;
                 return res;
             }
         }
     }
     /*if (all.Length <= 3 && all[0] == 'は')
     {
         if (st > 0 && char.IsLetter(source[st - 1]))
             continue;
     }*/
     EdictEntry[] ex = Edict.instance.SearchExact(all, null);
     if (ex.Length > 0 && ex[0].meaning.Length > 0)
     {
         WordRecord res = new WordRecord();
         res.key = all;
         res.reading = ex[0].reading;
         res.dict_key = string.Join("#", (from ed in ex select ed.key));
         res.dict_reading = string.Join("#", (from ed in ex select ed.reading));
         res.meaning = string.Join("#", (from ed in ex select string.Join("; ", ed.meaning)));
         res.score = res.key.Length * res.key.Length;
         if (res.key != res.dict_key)
             res.score -= 1;
         if (!Global.options.includeOkurigana)
         {
             if (hasKanji(res.key))
             {
                 int i = 1;
                 while (res.key.Length - i >= 0 && res.reading.Length - i >= 0 && res.key[res.key.Length - i] == res.reading[res.reading.Length - i])
                     ++i;
                 i -= 1;
                 if (i > 0)
                 {
                     res.key = res.key.Substring(0, res.key.Length - i);
                     res.reading = res.reading.Substring(0, res.reading.Length - i);
                 }
             }
         }
         return res;
     }
     EdictEntry[] entries;
     string ending;
     string stem;
     string orig;
     bool found = Inflect.FindInflected(all, out entries, out stem, out ending, out orig);
     if (found)
     {
         var tmp = new List<WordRecord>();
         foreach (var entry in entries)
         {
             WordRecord res = new WordRecord();
             string key = stem + ending;
             res.key = key;
             int len = entry.reading.Length - orig.Length;
             string reading;
             if (len >= 0)
                 reading = entry.reading.Substring(0, len) + ending;
             else
                 reading = "";
             res.reading = reading;
             res.dict_key = entry.key;
             res.dict_reading = entry.reading;
             res.meaning = string.Join("; ", entry.meaning);
             res.score = res.key.Length * res.key.Length;
             if (res.key != res.dict_key)
                 res.score -= 1;
             if (!Global.options.includeOkurigana)
             {
                 res.key = stem;
                 res.reading = entry.reading.Substring(0, len);
             }
             tmp.Add(res);
         }
         WordRecord allres = new WordRecord();
         allres.key = tmp[0].key;
         allres.reading = tmp[0].reading;
         allres.dict_key = string.Join("#", (from ed in tmp select ed.dict_key));
         allres.dict_reading = string.Join("#", (from ed in tmp select ed.dict_reading));
         allres.meaning = string.Join("#", (from ed in tmp select ed.meaning));
         allres.score = tmp[0].score;
         return allres;
     }
     return null;
 }
Example #2
0
        internal void Convert(SqlCommand cmd, GAFAWSData gData, Dictionary <string, FwMsa> prefixes, Dictionary <string, List <FwMsa> > stems, Dictionary <string, FwMsa> suffixes)
        {
            if (!CanConvert)
            {
                return;
            }

            WordRecord wr = new WordRecord();
            // Deal with prefixes, if any.
            int startStemOrd = 0;

            foreach (KeyValuePair <int, FwMorphBundle> kvp in m_morphBundles)
            {
                FwMorphBundle mb     = kvp.Value;
                string        msaKey = mb.GetMsaKey(cmd);
                if (mb.MSA.Class == 5001 || mb.MSA.Class == 5031 || mb.MSA.Class == 5032 || mb.MSA.Class == 5117)                 // What about 5117-MoUnclassifiedAffixMsa?
                {
                    // stem or derivational prefix, so bail out of this loop.
                    startStemOrd = kvp.Key;
                    break;
                }

                // Add prefix, if not already present.
                if (wr.Prefixes == null)
                {
                    wr.Prefixes = new List <Affix>();
                }
                if (!prefixes.ContainsKey(msaKey))
                {
                    prefixes.Add(msaKey, mb.MSA);
                    gData.Morphemes.Add(new Morpheme(MorphemeType.prefix, msaKey));
                }
                Affix afx = new Affix();
                afx.MIDREF = msaKey;
                wr.Prefixes.Add(afx);
            }

            // Deal with suffixes, if any.
            // Work through the suffixes from the end of the word.
            // We stop when we hit the stem or a derivational suffix.
            int endStemOrd = 0;

            for (int i = m_morphBundles.Count; i > 0; --i)
            {
                FwMorphBundle mb     = m_morphBundles[i];
                string        msaKey = mb.GetMsaKey(cmd);
                if (mb.MSA.Class == 5001 || mb.MSA.Class == 5031 || mb.MSA.Class == 5032 || mb.MSA.Class == 5117)                 // What about 5117-MoUnclassifiedAffixMsa?
                {
                    // stem or derivational suffix, so bail out of this loop.
                    endStemOrd = i;
                    break;
                }

                // Add suffix, if not already present.
                if (wr.Suffixes == null)
                {
                    wr.Suffixes = new List <Affix>();
                }
                if (!suffixes.ContainsKey(msaKey))
                {
                    suffixes.Add(msaKey, mb.MSA);
                    gData.Morphemes.Add(new Morpheme(MorphemeType.suffix, msaKey));
                }
                Affix afx = new Affix();
                afx.MIDREF = msaKey;
                wr.Suffixes.Insert(0, afx);
            }

            // Deal with stem.
            List <FwMsa> localStems = new List <FwMsa>();
            string       sStem      = "";

            foreach (KeyValuePair <int, FwMorphBundle> kvp in m_morphBundles)
            {
                FwMorphBundle mb         = kvp.Value;
                int           currentOrd = kvp.Key;
                if (currentOrd >= startStemOrd && currentOrd <= endStemOrd)
                {
                    string msaKey = mb.GetMsaKey(cmd);
                    string spacer = (currentOrd == 1) ? "" : " ";
                    sStem += spacer + msaKey;
                }
            }
            if (!stems.ContainsKey(sStem))
            {
                stems.Add(sStem, localStems);
                gData.Morphemes.Add(new Morpheme(MorphemeType.stem, sStem));
            }

            Stem stem = new Stem();

            stem.MIDREF = sStem;
            wr.Stem     = stem;

            // Add wr.
            gData.WordRecords.Add(wr);
        }
        /// <summary>
        /// Do whatever it takes to convert the input this processor knows about.
        /// </summary>
        public void Convert()
        {
            string outputPathname = null;

            OpenFileDialog openFileDlg = new OpenFileDialog();

            openFileDlg.InitialDirectory = "c:\\";
            openFileDlg.Filter           = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDlg.FilterIndex      = 2;
            openFileDlg.Multiselect      = false;

            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                string sourcePathname = openFileDlg.FileName;
                if (File.Exists(sourcePathname))
                {
                    // Try to convert it.
                    using (StreamReader reader = new StreamReader(sourcePathname))
                    {
                        string line = reader.ReadLine();
                        Dictionary <string, bool> dictPrefixes = new Dictionary <string, bool>();
                        Dictionary <string, bool> dictStems    = new Dictionary <string, bool>();
                        Dictionary <string, bool> dictSuffixes = new Dictionary <string, bool>();
                        while (line != null)
                        {
                            line = line.Trim();
                            if (line != String.Empty)
                            {
                                int openAngleLocation = line.IndexOf("<", 0);
                                if (openAngleLocation < 0)
                                {
                                    continue;
                                }
                                int closeAngleLocation = line.IndexOf(">", openAngleLocation + 1);
                                if (closeAngleLocation < 0)
                                {
                                    continue;
                                }
                                WordRecord wrdRec = new WordRecord();
                                m_gd.WordRecords.Add(wrdRec);

                                // Handle prefixes, if any.
                                string prefixes = null;
                                if (openAngleLocation > 0)
                                {
                                    prefixes = line.Substring(0, openAngleLocation);
                                }
                                if (prefixes != null)
                                {
                                    if (wrdRec.Prefixes == null)
                                    {
                                        wrdRec.Prefixes = new List <Affix>();
                                    }
                                    foreach (string prefix in prefixes.Split('-'))
                                    {
                                        if (prefix != null && prefix != "")
                                        {
                                            Affix afx = new Affix();
                                            afx.MIDREF = prefix;
                                            wrdRec.Prefixes.Add(afx);
                                            if (!dictPrefixes.ContainsKey(prefix))
                                            {
                                                m_gd.Morphemes.Add(new Morpheme(MorphemeType.prefix, prefix));
                                                dictPrefixes.Add(prefix, true);
                                            }
                                        }
                                    }
                                }

                                // Handle stem.
                                string sStem = null;
                                // Stem has content, so use it.
                                sStem = line.Substring(openAngleLocation + 1, closeAngleLocation - openAngleLocation - 1);
                                if (sStem.Length == 0)
                                {
                                    sStem = "stem";
                                }
                                Stem stem = new Stem();
                                stem.MIDREF = sStem;
                                wrdRec.Stem = stem;
                                if (!dictStems.ContainsKey(sStem))
                                {
                                    m_gd.Morphemes.Add(new Morpheme(MorphemeType.stem, sStem));
                                    dictStems.Add(sStem, true);
                                }

                                // Handle suffixes, if any.
                                string suffixes = null;
                                if (line.Length > closeAngleLocation + 2)
                                {
                                    suffixes = line.Substring(closeAngleLocation + 1);
                                }
                                if (suffixes != null)
                                {
                                    if (wrdRec.Suffixes == null)
                                    {
                                        wrdRec.Suffixes = new List <Affix>();
                                    }
                                    foreach (string suffix in suffixes.Split('-'))
                                    {
                                        if (suffix != null && suffix != "")
                                        {
                                            Affix afx = new Affix();
                                            afx.MIDREF = suffix;
                                            wrdRec.Suffixes.Add(afx);
                                            if (!dictSuffixes.ContainsKey(suffix))
                                            {
                                                m_gd.Morphemes.Add(new Morpheme(MorphemeType.suffix, suffix));
                                                dictSuffixes.Add(suffix, true);
                                            }
                                        }
                                    }
                                }
                            }
                            line = reader.ReadLine();
                        }

                        // Main processing.
                        PositionAnalyzer anal = new PositionAnalyzer();
                        anal.Process(m_gd);

                        // Do any post-analysis processing here, if needed.
                        // End of any optional post-processing.

                        // Save, so it can be transformed.
                        outputPathname = GetOutputPathname(sourcePathname);
                        m_gd.SaveData(outputPathname);

                        // Transform.
                        XslCompiledTransform trans = new XslCompiledTransform();
                        try
                        {
                            trans.Load(XSLPathname);
                        }
                        catch
                        {
                            MessageBox.Show("Could not load the XSL file.", "Information");
                            return;
                        }

                        string htmlOutput = Path.GetTempFileName() + ".html";
                        try
                        {
                            trans.Transform(outputPathname, htmlOutput);
                        }
                        catch
                        {
                            MessageBox.Show("Could not transform the input file.", "Information");
                            return;
                        }
                        finally
                        {
                            if (outputPathname != null && File.Exists(outputPathname))
                            {
                                File.Delete(outputPathname);
                            }
                        }
                        Process.Start(htmlOutput);
                    }                     // end 'using'
                }
            }

            // Reset m_gd, in case it gets called for another file.
            m_gd = GAFAWSData.Create();
        }
Example #4
0
 // Use this for initialization
 void Start()
 {
     btn = gameObject.GetComponent<Button> ();
     wordRecord = GameObject.FindGameObjectWithTag ("dog").GetComponent<DogController> ().word.GetComponent<WordRecord> ();
 }
Example #5
0
        /// -----------------------------------------------------------------------------------
        /// <summary>
        /// Convert the analysis and its morphemes.
        /// </summary>
        /// -----------------------------------------------------------------------------------
        internal void Convert()
        {
            int i = 0;

            if (m_stem == null || (m_prefixes == null && m_suffixes == null))
            {
                return;                 // Don't convert a failure or no affixes.
            }
            // Category Filter
            if ((PartsOfSpeech != null) && (PartsOfSpeech.Count != 0))
            {
                // \cat?
                string[] catCats = { m_wordCategory };
                if (m_wordCategory != null)
                {
                    if (ContainsCat(catCats))
                    {
                        goto label1;
                    }
                    else
                    {
                        return;
                    }
                }
                // \a?
                string[] stemElements = m_sstem.Split(' ');
                string[] stemCats     = new string[(stemElements.Length - 2) / 2];
                for (i = 0; i < (stemElements.Length - 2) / 2; ++i)
                {
                    stemCats[i] = new string(stemElements[(i * 2) + 1].ToCharArray());
                }

                if (ContainsCat(stemCats))
                {
                    goto label1;
                }

                return;
            }

            label1 : WordRecord wr = new WordRecord();
            s_gd.WordRecords.Add(wr);
            wr.WRID = "WR" + s_idx++;
            if (m_prefixes != null)
            {
                wr.Prefixes = new List <Affix>();
            }
            if (m_suffixes != null)
            {
                wr.Suffixes = new List <Affix>();
            }

            if ((m_originalForm != null) || (m_wordCategory != null))
            {
                string xml = "<ANAInfo";
                if (m_originalForm != null)
                {
                    xml += " form=\'" + m_originalForm + "\'";
                }
                if (m_wordCategory != null)
                {
                    xml += " category=\'" + m_wordCategory + "\'";
                }
                xml     += " />";
                wr.Other = new Other(xml);
            }

            for (i = 0; m_prefixes != null && i < m_prefixes.Count; ++i)
            {
                m_prefixes[i].Convert();
            }
            m_stem.Convert();
            for (i = 0; m_suffixes != null && i < m_suffixes.Count; ++i)
            {
                m_suffixes[i].Convert();
            }
        }