Ejemplo n.º 1
0
        private void saveWord(WordParseResult wordParseResult)
        {
            EdictEntry entry   = wordParseResult.getSelectedEntry();
            string     kanji   = string.Join(",", (from k in entry.kanji select k.text));
            string     kana    = string.Join(",", (from k in entry.kana select k.text));
            string     meaning = string.Join("/", (from m in entry.sense select m.glossary.First()).Take(3));
            string     fn      = Settings.app.SaveWordPath;

            if (File.Exists(fn))
            {
                foreach (string s in File.ReadAllLines(fn))
                {
                    if (kanji != "")
                    {
                        if (s.StartsWith(kanji))
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (s.StartsWith("\t" + kana))
                        {
                            return;
                        }
                    }
                }
            }
            File.AppendAllText(fn, kanji + "\t" + kana + "\t" + meaning + "\n");
        }
Ejemplo n.º 2
0
 private void saveWord(WordParseResult wordParseResult)
 {
     EdictEntry entry = wordParseResult.getSelectedEntry();
     string kanji = string.Join(",", (from k in entry.kanji select k.text));
     string kana = string.Join(",", (from k in entry.kana select k.text));
     string meaning = string.Join("/", (from m in entry.sense select m.glossary.First()).Take(3));
     string fn = Settings.app.SaveWordPath;
     if (File.Exists(fn)) {
         foreach (string s in File.ReadAllLines(fn)) {
             if (kanji != "") {
                 if (s.StartsWith(kanji)) {
                     return;
                 }
             } else {
                 if (s.StartsWith("\t" + kana)) {
                     return;
                 }
             }
         }
     }
     File.AppendAllText(fn, kanji + "\t" + kana + "\t" + meaning + "\n");
 }
Ejemplo n.º 3
0
        private void exportWord(WordParseResult wordParseResult)
        {
            EdictEntry entry = wordParseResult.getSelectedEntry();

            var kanjiUsed = new HashSet <char>(wordParseResult.asText().Where(FuriganaUtils.isKanji));
            var indices   =
                Enumerable.Zip(entry.kanji, Enumerable.Range(0, entry.kanji.Count), (e, i) => new { entry = e, index = i })
                .Where(x => new HashSet <char>(x.entry.text.Where(FuriganaUtils.isKanji)).SetEquals(kanjiUsed))
                .Select(x => x.index);

            if (indices.Count() == 0)
            {
                indices = Enumerable.Range(0, entry.kanji.Count);
            }

            string formatted = Regex.Replace(Settings.app.ankiSaveFormat, "\\$.", f => {
                switch (f.Value)
                {
                case "$t": return("\t");

                case "$d":     // Dictionary form
                    if (kanjiUsed.Count() == 0)
                    {
                        return(string.Join(", ", entry.kana.Select(k => k.text)));
                    }
                    else
                    {
                        return(string.Join(", ", indices.Select(i => entry.kanji.ElementAt(i).text)));
                    }

                case "$r":     // Reading
                    return(string.Join(", ", indices.Select(i => entry.kana.ElementAt(i).text)));

                case "$n":     // Definition
                    return(string.Join(";<br />", entry.sense.Select(m => string.Join(", ", m.glossary))));

                case "$s":     // Sentence
                    return(Regex.Replace(lastParseResult.asText(), "\\n|\\r|\\r\\n|\\n\\r", "<br />"));

                case "$f":     // Furigana
                    if (entry.kanji.Count == 0)
                    {
                        return(string.Join(", ", indices.Select(i => entry.kana.ElementAt(i).text)));
                    }
                    else
                    {
                        string kana = entry.kana.First().text;
                        return(string.Join(", ", indices.Select(i => FuriganaUtils.generateFurigana(entry.kanji.ElementAt(i).text, kana))));
                    }

                case "$i":     // Title
                    return(Text);

                default: return(f.Value);
                }
            });

            string fn         = "anki-rt.txt"; // TODO: Anki realtime export file in settings/temp folder
            string fieldNames = Settings.app.ankiFieldNames.Replace(' ', '\t');

            File.WriteAllText(fn, $"add\t1\n{fieldNames}\n\n{formatted}");
            using (UdpClient client = new UdpClient("127.0.0.1", ANKI_REALTIME_IMPORT_PORT)) {
                string fullPath = Path.GetFullPath(fn);
                byte[] bytes    = System.Text.Encoding.UTF8.GetBytes(fullPath);
                client.Send(bytes, bytes.Length);
            }
        }
Ejemplo n.º 4
0
        internal string replaceNames(ParseResult parseData)
        {
            StringBuilder             sb = new StringBuilder();
            IEnumerable <ParseResult> parts;

            if (parseData.type == ParseResult.ParseResultType.COMPLEX)
            {
                parts = (parseData as ComplexParseResult).parts;
            }
            else
            {
                parts = Enumerable.Repeat(parseData, 1);
            }
            bool prevName = false;

            foreach (var part in parts)
            {
                if (part.type == ParseResult.ParseResultType.UNPARSED)
                {
                    prevName = false;
                    sb.Append(part.asText());
                }
                else if (part.type == ParseResult.ParseResultType.WORD)
                {
                    WordParseResult wp            = part as WordParseResult;
                    bool            found         = false;
                    bool            isName        = wp.isName();
                    bool            isReplacement = wp.isReplacement();
                    if (isName || isReplacement)
                    {
                        var             entry = wp.getSelectedEntry();
                        DictionarySense sense = entry.sense.FirstOrDefault();
                        if (sense != null && sense.glossary.Count > 0)
                        {
                            if (prevName)
                            {
                                sb.Append(' ');
                            }
                            sb.Append(sense.glossary.First());
                            found = true;
                        }
                        else
                        {
                            if (entry.kana.Count > 0)
                            {
                                if (prevName)
                                {
                                    sb.Append(' ');
                                }
                                sb.Append(entry.kana.First().text);
                                found = true;
                            }
                        }
                    }
                    else if (prevName)
                    {
                        foreach (EdictEntry entry in wp.getEntries())
                        {
                            if (nameSuffixes.ContainsKey(part.asText()))
                            {
                                string reading = nameSuffixes[part.asText()];
                                sb.Append(reading);
                                found = true;
                                break;
                            }
                            else if (nameSuffixes.ContainsKey(entry.getText()))
                            {
                                string reading = nameSuffixes[entry.getText()];
                                sb.Append(reading);
                                found = true;
                                break;
                            }
                        }
                    }
                    if (found)
                    {
                        prevName = isName || prevName;
                    }
                    else
                    {
                        prevName = false;
                        sb.Append(part.asText());
                    }
                }
            }
            return(sb.ToString());
        }