Exemplo n.º 1
0
        public static CedictEntry BuildEntry(string simp, string trad, string pinyin, string trg)
        {
            // Prepare pinyin as list of proper syllables
            List <PinyinSyllable> pyList = new List <PinyinSyllable>();

            string[] pyRawArr = pinyin.Split(' ');
            foreach (string pyRaw in pyRawArr)
            {
                PinyinSyllable ps = PinyinSyllable.FromDisplayString(pyRaw);
                if (ps == null)
                {
                    ps = new PinyinSyllable(pyRaw, -1);
                }
                pyList.Add(ps);
            }

            // Build TRG entry in "canonical" form; parse; render
            trg = trg.Replace("\r\n", "\n");
            string[] senses = trg.Split('\n');
            string   can    = trad + " " + simp + " [";

            for (int i = 0; i != pyList.Count; ++i)
            {
                if (i != 0)
                {
                    can += " ";
                }
                can += pyList[i].GetDisplayString(false);
            }
            can += "] /";
            foreach (string str in senses)
            {
                can += str.Replace('/', '\\') + "/";
            }
            CedictParser parser = new CedictParser();

            return(parser.ParseEntry(can, 0, null));
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        public override void Process()
        {
            string simp = Req.Params["simp"];

            if (simp == null)
            {
                throw new ApiException(400, "Missing 'simp' parameter.");
            }
            string trad = Req.Params["trad"];

            if (trad == null)
            {
                throw new ApiException(400, "Missing 'trad' parameter.");
            }
            string pinyin = Req.Params["pinyin"];

            if (pinyin == null)
            {
                throw new ApiException(400, "Missing 'pinyin' parameter.");
            }

            Result res = new Result();

            res.Passed = true;

            // DBG
            if (simp == "大家" || simp == "污染")
            {
                res.Passed = false;
            }

            // Prepare pinyin as list of proper syllables
            List <PinyinSyllable> pyList = new List <PinyinSyllable>();

            string[] pyRawArr = pinyin.Split(' ');
            foreach (string pyRaw in pyRawArr)
            {
                pyList.Add(PinyinSyllable.FromDisplayString(pyRaw));
            }

            // Return all entries, CEDICT and HanDeDict, rendered as HTML
            CedictEntry[] ced, hdd;
            Global.HWInfo.GetEntries(simp, out ced, out hdd);
            StringBuilder sb = new StringBuilder();

            using (HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb)))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, "newEntryRefCED");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                foreach (CedictEntry entry in ced)
                {
                    EntryRenderer er = new EntryRenderer(entry, trad, pyList);
                    er.Render(writer);
                }
                writer.RenderEndTag();
                writer.AddAttribute(HtmlTextWriterAttribute.Id, "newEntryRefHDD");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                foreach (CedictEntry entry in hdd)
                {
                    EntryRenderer er = new EntryRenderer(entry, trad, pyList);
                    er.Render(writer);
                }
                writer.RenderEndTag();
            }
            res.RefEntries = sb.ToString();

            // Tell our caller
            Res = res;
        }
Exemplo n.º 3
0
        private UniHanziInfo getInfo(char c, CharInfo ci)
        {
            bool        canBeSimp    = false;
            List <char> tradVariants = new List <char>();

            // Character can be used as simplified or not
            // And its traditional variants
            // As per http://www.unicode.org/reports/tr38/index.html#SCTC from Unihan report
            // 1: Simp and trad forms identical
            if (ci.TradVars == null && ci.SimpVars == null)
            {
                canBeSimp = true;
                tradVariants.Add(c);
            }
            // 2: Only trad
            else if (ci.TradVars == null && ci.SimpVars != null)
            {
                canBeSimp = false;
                tradVariants.Add(c);
            }
            // 3: Only simp
            else if (ci.TradVars != null && ci.SimpVars == null)
            {
                canBeSimp = true;
                tradVariants.AddRange(ci.TradVars);
            }
            else
            {
                canBeSimp = true;
                // 4/1: Both; may remain or get mapped in traditional
                if (ci.TradVars.Contains(c))
                {
                    tradVariants.AddRange(ci.TradVars);
                }
                // 4/2: Both; different meaning
                else
                {
                    tradVariants.AddRange(ci.TradVars);
                }
            }

            List <string> pinyin = new List <string>();

            // Pinyin reading: use Mandarin only if no other source available
            // Otherwise, combine ranking of sources
            if (ci.Pinlu == null && ci.Pinyin == null && ci.XHC == null)
            {
                pinyin.Add(ci.Mandarin);
            }
            else
            {
                int max = 0;
                if (ci.Pinlu != null)
                {
                    max = ci.Pinlu.Length;
                }
                if (ci.Pinyin != null && max < ci.Pinyin.Length)
                {
                    max = ci.Pinyin.Length;
                }
                if (ci.XHC != null && max < ci.XHC.Length)
                {
                    max = ci.XHC.Length;
                }
                Dictionary <string, PyCalc> cnts = new Dictionary <string, PyCalc>();
                if (ci.Pinlu != null)
                {
                    for (int i = 0; i != ci.Pinlu.Length; ++i)
                    {
                        PyCalc pyCalc;
                        if (cnts.ContainsKey(ci.Pinlu[i]))
                        {
                            pyCalc = cnts[ci.Pinlu[i]];
                        }
                        else
                        {
                            pyCalc = new PyCalc(); pyCalc.Py = ci.Pinlu[i]; cnts[ci.Pinlu[i]] = pyCalc;
                        }
                        pyCalc.Cnt++;
                        pyCalc.Sum += max - i;
                    }
                }
                if (ci.Pinyin != null)
                {
                    for (int i = 0; i != ci.Pinyin.Length; ++i)
                    {
                        PyCalc pyCalc;
                        if (cnts.ContainsKey(ci.Pinyin[i]))
                        {
                            pyCalc = cnts[ci.Pinyin[i]];
                        }
                        else
                        {
                            pyCalc = new PyCalc(); pyCalc.Py = ci.Pinyin[i]; cnts[ci.Pinyin[i]] = pyCalc;
                        }
                        pyCalc.Cnt++;
                        pyCalc.Sum += max - i;
                    }
                }
                if (ci.XHC != null)
                {
                    for (int i = 0; i != ci.XHC.Length; ++i)
                    {
                        PyCalc pyCalc;
                        if (cnts.ContainsKey(ci.XHC[i]))
                        {
                            pyCalc = cnts[ci.XHC[i]];
                        }
                        else
                        {
                            pyCalc = new PyCalc(); pyCalc.Py = ci.XHC[i]; cnts[ci.XHC[i]] = pyCalc;
                        }
                        pyCalc.Cnt++;
                        pyCalc.Sum += max - i;
                    }
                }
                List <PyCalc> lst = new List <PyCalc>();
                lst.AddRange(cnts.Values);
                lst.Sort((x, y) => y.Sum.CompareTo(x.Sum));
                foreach (var x in lst)
                {
                    pinyin.Add(x.Py);
                }
            }

            // Convert to typed Pinyin syllables
            PinyinSyllable[] sylls = new PinyinSyllable[pinyin.Count];
            for (int i = 0; i != pinyin.Count; ++i)
            {
                sylls[i] = PinyinSyllable.FromDisplayString(pinyin[i]);
            }

            // Done.
            return(new UniHanziInfo(canBeSimp, tradVariants.ToArray(), sylls));
        }
Exemplo n.º 4
0
        public IActionResult VerifyHead([FromQuery] string lang, [FromQuery] string simp, [FromQuery] string trad, [FromQuery] string pinyin)
        {
            if (simp == null)
            {
                return(StatusCode(400, "Missing 'simp' parameter."));
            }
            if (trad == null)
            {
                return(StatusCode(400, "Missing 'trad' parameter."));
            }
            if (pinyin == null)
            {
                return(StatusCode(400, "Missing 'pinyin' parameter."));
            }

            NewEntryVerifyHeadResult res = new NewEntryVerifyHeadResult();
            StringBuilder            sb  = new StringBuilder();

            // Prepare pinyin as list of proper syllables
            List <PinyinSyllable> pyList = new List <PinyinSyllable>();

            string[] pyRawArr = pinyin.Split(' ');
            foreach (string pyRaw in pyRawArr)
            {
                PinyinSyllable syll = PinyinSyllable.FromDisplayString(pyRaw);
                if (syll == null)
                {
                    syll = new PinyinSyllable(pyRaw, -1);
                }
                pyList.Add(syll);
            }
            string pyInOne = "";

            foreach (var syll in pyList)
            {
                if (pyInOne != "")
                {
                    pyInOne += " ";
                }
                pyInOne += syll.GetDisplayString(false);
            }

            // Is this a dupe?
            string      head          = trad + " " + simp + " [" + pyInOne + "]";
            CedictEntry existingEntry = SqlDict.GetEntryByHead(head);

            if (existingEntry != null)
            {
                res.Duplicate       = true;
                res.ExistingEntryId = EntryId.IdToString(existingEntry.StableId);
                EntryRenderer er = new EntryRenderer(lang, existingEntry, true);
                er.Render(sb, null);
                res.ExistingEntry = sb.ToString();
                return(new ObjectResult(res));
            }

            // OK, we're good.
            res.Duplicate = false;

            // Return all entries, CEDICT and HanDeDict, rendered as HTML
            CedictEntry[] ced, hdd;
            langRepo.GetEntries(simp, out ced, out hdd);
            sb.Append("<div id='newEntryRefCED'>");
            foreach (CedictEntry entry in ced)
            {
                EntryRenderer er = new EntryRenderer(lang, entry, trad, pyList);
                er.Render(sb, null);
            }
            sb.Append("</div>");
            sb.Append("<div id='newEntryRefHDD'>");
            foreach (CedictEntry entry in hdd)
            {
                EntryRenderer er = new EntryRenderer(lang, entry, trad, pyList);
                er.Render(sb, null);
            }
            sb.Append("</div>");
            res.RefEntries = sb.ToString();

            // Tell our caller
            return(new ObjectResult(res));
        }