示例#1
0
        static private SemanticDomainQuestionsCollection PopulateSemanticDomainsQuestionsFromXml(XmlReader fileReader)
        {
            if (!(fileReader.Name == "semantic-domain-questions"))
            {
                throw new InvalidOperationException(String.Format("Node is {0} but we expected 'semantic-domain-questions' node.", fileReader.Name));
            }

            SemanticDomainQuestionsCollection allQuestions = new SemanticDomainQuestionsCollection();

            string type = fileReader.GetAttribute("semantic-domain-type");

            if (type == "DDP4")
            {
                allQuestions.Version = SemanticDomainQuestionsCollection.SemanticDomainsVersion.DDP4;
            }
            else if (type == "DDP1")
            {
                allQuestions.Version = SemanticDomainQuestionsCollection.SemanticDomainsVersion.DDP1;
            }
            else
            {
                throw new ArgumentOutOfRangeException(String.Format("There is no type for {0}", type));
            }

            allQuestions.WritingSystemId = fileReader.GetAttribute("lang");

            if (fileReader.IsEmptyElement)
            {
                return(allQuestions);
            }

            while (fileReader.Read())
            {
                bool readerHasReachedEndOfSemanticDomainsQuestions =
                    fileReader.NodeType == XmlNodeType.EndElement && fileReader.Name == "semantic-domain-questions";

                if (readerHasReachedEndOfSemanticDomainsQuestions)
                {
                    break;
                }

                if (fileReader.Name == "semantic-domain")
                {
                    SemanticDomainQuestions questions = PopulateSemanticDomainQuestionsFromXml(fileReader);
                    allQuestions.DomainKeyToQuestionsMap.Add(questions.SemanticDomainKey, questions);
                }
                else
                {
                    throw new InvalidOperationException(
                              String.Format("Malformed 'semantic-domain-questions' node found. Found an unexpected '{0}' element.", fileReader.Name));
                }
            }
            fileReader.Close();
            return(allQuestions);
        }
        private static void WriteSemanticDomainQuestions(XmlWriter fileWriter, SemanticDomainQuestions domainKeyToQuestionsMap)
        {
            fileWriter.WriteStartElement("semantic-domain");
            fileWriter.WriteAttributeString("guid", domainKeyToQuestionsMap.Guid.ToString());
            fileWriter.WriteAttributeString("id", domainKeyToQuestionsMap.SemanticDomainKey);

            foreach (string question in domainKeyToQuestionsMap.Questions)
            {
                WriteQuestion(fileWriter, question);
            }
            fileWriter.WriteEndElement();
        }
示例#3
0
        static void Main(string[] args)
        {
            string filePathToDdp1 = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp1.xml";
            string filePathToDdp4 = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp4.xml";
            string filePathToThaiDdp1Questions    = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp4Questions-th.xml";
            string filePathToEnglishDdp4Questions = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Ddp4Questions-en.xml";
            string filePathToMap = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\Semantic Domains v1 to v4 map.txt";

            string newDdpFile       = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\newDdp.xml";
            string newQuestionsFile = @"D:\SoftwareDevelopment\Ddp124Map\DDP4\newQuestion.xml";

            SemanticDomainCollection semanticDomainsDdp1 = SemanticDomainReader.ReadFromFile(filePathToDdp1);
            SemanticDomainCollection semanticDomainsDdp4 = SemanticDomainReader.ReadFromFile(filePathToDdp4);

            Dictionary <string, string> map = Ddp4to1MapReader.ReadFromFile(filePathToMap);

            foreach (SemanticDomainInfo ddp1SemanticDomain in semanticDomainsDdp1.AllSemanticDomains)
            {
                string correspondingNumberInDdp4 = ddp1SemanticDomain.Number;
                if (map.ContainsKey(ddp1SemanticDomain.Number))
                {
                    correspondingNumberInDdp4 = map[ddp1SemanticDomain.Number];
                }
                SemanticDomainInfo semanticDomainToMergeInto = semanticDomainsDdp4.GetSemanticDomainWithNumber(correspondingNumberInDdp4);
                semanticDomainToMergeInto.MergeInTranslations(ddp1SemanticDomain);
            }

            SemanticDomainWriter.WriteToFile(newDdpFile, semanticDomainsDdp4);

            SemanticDomainQuestionsCollection thaiDdp1Questions =
                SemanticDomainQuestionsReader.ReadFromFile(filePathToThaiDdp1Questions);

            SemanticDomainQuestionsCollection thaiDdp4Questions = new SemanticDomainQuestionsCollection(thaiDdp1Questions.Version, thaiDdp1Questions.WritingSystemId);

            foreach (SemanticDomainQuestions domainQuestions in thaiDdp1Questions.DomainKeyToQuestionsMap.Values)
            {
                string semanticDomainKey = GetMappedSemanticDomainKey(map, semanticDomainsDdp4, domainQuestions);

                bool questionsForDomainAlreadyExist =
                    thaiDdp4Questions.DomainKeyToQuestionsMap.ContainsKey(semanticDomainKey);

                if (!questionsForDomainAlreadyExist)
                {
                    SemanticDomainQuestions newQuestions = new SemanticDomainQuestions(Guid.NewGuid(), semanticDomainKey);
                    thaiDdp4Questions.DomainKeyToQuestionsMap.Add(semanticDomainKey, newQuestions);
                }
                thaiDdp4Questions.DomainKeyToQuestionsMap[semanticDomainKey].Questions.AddRange(domainQuestions.Questions);
            }

            SemanticDomainQuestionsWriter.WriteToFile(newQuestionsFile, thaiDdp4Questions);

            Console.ReadLine();
        }
示例#4
0
        static private SemanticDomainQuestions PopulateSemanticDomainQuestionsFromXml(XmlReader fileReader)
        {
            if (fileReader.Name != "semantic-domain")
            {
                throw new InvalidOperationException(String.Format("Node is {0} but we expected 'semantic-domain' node.", fileReader.Name));
            }

            Guid   guid = Guid.NewGuid();
            string id   = fileReader.GetAttribute("id");

            SemanticDomainQuestions questions = new SemanticDomainQuestions(guid, id);

            if (fileReader.IsEmptyElement)
            {
                return(questions);
            }

            while (fileReader.Read())
            {
                bool readerHasReachedEndOfSemanticDomainsQuestions =
                    fileReader.NodeType == XmlNodeType.EndElement && fileReader.Name == "semantic-domain";


                if (readerHasReachedEndOfSemanticDomainsQuestions)
                {
                    break;
                }

                if (fileReader.Name == "question")
                {
                    string question = PopulateSemanticDomainQuestionFromXml(fileReader);
                    questions.Questions.Add(question);
                }
                else
                {
                    throw new InvalidOperationException(
                              String.Format("Malformed 'semantic-domain' node found. found an unexpected '{0}' element.", fileReader.Name));
                }
            }
            Console.WriteLine("Populated questions");
            return(questions);
        }
示例#5
0
        private static string GetMappedSemanticDomainKey(Dictionary <string, string> map, SemanticDomainCollection semanticDomainsDdp4, SemanticDomainQuestions domainQuestions)
        {
            string semanticDomainNumber = domainQuestions.Number;

            if (map.ContainsKey(domainQuestions.Number))
            {
                semanticDomainNumber = map[domainQuestions.Number];
            }
            return(semanticDomainsDdp4.GetSemanticDomainWithNumber(semanticDomainNumber).Key);
        }