123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- using HTEXLib.COMM.Helpers;
- using HTEXLib.DOCX.Models;
- using HTEXLib.Helpers.ShapeHelpers;
- using HtmlAgilityPack;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace HTEXLib.Translator
- {
- public class HTML2ITEMV3Translator
- {
- /// <summary>
- /// 全角
- /// </summary>
- string[] aza = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
- /// <summary>
- /// 半角
- /// </summary>
- string[] azh = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
- public const string Answer = "Answer";
- public const string Analysis = "Analysis";
- public const string Ended = "Ended";
- public const string Point = "Point";
- public const string Score = "Score";
- public const string Summary = "Summary";
- public const string Filed = "Filed";
- public const string Level = "Level";
- public const string Count = "Count";
- public const string Taxonomy = "Taxonomy";
- // public LangConfig langConfig { get; set; }
- // public HtmlDocument doc { get; set; } = new HtmlDocument();
- public List<List<string>> optionsKeys { get; set; } = new List<List<string>>();
- public Dictionary<string, string[]> dict { get; set; }
- // public string[] Fileds { get; set; }
- public TagConfig? _TagConfig { get; set; }
- public HTML2ITEMV3Translator(string configPath)
- {
- FileStream fs = new FileStream(configPath+ "/LangConfigV3.json", System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
- string line;
- StringBuilder builder = new StringBuilder();
- while ((line = sr.ReadLine()) != null)
- {
- builder.Append(line.ToString());
- }
- sr.Close();
- string text = builder.ToString();
- _TagConfig=JsonSerializer.Deserialize<TagConfig>(text);
- //_TagConfig = text.FromJson<TagConfig>();
- dict = new Dictionary<string, string[]>();
- foreach (var _langConfig in _TagConfig.LangConfig)
- {
- string[] Fileds = _langConfig.Item.Filed.Split('|');
-
- foreach (var filed in Fileds)
- {
- dict[filed] = new string[] { _langConfig.Lang, Filed, $"{Array.IndexOf(Fileds, filed)}" };
- }
- string[] Judges = _langConfig.Item.Judge.Split('|');
- foreach (var Judge in Judges)
- {
- dict[Judge] = new string[] { _langConfig.Lang, Judge, $"{Array.IndexOf(Judges, Judge)}" };
- }
- dict[_langConfig.Item.Answer] = new string[] { _langConfig.Lang, Answer };
- dict[_langConfig.Item.Analysis] = new string[] { _langConfig.Lang, Analysis };
- dict[_langConfig.Item.Taxonomy] = new string[] { _langConfig.Lang, Taxonomy };
-
- dict[_langConfig.Item.Ended] = new string[] { _langConfig.Lang, Ended };
- dict[_langConfig.Item.Point] = new string[] { _langConfig.Lang, Point };
- dict[_langConfig.Item.Score] = new string[] { _langConfig.Lang, Score };
- dict[_langConfig.Item.Level] = new string[] { _langConfig.Lang, Level };
- dict[_langConfig.Item.Count] = new string[] { _langConfig.Lang, Count };
- foreach (string key in _langConfig.Item.Type.Keys)
- {
- dict[_langConfig.Item.Type[key]] = new string[] { _langConfig.Lang, Summary, key };
- }
- }
- string [] ops= _TagConfig.Options.Split('-');
- foreach (string op in ops)
- {
- if (!string.IsNullOrWhiteSpace(op))
- {
- var data = op.Select(z => z.ToString()).ToList();
- optionsKeys.Add(data);
- }
- }
- // optionsKeys = .Select(s => s.ToString()).ToArray();
- }
- /// <summary>
- /// 处理标签中以及题型标签中包含的空格字符
- /// </summary>
- /// <param name="html"></param>
- /// <returns></returns>
- public string BlankProcess(string html)
- {
- foreach (var _langConfig in _TagConfig.LangConfig) {
- string ans = _TagConfig.Start + _langConfig.Item.Answer + _TagConfig.End;
- string als = _TagConfig.Start + _langConfig.Item.Analysis + _TagConfig.End;
- string tax = _TagConfig.Start + _langConfig.Item.Taxonomy + _TagConfig.End;
- string end = _TagConfig.Start + _langConfig.Item.Ended + _TagConfig.End;
- string pot = _TagConfig.Start + _langConfig.Item.Point + _TagConfig.End;
- string scr = _TagConfig.Start + _langConfig.Item.Score + _TagConfig.End;
- string lvl = _TagConfig.Start + _langConfig.Item.Level + _TagConfig.End;
- string cut = _TagConfig.Start + _langConfig.Item.Count + _TagConfig.End;
- string[] Fileds = _langConfig.Item.Filed.Split('|');
- foreach (var filed in Fileds)
- {
- var fld = _TagConfig.Start + filed + _TagConfig.End;
- string[] fldarry = fld.Select(s => s.ToString()).ToArray();
- string fldReg = string.Join("\\s*", fldarry);
- html = Regex.Replace(html, fldReg, fld);
- }
- string[] ansarry = ans.Select(s => s.ToString()).ToArray();
- string[] alsarry = als.Select(s => s.ToString()).ToArray();
- string[] taxarry = tax.Select(s => s.ToString()).ToArray();
- string[] endarry = end.Select(s => s.ToString()).ToArray();
- string[] potarry = pot.Select(s => s.ToString()).ToArray();
- string[] scrarry = scr.Select(s => s.ToString()).ToArray();
- string[] lvlarry = lvl.Select(s => s.ToString()).ToArray();
- string[] cutarry = cut.Select(s => s.ToString()).ToArray();
- string ansReg = string.Join("\\s*", ansarry);
- string alsReg = string.Join("\\s*", alsarry);
- string taxReg = string.Join("\\s*", taxarry);
- string endReg = string.Join("\\s*", endarry);
- string potReg = string.Join("\\s*", potarry);
- string scrReg = string.Join("\\s*", scrarry);
- string lvlReg = string.Join("\\s*", lvlarry);
- string cutReg = string.Join("\\s*", cutarry);
- html = Regex.Replace(html, ansReg, ans);
- html = Regex.Replace(html, alsReg, als);
- html = Regex.Replace(html, taxReg, tax);
- html = Regex.Replace(html, endReg, end);
- html = Regex.Replace(html, potReg, pot);
- html = Regex.Replace(html, scrReg, scr);
- html = Regex.Replace(html, lvlReg, lvl);
- html = Regex.Replace(html, cutReg, cut);
- string blankReg = "\\s*";
- foreach (string value in _langConfig.Item.Type.Values)
- {
- string tag = $"{_TagConfig.Start}\\s*\\d+\\s*{string.Join("\\s*", value.Select(s => s.ToString()).ToArray())}\\s*{_TagConfig.End}";
- var m = Regex.Match(html, tag);
- while (m.Success)
- {
- string blankStr = Regex.Replace(m.Value, blankReg, "");
- html = html.Replace(m.Value, blankStr);
- m = m.NextMatch();
- }
- }
- }
- return html;
- }
- public (List<DOCX.Models.ItemInfo> tests, List<string> error) Translate(string html )
- {
- List<string> emferror = new List<string>();
- string mathjax = "<script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>";
- html = html.Replace(mathjax, "");
- //去除class 以及span标签"
- string classpattern = "class=\"([^\"]*)\"";
- html = Regex.Replace(html, classpattern, "");
- string pattern = "<span([^>]{0,})>";
- html = Regex.Replace(html, pattern, "");
- string apattern = "<a([^>]{0,})></a>";
- html = Regex.Replace(html, apattern, "");
- //<a id="OP2_89937270DA164EFF8650A3FB645B678A" ></a>B<a id="OPTG2_89937270DA164EFF8650A3FB645B678A" ></a>
- html = html.Replace(" close=\"\" separators=\" | \">", "");
- html = html.Replace("\t", " ").Replace("<span>", "").Replace("</span>", "").Replace("dir=\"ltr\"", "");
- HtmlDocument doc = new HtmlDocument();
- doc.LoadHtml(html);
- //处理 标签中包含的空格字符
- html = BlankProcess(html);
- var array = Regex.Split(html, "{([\\S]*?)}");
- List<KeyValuePair<int[], List<string>>> composeKeys = new List<KeyValuePair<int[], List<string>>>();
- //List<string>
- //处理综合题
- for (int index = 1; index < array.Length; index++)
- {
- var tagValue = BlankTag(array[index]);
- tagValue = Regex.Replace(tagValue, @"\d", "");
- if (dict.TryGetValue(tagValue, out string[] keyInfo))
- {
- if (keyInfo[1] == Summary && keyInfo[2].Equals("compose"))
- {
- var curr = index;
- List<string> comsArray = new List<string>() ;
- bool hasEndFlage = false;
- int allIndex=0;
- for (int composeIndex = index + 1; composeIndex < array.Length; composeIndex++) {
- var conIndex = BlankTag(array[composeIndex]);
- conIndex = Regex.Replace(conIndex, @"\d", "");
- var langStr = keyInfo[0];
- var langfig= _TagConfig.LangConfig.Where(x => x.Lang == langStr).FirstOrDefault();
- allIndex = composeIndex;
- //当没有综合题结束时,又遇到新的综合题
- bool isNewCompose = false;
- if (dict.TryGetValue(conIndex, out string[] newTagKeyInfo)) {
- if (newTagKeyInfo[1] == Summary && newTagKeyInfo[2].Equals("compose")) {
- isNewCompose = true;
- }
- }
- if (conIndex.Equals(langfig.Item.Ended) || conIndex.Equals(tagValue + langfig.Item.Ended) )
- {
- comsArray.AddRange(array.ToList().GetRange(index + 1, composeIndex - index - 1));
- index = composeIndex + 1;
- hasEndFlage = true;
- break;
- }
- if ( isNewCompose)
- {
- comsArray.AddRange(array.ToList().GetRange(index + 1, composeIndex - index - 1));
- index = composeIndex-1;
- hasEndFlage = true;
- break;
- }
- }
- //当没有综合题结束时,并且没有遇到新的综合题
- if (hasEndFlage == false && allIndex>0) {
- comsArray.AddRange(array.ToList().GetRange(index + 1, allIndex - index ));
- index = allIndex+1;
- }
- //int[0]综合题开始标签位置,int[1]综合题结束标签位置,int[2]综合题第一个小题的开始标签位置
- KeyValuePair<int[], List<string>> coms = new KeyValuePair<int[], List<string>>(new int[] { curr, index }, comsArray);
- composeKeys.Add(coms);
- }
- }
- }
- List<KeyValuePair<int[], List<ItemInfo>>> composeList = new List<KeyValuePair<int[], List<ItemInfo>>>();
- foreach(var conskey in composeKeys) {
- (List<DOCX.Models.ItemInfo> consInner, List<string> eferror)= ConvertTest(conskey.Value.ToArray(), null);
- if (eferror.IsNotEmpty())
- {
- emferror.AddRange(eferror);
- }
-
- int stIndex = conskey.Key[0]+1;
- if (consInner.IsNotEmpty())
- {
- stIndex = consInner[0].order<=0? conskey.Key[0]+consInner[0].order:stIndex;
- }
- KeyValuePair<int[], List<ItemInfo>> innerComposeItem = new KeyValuePair<int[], List<ItemInfo>>(new int[] { conskey.Key[0], conskey.Key[1], stIndex }, consInner);
- composeList.Add(innerComposeItem);
- }
- (List < DOCX.Models.ItemInfo > tests,List<string> error) = ConvertTest(array, composeList);
- if (error.IsNotEmpty()) {
- emferror.AddRange(error);
- }
- return (tests, emferror);
- }
- private string BlankTag(string tagHtml) {
- //去掉标签中的Html
- HtmlDocument doc = new HtmlDocument();
- doc.LoadHtml(tagHtml);
- var tagValue = doc.DocumentNode.InnerText.Replace("{", "").Replace("}", "")
- .Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "")
- .Replace(" ", "").Replace(" ", "").Replace(" ", "");
- // tagValue = Regex.Replace(tagValue, @"\d", "");
- tagValue = Regex.Replace(tagValue, @"\s", "");
- return tagValue;
- }
- private string BlankPointTag(string tagHtml)
- {
- //去掉标签中的Html
- HtmlDocument doc = new HtmlDocument();
- doc.LoadHtml(tagHtml);
- var tagValue = doc.DocumentNode.InnerText.Replace("{", "").Replace("}", "")
- .Replace("\n", " ").Replace("\t", " ").Replace("\r", " ")
- .Replace(" ", " ").Replace(" ", " ").Replace(" ", " ");
- // tagValue = Regex.Replace(tagValue, @"\d", "");
- tagValue = Regex.Replace(tagValue, @"\s", " ");
- return tagValue;
- }
- public (List<DOCX.Models.ItemInfo> ItemInfo, List<string> error) ConvertTest(string[] array, List<KeyValuePair<int[], List<ItemInfo>>> composeList) {
- List<DOCX.Models.ItemInfo> tests = new List<DOCX.Models.ItemInfo>();
- List<string> error = new List<string>();
- //内容零时变量,追加完成后重新实例化 new StringBuilder()
- StringBuilder content = new StringBuilder();
- //告知遇到新标签,内容需要重新初始化实例
- DOCX.Models.ItemInfo test=null;
- string openTag = "";
- string openTagVal = "";
- bool openFlag = false;
- string openTagLang = "";
- if (array[0].Contains("EmfBase64ConvertError"))
- {
- error.Add(array[0]);
- }
- for (int index = 1; index < array.Length; index++) {
- if (index % 2 == 1)
- {
- //去掉标签中的{} 空格换行制表符及Html空格数字等
- var tagValue = BlankTag(array[index]);
- tagValue = Regex.Replace(tagValue, @"\d", "");
- Console.WriteLine(index);
- if (index==157) {
- Console.WriteLine(index);
- }
- if (dict.TryGetValue(tagValue, out string[] keyInfo))
- {
- switch ( keyInfo[1] ) {
- case Summary:
- if (!string.IsNullOrEmpty(openTag) && openFlag && test != null)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- if (test != null)
- {
- tests.Add(test);
- }
- //下列代码不能调整顺序
- if (keyInfo[2].Equals("compose"))
- {
- var id = System.Guid.NewGuid().ToString();
- var compose = new ItemInfo { type = keyInfo[2],objective=false,order=index,id=id };
- if (composeList.IsNotEmpty())
- {
- var childItem = composeList.Where(x => x.Key[0] == index).FirstOrDefault();
- childItem.Value.ForEach(x => x.pid = id);
- compose.children = childItem.Value;
- var ques= array.ToList().GetRange(childItem.Key[0]+1, childItem.Key[2]- childItem.Key[0]);
- compose.question= string.Join("", ques);
- index = childItem.Key[1] - 1;
- }
- tests.Add(compose);
- openTag = "";
- openTagVal = "";
- openFlag = false;
- openTagLang = keyInfo[0];
- test = null;
- }
- else {
- var id = System.Guid.NewGuid().ToString();
- test = new ItemInfo() { type = keyInfo[2], order = index, id = id };
- if (keyInfo[2].Equals("single") || keyInfo[2].Equals("multiple") || keyInfo[2].Equals("judge")||keyInfo[2].Equals("sortmultiple"))
- {
- test.objective = true;
- }
- else
- {
- test.objective = false;
- }
- //处理填空题默认填空数量为1
- if (keyInfo[2].Equals("complete"))
- {
- test.blankCount = 1;
- }
- //不是填空题 的填空数量为0
- else {
- test.blankCount = 0;
- }
- openTag = Summary;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- }
- break;
- case Answer:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Answer;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Analysis:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Analysis;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Taxonomy:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Taxonomy;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Ended:
- break;
- case Point:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Point;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Score:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Score;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Level:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Level;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Filed:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Filed;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- case Count:
- //下列代码不能调整顺序
- if (!string.IsNullOrEmpty(openTag) && openFlag)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- content = new StringBuilder();
- }
- openTag = Count;
- openTagVal = tagValue;
- openFlag = true;
- openTagLang = keyInfo[0];
- break;
- }
- }
- //如果不是标签内的则累加到内容上
- else {
- content.Append(array[index]);
- if (array[index].Contains("EmfBase64ConvertError"))
- {
- error.Add(array[index]);
- }
- }
- }
- else {
- //偶数序列为内容
- content.Append(array[index]);
- if (array[index].Contains("EmfBase64ConvertError"))
- {
- error.Add(array[index]);
- }
- }
- }
- if (test != null)
- {
- DoOpenTag(openTag, openTagVal, openFlag, openTagLang, content, test);
- tests.Add(test);
- }
- return (tests,error);
- }
- public (List<CodeValue> options,string question) OptionProcess(string question) {
- for (int idx = 0; idx < 26; idx++)
- {
- question = question.Replace(aza[idx], azh[idx]);
- }
- List<CodeValue> options = new List<CodeValue>();
- int index = 0;
- string optsRgex = optionsKeys[0][0] + "\\s*(\\.|\\.|\\、|\\:|\\:)([\\s\\S]*?).*";
- string optsHtml = Regex.Match(question, optsRgex).Value;
- if (string.IsNullOrWhiteSpace(optsHtml) && optionsKeys.Count>1) {
- optsRgex = optionsKeys[1][0] + "\\s*(\\.|\\.|\\、|\\:|\\:)([\\s\\S]*?).*";
- index=1;
- optsHtml = Regex.Match(question, optsRgex).Value;
- }
- // StringBuilder textImg = new StringBuilder();
- for (int i = 0; i < optionsKeys[index].Count - 1; i++)
- {
- string optRgex = optionsKeys[index][i] + "\\s*(\\.|\\.|\\、|\\:|\\:)([\\s\\S]*?)" + optionsKeys[index][i + 1] + "\\s*(\\.|\\.|\\、|\\:|\\:)";
- string optHtml = Regex.Match(optsHtml, optRgex).Value;
- if (string.IsNullOrWhiteSpace(optHtml)) {
- optRgex = optionsKeys[index][i] + "\\s*(\\.|\\.|\\、|\\:|\\:).*";
- optHtml = Regex.Match(optsHtml, optRgex).Value;
- }
- if (!string.IsNullOrEmpty(optHtml))
- {
- optHtml = Regex.Replace(optHtml, optionsKeys[index][i + 1] + "\\s*(\\.|\\.|\\、|\\:|\\:)", "");
- optHtml = optHtml.Substring(2, optHtml.Length - 2);
- optHtml = HtmlHelper.DoUselessTag(optHtml);
- optHtml = optHtml.TrimStart().TrimEnd();
- // textImg.Append(HtmlHelper.DoTextImg(optHtml));
- if (index==1)
- {
- var code = optionsKeys[0][int.Parse(optionsKeys[index][i])-1];
- options.Add(new CodeValue { code = code, value = optHtml });
- }
- else {
- options.Add(new CodeValue { code = optionsKeys[index][i], value = optHtml });
- }
-
- }
- }
- if (!string.IsNullOrWhiteSpace(optsHtml))
- {
- return (options, question.Replace(optsHtml, ""));
- }
- else {
- return (new List<CodeValue>(), question) ;
- }
- }
- public void DoOpenTag (string openTag,string openTagVal, bool openFlag, string openTagLang , StringBuilder content , DOCX.Models.ItemInfo test) {
- if (test != null) {
- switch (openTag) {
- case Summary:
- if (test.type.Equals("single") || test.type.Equals("multiple")|| test.type.Equals("judge")||test.type.Equals("sortmultiple"))
- {
- (List<CodeValue> options, string question) = OptionProcess(content.ToString());
- test.option = options;
- test.question = HtmlHelper.DoUselessTag(question) ;
- }
- else {
- test.question = HtmlHelper.DoUselessTag(content.ToString());
- }
- break;
- case Answer:
- if (test.type.Equals("single") || test.type.Equals("multiple")|| test.type.Equals("judge")||test.type.Equals("sortmultiple"))
- {
- HashSet<string> ans = new HashSet<string>();
- var anstr = BlankTag(content.ToString());
- for (int idx = 0; idx < 26; idx++)
- {
- anstr = anstr.Replace(aza[idx], azh[idx]);
- }
- if (!test.type.Equals("judge"))
- {
- anstr.Select(s => s.ToString()).ToList().ForEach(x =>
- {
- ans.Add(x);
- });
- }
- else {
- ans.Add(anstr);
- }
- if (test.option.IsNotEmpty())
- {
- List<string> codes= test.option.Select(x => x.code).ToList();
- var notin = ans.Except(codes).ToList();
- List<string> ansd= ans.ToList();
- ansd.RemoveAll(x => notin.Contains(x));
- if (!ansd.IsNotEmpty()) {
- ansd=new List<string>();
- foreach (var an in ans) {
- if (int.TryParse(an, out int op) && op>=1) {
- ansd.Add(optionsKeys[0][op-1]);
- }
- }
- ansd.RemoveAll(x => notin.Contains(x));
- }
- test.answer = ansd;
- }
- else {
- test.answer = ans.ToList();
- }
-
- if (test.type.Equals("judge")) {
- if (test.answer != null && test.answer.Count > 0)
- {
- test.answer = new List<string>() { ans.ToList().First() };
- if (dict.TryGetValue(test.answer[0], out string[] keyInfoas))
- {
- var lang = keyInfoas[0];
- var LangConfigAs =_TagConfig.LangConfig.Where(x => x.Lang == lang).FirstOrDefault();
- string[] Judge = LangConfigAs.Item.Judge.Split('|');
- List<CodeValue> option = new List<CodeValue>() { new CodeValue { code = "A", value = Judge[0] }, new CodeValue { code = "B", value = Judge[1] } };
- int index = 0;
- foreach (var j in Judge)
- {
- if (String.Equals(test.answer[0], j, StringComparison.CurrentCultureIgnoreCase))
- {
- test.answer[0] = option[index].code;
- test.option = option;
- break;
- }
- index += 1;
- }
- }
- }
- }
- }
- else {
- test.answer = new List<string>() { HtmlHelper.DoUselessTag(content.ToString()) };
- }
- break;
- case Analysis:
- test.explain = HtmlHelper.DoUselessTag(content.ToString());
- break;
- case Taxonomy:
- {
- string taxonomy = HtmlHelper.DoUselessTag(content.ToString());
- taxonomy= BlankTag(taxonomy);
- if ( !string.IsNullOrWhiteSpace(taxonomy) && dict.TryGetValue(taxonomy, out string[] keyInfo))
- {
-
- var LangConfigfd = _TagConfig.LangConfig.Where(x => x.Lang ==keyInfo[0]).FirstOrDefault();
- var Fileds = LangConfigfd.Item.Filed.Split('|');
- int fld = Array.IndexOf(Fileds, taxonomy) + 1;
- if (fld>0) {
- test.field =fld;
- }
- }
- break;
- }
- case Ended: break;
- case Point:
- string Points = BlankPointTag(content.ToString());
- if (!string.IsNullOrWhiteSpace(Points))
- {
-
- string[] ps = Regex.Split(Points, "\\.|\\.|\\。|\\;|\\;");
- // string[] ps = Regex.Split(Points, "\\.|\\.|\\、|\\:|\\:|\\,|\\,|\\;|\\;");
- if (ps != null && ps.Length > 0)
- {
- test.knowledge = ps.Distinct().ToList();
- }
- }
- break;
- case Score:
- //单选或多选,判断答案 脱html标签
- string Scores = BlankTag(content.ToString());
- //正则匹配数字 整数和小数点
- var reg = "^[0-9]+(\\.?[0-9]+)?";
- Match m1t = Regex.Match(Scores, reg);
- double.TryParse(m1t.Value, out double sc);
- test.score = sc;
- break;
- case Level:
- //单选或多选,判断答案 脱html标签
- string Levels = BlankTag(content.ToString());
- //正则匹配数字 整数和小数点
- var lelreg = "^[0-9]+(\\.?[0-9]+)?";
- Match lelm1t = Regex.Match(Levels, lelreg);
- int.TryParse(lelm1t.Value, out int lvl);
- test.level = lvl;
- break;
- case Filed:
- {
- if (dict.TryGetValue(openTagVal, out string[] keyInfo))
- {
- var LangConfigfd = _TagConfig.LangConfig.Where(x => x.Lang == keyInfo[0]).FirstOrDefault();
- var Fileds = LangConfigfd.Item.Filed.Split('|');
- int fld = Array.IndexOf(Fileds, openTagVal) + 1;
- if (fld>0)
- {
- test.field =fld;
- }
- }
- break;
- }
- case Count:
- //只有填空题才配置填空数量
- if (test.type.Equals("complete")) {
- //单选或多选,判断答案 脱html标签
- string Counts = BlankTag(content.ToString());
- //正则匹配数字 整数和小数点
- var ctreg = "^[0-9]+(\\.?[0-9]+)?";
- Match ctm1t = Regex.Match(Counts, ctreg);
- int.TryParse(ctm1t.Value, out int ct);
- test.blankCount = ct;
- }
- break;
- }
- }
- }
- }
- }
|