Esempio n. 1
0
        /// <summary>
        /// 解析题目
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="isCheck">是否检查段落</param>
        /// <param name="analysis">开启横向解析</param>
        /// <returns></returns>
        public Result Fun1(string file, bool isCheck, bool analysis)
        {
            return(RunFun(logPath =>
            {
                string path = allPath + @"" + file + ".docx";
                string tigerPath = allTiger + @"" + file + ".txt";

                WriteLog(logPath, file + "," + isCheck);

                string exclude = allPath + "ExcludeList.json";

                if (File.Exists(exclude))
                {
                    ExcludeList = ToolString.ConvertObject <List <string> >(File.ReadAllText(exclude));
                }

                XWPFDocument document = ToolFile.ReadWord(path);
                StringBuilder builder = new StringBuilder();

                string subject = "";

                List <string> answerList = new List <string>();
                List <string> itemList = new List <string>();

                string[] typeArr = new string[] { "单项选择", "多项选择", "判断" };
                int index = -1, nextIndex = -1; // 题目类型
                int itemIndex = 0;              // 答案索引
                int subjectIndex = 1;           // 题目索引

                if (analysis)
                {
                    fun1Str5 = @"[  ((]*[A-H]{1}[ .、.))::]+";
                }
                foreach (XWPFParagraph para in document.Paragraphs)
                {
                    string paragraphs = para.ParagraphText.Trim();//.Replace(" ", "");

                    for (int i = 0; i < typeArr.Length; i++)
                    {
                        if (ToolRegular.Contains(paragraphs, ".*" + typeArr[i] + @"题[((][\d]*.*[))]"))
                        {
                            nextIndex = i;
                            subjectIndex = 1;
                            if (index == -1)
                            {
                                index = i;
                            }
                        }
                    }

                    List <string> list = paragraphs.Split('\n').ToList();

                    foreach (var line in list)
                    {
                        // 判断是否是题目开头
                        Regex reg = new Regex(fun1Str1);
                        if (IsSubject(para, isCheck) || (reg.IsMatch(line) && !isCheck))
                        {
                            // 输出上一题
                            if (!string.IsNullOrWhiteSpace(subject))
                            {
                                subject = ReplaceSubject(subject, "、", isCheck);


                                if (!string.IsNullOrWhiteSpace(subject))
                                {
                                    answerList = AnswerList(subject);
                                    subject = answerList[0];
                                    answerList.Remove(subject);

                                    if (index == -1)
                                    {
                                        index = 0; nextIndex = 0;
                                    }

                                    subject += "[" + typeArr[index] + "题]\n";

                                    AnswerList(answerList, itemList, typeArr[index]);

                                    foreach (var item in itemList)
                                    {
                                        subject += item + "\n";
                                    }

                                    builder.AppendLine(subject);

                                    index = nextIndex;

                                    WriteLog(allTiger + @"" + file + ".log", "[" + typeArr[index] + "题]\t" + ToolString.AddStrByLength(subjectIndex + "", " ", 3) + "\t" + ToolString.AddStrByLength(string.Join(",", answerList), " ", 10) + "\t[" + string.Join(",", itemList.Select(c => c[0])) + "]", true);
                                    subjectIndex++;
                                }
                            }

                            // 开始下一题
                            itemList = new List <string>();
                            answerList = new List <string>();
                            subject = line;
                            itemIndex = 0;
                        }
                        else
                        {
                            if (string.IsNullOrWhiteSpace(subject))
                            {
                                continue;
                            }
                            // 判断是否是选项开头
                            else if (IsItem(para, isCheck) || new Regex(fun1Str3).IsMatch(line))
                            {
                                itemList.AddRange(ItemList(line, para, isCheck, itemIndex++, analysis));
                                continue;
                            }
                            // 判断是否是换行题目
                            else if (itemList.Count <= 0 && !string.IsNullOrWhiteSpace(line) && (!isCheck || para.GetNumFmt() != "lowerRoman"))
                            {
                                subject += Br + line;
                            }
                        }
                    }
                }

                // 输出最后一题
                if (!string.IsNullOrWhiteSpace(subject))
                {
                    subject = ReplaceSubject(subject, "、", isCheck);


                    if (!string.IsNullOrWhiteSpace(subject))
                    {
                        answerList = AnswerList(subject);
                        subject = answerList[0];
                        answerList.Remove(subject);

                        subject += "[" + typeArr[index] + "题]\n";

                        AnswerList(answerList, itemList, typeArr[index]);

                        foreach (var item in itemList)
                        {
                            subject += item + "\n";
                        }

                        builder.AppendLine(subject);

                        index = nextIndex;
                        WriteLog(allTiger + @"" + file + ".log", "[" + typeArr[index] + "题]\t" + ToolString.AddStrByLength(subjectIndex + "", " ", 3) + "\t" + ToolString.AddStrByLength(string.Join(",", answerList), " ", 10) + "\t[" + string.Join(",", itemList.Select(c => c[0])) + "]", true);
                    }
                }

                ToolFile.CreatFile(tigerPath, builder.ToString(), false);

                return Res;
            }));
        }