Beispiel #1
0
        /// <summary>
        /// 単語登録
        /// </summary>
        /// <param name="glossaryEntry">単語エントリ</param>
        /// <returns>処理結果</returns>
        public bool AddEntry(PfkGlossaryEntry glossaryEntry)
        {
            var key = this.MakeKey(glossaryEntry.Prefix, glossaryEntry.OriginalText);

            if (this.Items.ContainsKey(key))
            {
                var msg = $"Duplicate key({key}).";
                Console.WriteLine(msg);
                return(false);
            }
            else
            {
                this.Items.Add(key, glossaryEntry);
                return(true);
            }
        }
Beispiel #2
0
        public static PfkGlossaryInfo LoadFromCsv(string path, Encoding enc = null)
        {
            if (enc == null)
            {
                enc = Encoding.UTF8;
            }

            var fileID = Path.GetFileNameWithoutExtension(path);

            using (var reader = new StreamReader(path, enc))
            {
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    //// 区切り文字
                    csv.Configuration.Delimiter = ",";
                    //// ヘッダーの有無
                    csv.Configuration.HasHeaderRecord = true;
                    //// CSVファイルに合ったマッピングルールを登録
                    csv.Configuration.RegisterClassMap <CsvMapper>();
                    //// データを読み出し
                    var records = csv.GetRecords <FlashCardSheet>();

                    var             errorCount = 0;
                    PfkGlossaryInfo sheet      = new PfkGlossaryInfo();
                    foreach (var record in records)
                    {
                        PfkGlossaryEntry.NConversionType conversionType = PfkGlossaryEntry.NConversionType.None;
                        switch (record.ConversionType)
                        {
                        case "":
                            conversionType = PfkGlossaryEntry.NConversionType.None;
                            break;

                        case "#":
                            conversionType = PfkGlossaryEntry.NConversionType.AlwaysTranslate;
                            break;

                        case "$":
                            conversionType = PfkGlossaryEntry.NConversionType.NounTranslate;
                            break;

                        default:
                            throw new Exception($"Unknown ConversionType({record.ConversionType}).");
                        }

                        PfkGlossaryEntry entry = new PfkGlossaryEntry(conversionType, record.Prefix, record.OriginalText, record.TranslatedText);
                        if (!sheet.AddEntry(entry))
                        {
                            errorCount++;
                        }
                    }

                    if (errorCount > 0)
                    {
                        var msg = $"Duplicated key. errors({errorCount})";
                        Console.WriteLine("*****************************");
                        Console.WriteLine(msg);
                        Console.WriteLine("*****************************");
                        throw new Exception(msg);
                    }

                    return(sheet);
                }
            }
        }