コード例 #1
0
        public void Import(object importSettingsViewModel, Stream stream, CogProject project)
        {
            var reader = new CsvReader(new StreamReader(stream), ',');

            if (!SkipRows(reader, 5))
            {
                project.Meanings.Clear();
                project.Varieties.Clear();
                return;
            }

            var            varieties = new List <Variety>();
            var            meanings  = new Dictionary <string, Meaning>();
            IList <string> varietyRow;

            while (reader.ReadRow(out varietyRow))
            {
                if (string.IsNullOrEmpty(varietyRow[0]))
                {
                    break;
                }

                var variety = new Variety(varietyRow[0].Trim());
                if (!SkipRows(reader, 2))
                {
                    throw new ImportException("Metadata for a variety is incomplete.");
                }

                Meaning        curMeaning = null;
                IList <string> glossRow;
                while (reader.ReadRow(out glossRow) && glossRow.Any(s => !string.IsNullOrEmpty(s)))
                {
                    if (!string.IsNullOrEmpty(glossRow[0]))
                    {
                        string gloss = glossRow[0].Trim();
                        curMeaning = meanings.GetValue(gloss, () => new Meaning(gloss, null));
                    }
                    if (curMeaning == null)
                    {
                        throw new ImportException("A gloss is missing.");
                    }

                    string wordStr = glossRow[1].Trim();
                    if (!string.IsNullOrEmpty(wordStr))
                    {
                        variety.Words.Add(new Word(wordStr, curMeaning));
                    }
                }
                varieties.Add(variety);
            }

            project.Meanings.ReplaceAll(meanings.Values);
            project.Varieties.ReplaceAll(varieties);
        }
コード例 #2
0
        public void Import(object importSettingsViewModel, Stream stream, CogProject project)
        {
            var reader = new CsvReader(new StreamReader(stream), ',');
            if (!SkipRows(reader, 5))
            {
                project.Meanings.Clear();
                project.Varieties.Clear();
                return;
            }

            var varieties = new List<Variety>();
            var meanings = new Dictionary<string, Meaning>();
            IList<string> varietyRow;
            while (reader.ReadRow(out varietyRow))
            {
                if (string.IsNullOrEmpty(varietyRow[0]))
                    break;

                var variety = new Variety(varietyRow[0].Trim());
                if (!SkipRows(reader, 2))
                    throw new ImportException("Metadata for a variety is incomplete.");

                Meaning curMeaning = null;
                IList<string> glossRow;
                while (reader.ReadRow(out glossRow) && glossRow.Any(s => !string.IsNullOrEmpty(s)))
                {
                    if (!string.IsNullOrEmpty(glossRow[0]))
                    {
                        string gloss = glossRow[0].Trim();
                        curMeaning = meanings.GetValue(gloss, () => new Meaning(gloss, null));
                    }
                    if (curMeaning == null)
                        throw new ImportException("A gloss is missing.");

                    string wordStr = glossRow[1].Trim();
                    if (!string.IsNullOrEmpty(wordStr))
                        variety.Words.Add(new Word(wordStr, curMeaning));
                }
                varieties.Add(variety);
            }

            project.Meanings.ReplaceAll(meanings.Values);
            project.Varieties.ReplaceAll(varieties);
        }
コード例 #3
0
        private bool SkipRows(CsvReader reader, int numRows)
        {
            IList<string> row;
            int i = 0;
            while (reader.ReadRow(out row))
            {
                i++;
                if (i == numRows)
                    break;
            }

            return i == numRows;
        }
コード例 #4
0
        private bool SkipRows(CsvReader reader, int numRows)
        {
            IList <string> row;
            int            i = 0;

            while (reader.ReadRow(out row))
            {
                i++;
                if (i == numRows)
                {
                    break;
                }
            }

            return(i == numRows);
        }
コード例 #5
0
        public IEnumerable<Tuple<string, string>> Import(object importSettingsViewModel, Stream stream)
        {
            var mappings = new List<Tuple<string, string>>();
            var reader = new CsvReader(new StreamReader(stream), _delimiter);
            IList<string> mapping;
            while (reader.ReadRow(out mapping))
            {
                if (mapping.Count >= 2)
                {
                    string str1 = mapping[0].Trim();
                    string str2 = mapping[1].Trim();
                    if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
                        throw new ImportException("An empty segment is not allowed.");
                    mappings.Add(Tuple.Create(str1, str2));
                }
            }

            return mappings;
        }
コード例 #6
0
        public IEnumerable <Tuple <string, string> > Import(object importSettingsViewModel, Stream stream)
        {
            var            mappings = new List <Tuple <string, string> >();
            var            reader   = new CsvReader(new StreamReader(stream), _delimiter);
            IList <string> mapping;

            while (reader.ReadRow(out mapping))
            {
                if (mapping.Count >= 2)
                {
                    string str1 = mapping[0].Trim();
                    string str2 = mapping[1].Trim();
                    if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
                    {
                        throw new ImportException("An empty segment is not allowed.");
                    }
                    mappings.Add(Tuple.Create(str1, str2));
                }
            }

            return(mappings);
        }
コード例 #7
0
ファイル: TextWordListsImporter.cs プロジェクト: FieldDB/cog
        private void ImportVarietyRows(Stream stream, CogProject project, bool categoriesIncluded)
        {
            var            reader = new CsvReader(new StreamReader(stream), _delimiter);
            IList <string> glosses;

            if (!reader.ReadRow(out glosses))
            {
                project.Meanings.Clear();
                project.Varieties.Clear();
                return;
            }
            IList <string> categories = null;

            if (categoriesIncluded)
            {
                if (reader.ReadRow(out categories))
                {
                    if (categories.Count <= 1)
                    {
                        categories = null;
                    }
                    else if (categories.Count != glosses.Count)
                    {
                        throw new ImportException("A category is not specified for each gloss. Line: 2");
                    }
                }
                else
                {
                    throw new ImportException("Missing categories row. Line: 2");
                }
            }

            var blankColumns = new HashSet <int>();
            var meanings     = new Dictionary <string, Meaning>();

            for (int i = 1; i < glosses.Count; i++)
            {
                string gloss = glosses[i].Trim();
                if (string.IsNullOrEmpty(gloss))
                {
                    blankColumns.Add(i);
                    continue;
                }

                if (meanings.ContainsKey(gloss))
                {
                    throw new ImportException(string.Format("The gloss, \"{0}\", is not unique. Line: 1", gloss));
                }
                string category = null;
                if (categoriesIncluded && categories != null)
                {
                    category = categories[i].Trim();
                }
                meanings[gloss] = new Meaning(gloss, string.IsNullOrEmpty(category) ? null : category);
            }

            int line = categoriesIncluded ? 3 : 2;

            var            varieties = new Dictionary <string, Variety>();
            IList <string> varietyRow;

            while (reader.ReadRow(out varietyRow))
            {
                if (varietyRow.Count != glosses.Count)
                {
                    throw new ImportException(string.Format("Incorrect number of words specified for a variety. Line: {0}", line));
                }
                string  name    = varietyRow[0].Trim();
                Variety variety = null;
                if (!string.IsNullOrEmpty(name))
                {
                    if (varieties.ContainsKey(name))
                    {
                        throw new ImportException(string.Format("The variety name, \"{0}\", is not unique. Line: {1}", name, line));
                    }
                    variety         = new Variety(name);
                    varieties[name] = variety;
                }
                for (int j = 1; j < varietyRow.Count; j++)
                {
                    string wordStr = varietyRow[j].Trim();
                    if (!string.IsNullOrEmpty(wordStr))
                    {
                        if (variety == null)
                        {
                            throw new ImportException(string.Format("A blank variety name is not allowed. Line: {0}", line));
                        }
                        if (blankColumns.Contains(j))
                        {
                            throw new ImportException("A blank gloss is not allowed. Line: 1");
                        }
                        foreach (string w in wordStr.Split(',', '/'))
                        {
                            string str = w.Trim();
                            variety.Words.Add(new Word(str, meanings[glosses[j].Trim()]));
                        }
                    }
                }
                line++;
            }

            project.Meanings.ReplaceAll(meanings.Values);
            project.Varieties.ReplaceAll(varieties.Values);
        }
コード例 #8
0
        private void ImportGlossRows(Stream stream, CogProject project, bool categoriesIncluded)
        {
            var reader = new CsvReader(new StreamReader(stream), _delimiter);

            IList <string> varietyNames;

            if (!reader.ReadRow(out varietyNames))
            {
                project.Varieties.Clear();
                project.Meanings.Clear();
                return;
            }

            var blankColumns = new HashSet <int>();
            var varieties    = new Dictionary <string, Variety>();

            for (int i = (categoriesIncluded ? 2 : 1); i < varietyNames.Count; i++)
            {
                string name = varietyNames[i].Trim();
                if (string.IsNullOrEmpty(name))
                {
                    blankColumns.Add(i);
                    continue;
                }

                if (varieties.ContainsKey(name))
                {
                    throw new ImportException($"The variety name, \"{name}\", is not unique. Line: 1");
                }
                varieties[name] = new Variety(name);
            }

            int            line     = 2;
            var            meanings = new Dictionary <string, Meaning>();
            IList <string> glossRow;

            while (reader.ReadRow(out glossRow))
            {
                if (glossRow.Count != varietyNames.Count)
                {
                    throw new ImportException($"Incorrect number of words specified for a gloss. Line: {line}");
                }
                int     column  = 0;
                string  gloss   = glossRow[column++].Trim();
                Meaning meaning = null;
                if (!string.IsNullOrEmpty(gloss))
                {
                    if (meanings.ContainsKey(gloss))
                    {
                        throw new ImportException($"The gloss, \"{gloss}\", is not unique. Line: {line}");
                    }
                    string category = null;
                    if (categoriesIncluded)
                    {
                        if (glossRow.Count == 1)
                        {
                            throw new ImportException($"Missing categories column. Line: {line}");
                        }
                        category = glossRow[column++].Trim();
                    }
                    meaning         = new Meaning(gloss, string.IsNullOrEmpty(category) ? null : category);
                    meanings[gloss] = meaning;
                }
                for (int j = column; j < glossRow.Count; j++)
                {
                    string wordStr = glossRow[j].Trim();
                    if (!string.IsNullOrEmpty(wordStr))
                    {
                        if (meaning == null)
                        {
                            throw new ImportException($"A blank gloss is not allowed. Line: {line}");
                        }
                        if (blankColumns.Contains(j))
                        {
                            throw new ImportException("A blank variety name is not allowed. Line: 1");
                        }
                        foreach (string w in wordStr.Split(',', '/'))
                        {
                            string str = w.Trim();
                            varieties[varietyNames[j].Trim()].Words.Add(new Word(str, meaning));
                        }
                    }
                }
                line++;
            }

            project.Meanings.ReplaceAll(meanings.Values);
            project.Varieties.ReplaceAll(varieties.Values);
        }