Example #1
0
        private void main(string path)
        {
            XmlNodeList N2;

            Essay_exam_reader xml_reader = new Essay_exam_reader();
            N2 = xml_reader.xml_reader_N2(path);

            Essay_exam_withKey_Writer essay_exam_withKey_writer = new Essay_exam_withKey_Writer();
            essay_exam_withKey_writer.setpath(".\\xml\\exam_withKey.xml");

            Essay_QA_find_key xml_find_key = new Essay_QA_find_key();

            //List<Thread> find_key_thread = new List<Thread>();

            //int ft = 0;

            //foreach (XmlNode n2 in N2)
            //{

            //    find_key_thread.Add(new Thread(() =>
            //    {
            //        XmlDocument xmldocument = new XmlDocument();
            //        XmlNode new_n2 = xmldocument.ImportNode(n2, true);
            //        List<string> key;
            //        key = xml_find_key.findkey(new_n2, ft);
            //        Monitor.Enter(essay_exam_withKey_writer);
            //        essay_exam_withKey_writer.write_key(new_n2, key);
            //        Monitor.Exit(essay_exam_withKey_writer);
            //    }
            //    ));
            //    find_key_thread[ft].Start();
            //    ft++;
            //}

            //foreach (Thread t in find_key_thread) t.Join();

            foreach (XmlNode n2 in N2)
            {
                XmlDocument xmldocument = new XmlDocument();
                XmlNode new_n2 = xmldocument.ImportNode(n2, true);
                List<string> key;
                key = xml_find_key.findkey(new_n2);
                essay_exam_withKey_writer.write_key(new_n2, key);
            }

            essay_exam_withKey_writer.save();

            MessageBox.Show("finish QA");
        }
        //Essay分類,各分類特殊需求操作
        public List<string> findkey(XmlNode N2)
        {
            string type = "";
            string mainKey = "Nu11";
            XmlNodeList N3D, N3I, N4IR, N4QI, N4DU, N5QIR;
            List<string> Key = new List<string>();

            Essay_exam_reader xml_reader = new Essay_exam_reader();

            N3D = xml_reader.xml_reader_N3_data(N2);
            N3I = xml_reader.xml_reader_N3_instruction(N2);

            //Analytics question
            if (N2.Attributes["minimal"].Value == "yes")
            {
                //type1,2
                //Analytics instruction
                foreach (XmlNode N3 in N3I)
                {
                    //set type
                    if (N3.HasChildNodes)
                    {
                        //type1
                        type = "type-1";
                        Key.Add(type);
                        N4IR = xml_reader.xml_reader_N4_instruction_ref(N2);
                        foreach (XmlNode N4 in N4IR)
                        {
                            mainKey = N4.Attributes["target"].Value.Trim();
                        }
                    }
                    else
                    {
                        //type2
                        type = "type-2";
                        Key.Add(type);
                    }

                    //Analytics instruction by nlp
                    String instruction = N3.InnerText;
                    foreach (string k in nlp(instruction))
                    {
                        Key.Add("#/" + k.Trim());
                    }
                }

                //Analytics data
                foreach (XmlNode N3 in N3D)
                {
                    if (N3.Attributes["id"].Value.Trim() == mainKey)
                    {
                        //all main key
                        String data = N3.InnerText;
                        string[] datalist = data.Split(',');
                        foreach (string d in datalist)
                        {
                            Key.Add("@/" + d.Trim());
                        }
                    }
                    else
                    {
                        //Analytics data by nlp
                        foreach (string k in nlp(N3.InnerText))
                        {
                            Key.Add("#/" + k.Trim());
                        }
                    }
                }
            }
            else
            {
                //type3,4
                //Analytics instruction
                foreach (XmlNode N3 in N3I)
                {
                    //set type
                    if (N3.HasChildNodes)
                    {
                        //type3
                        type = "type-3";
                        Key.Add(type);
                    }
                    else
                    {
                        //type4
                        type = "type-4";
                        Key.Add(type);
                    }

                    //Analytics instruction by nlp
                    foreach (string k in nlp(N3.InnerText))
                    {
                        Key.Add("#/" + k.Trim());
                    }
                }

                //Analytics data
                foreach (XmlNode N3 in N3D)
                {
                    //Analytics data by nlp
                    foreach (string k in nlp(N3.InnerText))
                    {
                        Key.Add("#/" + k.Trim());
                    }
                }

                //Analytics question_instruction
                N4QI = xml_reader.xml_reader_N4_question_instruction(N2);
                N4DU = xml_reader.xml_reader_N4_data_utext(N2);
                N5QIR = xml_reader.xml_reader_N5_question_instruction_ref(N2);
                foreach (XmlNode N4 in N4QI)
                {
                    //have ref target
                    if (N5QIR != null)
                    {
                        foreach (XmlNode N5 in N5QIR)
                        {
                            try
                            {
                                string tager = N5.Attributes["target"].Value;
                                foreach (XmlNode N4_5 in N4DU)
                                {
                                    if (N4_5.Attributes["id"].Value == tager)
                                    {
                                        string[] w = N4_5.InnerText.Split(')');
                                        Key.Add("@/" + w[1].Trim());
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {

                            }
                        }
                    }

                    //Analytics question_instruction by nlp
                    foreach (string k in nlp(N4.InnerText))
                    {
                        Key.Add("#/" + k.Trim());
                    }
                }
            }

            Key = delete_repeat_key(Key);

            return Key;
        }
        //經由nlp分析出的ner將單字組合為有意義的key組裝
        private List<string> ner(string path)
        {
            List<string> return_key = new List<string>();

            Essay_exam_reader xml_reader = new Essay_exam_reader();
            XmlDocument xmldocument = new XmlDocument();

            //讀出Relay.xml中nlp分析結果
            XmlNodeList nlp_tokens = xml_reader.xml_reader_nlp(path);

            string selectKey = "";
            Boolean iskey = true;

            foreach (XmlNode nlp_token in nlp_tokens)
            {
                //將nlpNode轉換為本地物件
                XmlNode local_nlp_token = xmldocument.ImportNode(nlp_token, true);

                //取得nlp_token中word資料
                XmlNode nlp_word = xml_reader.xml_reader_nlp_word(local_nlp_token);

                //取得nlp_token中ner資料
                XmlNode nlp_ner = xml_reader.xml_reader_nlp_ner(local_nlp_token);

                //組合key
                if (nlp_ner.InnerText == "DATE")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else if (nlp_ner.InnerText == "LOCATION")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else if (nlp_ner.InnerText == "ORGANIZATION")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else if (nlp_ner.InnerText == "MONEY")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else if (nlp_ner.InnerText == "PERSON")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else if (nlp_ner.InnerText == "PERCENT")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else if (nlp_ner.InnerText == "TIME")
                {
                    selectKey += (" " + nlp_word.InnerText);
                    iskey = false;
                }
                else
                {
                    if (iskey != true)
                    {
                        return_key.Add(selectKey);
                        selectKey = "";
                        iskey = true;
                    }
                }
            }

            return return_key;
        }