]{0,}\\s*exps-sq[^>]{0,}>";
var explains = Regex.Split(x.explanation, rg_qml_exps_sq);
if (questiones.Length > 1)
{
index += 1;
XkwItemInfo itemInfo = new XkwItemInfo
{
source = 3,
shaCode = ShaHashHelper.GetSHA1(x.stem),
id = x.id,
order = index,
subjectName = course?.subject_name,
periodName = course?.stage_name,
field = field,
level = level,
gradeNames = gradeNames,
knowledge = knowledge,
objective = objective,
tag = tag,
question = questiones[0],
type = "compose",
};
itemInfos.Add(itemInfo);
for (int i = 1; i < questiones.Length; i++)
{
index += 1;
XkwItemInfo child_itemInfo = new XkwItemInfo
{
pid = x.id,
source = 3,
id = $"{x.id}-{i}",
order = index,
subjectName = course?.subject_name,
periodName = course?.stage_name,
field = field,
level = level,
gradeNames = gradeNames,
knowledge = knowledge,
objective = objective,
tag = "",
};
questiones[i] = questiones[i].Replace("
", "");
if (questiones[i].Contains("qml-og"))
{
child_itemInfo.objective = true;
string shtml = questiones[i];
//去除class 以及span标签"
// shtml = Regex.Replace(shtml, table_pattern, "").Replace("", "").Replace("
", "").Replace("", "").Replace(" | ", "").Replace("
", "");
//shtml = Regex.Replace(shtml, classpattern, "");
//shtml = Regex.Replace(shtml, pattern, "");
//shtml = Regex.Replace(shtml, apattern, "");
//shtml = shtml.Replace(" close=\"\" separators=\" | \">", "");
//shtml = shtml.Replace("\t", " ").Replace("
", "").Replace("", "").Replace("dir=\"ltr\"", "");
//处理 标签中包含的空格字符
//处理题干
(List
options, string question) = OptionProcess(shtml);
child_itemInfo.option = options;
child_itemInfo.opts = options.Count;
child_itemInfo.question = question;
//处理答案
//去除class 以及span标签"
if (answers.Length - 1 >= i)
{
string ahtml = answers[i];
ahtml = Regex.Replace(ahtml, classpattern, "");
ahtml = Regex.Replace(ahtml, pattern, "");
ahtml = Regex.Replace(ahtml, apattern, "");
ahtml = ahtml.Replace(" close=\"\" separators=\" | \">", "");
ahtml = ahtml.Replace("\t", " ").Replace("", "").Replace("", "").Replace("dir=\"ltr\"", "");
HashSet ans = new HashSet();
var anstr = BlankTag(ahtml);
for (int idx = 0; idx < 26; idx++)
{
anstr = anstr.Replace(aza[idx], azh[idx]);
}
anstr.Select(s => s.ToString()).ToList().ForEach(x =>
{
ans.Add(x);
});
child_itemInfo.answerHtml = answers[0];
child_itemInfo.answer = ans.ToList();
child_itemInfo.objective = true;
if (child_itemInfo.opts > 0)
{
if (ans.Count > 1)
{
child_itemInfo.type = "multiple";
}
else
{
child_itemInfo.type = "single";
}
}
else
{
if (child_itemInfo.answer.Any()
&&(child_itemInfo.answer.First().Equals("√") || child_itemInfo.answer.First().Equals("×")
|| child_itemInfo.answer.First().Equals("对") || child_itemInfo.answer.First().Equals("错")
|| child_itemInfo.answer.First().Equals("正确") || child_itemInfo.answer.First().Equals("错误")))
{
child_itemInfo.type = "judge";
child_itemInfo.opts = 2;
child_itemInfo.option = new List() { new CodeValue { code = "A", value = "对" }, new CodeValue { code = "B", value = "错" } };
child_itemInfo.answer = itemInfo.answer.First().Equals("√") || child_itemInfo.answer.First().Equals("对")|| child_itemInfo.answer.First().Equals("正确") ? new List { "A" } : new List { "B" };
}
else
{
child_itemInfo.objective = false;
child_itemInfo.type = "subjective";
}
}
}
//处理解析
if (explains.Length - 1 >= i)
{
child_itemInfo.explain = explains[i];
}
}
else
{
child_itemInfo.type = "subjective";
child_itemInfo.objective = false;
if (explains.Length - 1 >= i)
{
child_itemInfo.explain = explains[i];
}
string ansstr = "";
if (answers.Length - 1 >= i)
{
child_itemInfo.answer = new List { answers[i] };
ansstr = HtmlHelper.DoUselessTag(answers[i]);
}
child_itemInfo.question = questiones[i];
if (ansstr.Equals("√") || ansstr.Equals("×")
|| ansstr.Equals("对") || ansstr.Equals("错")
|| ansstr.Equals("正确") || ansstr.Equals("错误"))
{
child_itemInfo.objective = true;
child_itemInfo.type = "judge";
child_itemInfo.opts = 2;
child_itemInfo.option = new List() { new CodeValue { code = "A", value = "对" }, new CodeValue { code = "B", value = "错" } };
child_itemInfo.answer = ansstr.Equals("√") || ansstr.Equals("对") || ansstr.Equals("正确") ? new List { "A" } : new List { "B" };
}
}
string rg_ques_no = "]{0,}\\s*ques-no[^>]{0,}>[\\s\\S]*?";
child_itemInfo.question = Regex.Replace(child_itemInfo.question, rg_ques_no, "");
child_itemInfo.shaCode = ShaHashHelper.GetSHA1(child_itemInfo.question);
itemInfo.children.Add(child_itemInfo);
//itemInfos.Add(child_itemInfo);
}
}
else
{
XkwItemInfo itemInfo = new XkwItemInfo
{
source = 3,
shaCode = ShaHashHelper.GetSHA1(x.stem),
id = x.id,
order = index,
explain = x.explanation,
subjectName = course?.subject_name,
periodName = course?.stage_name,
// explain = x.explanation.Replace("【分析】", "分析:").Replace("【详解】", "详解:")
field = field,
level = level,
gradeNames = gradeNames,
knowledge = knowledge,
objective = objective,
tag = tag
};
index += 1;
//如果是客观题,则需要处理选项和答案。
if (itemInfo.objective)
{
string shtml = x.stem;
//去除class 以及span标签"
// shtml = Regex.Replace(shtml, table_pattern, "").Replace("", "").Replace("", "").Replace("", "").Replace(" | ", "").Replace("
", "");
//shtml = Regex.Replace(shtml, classpattern, "");
//shtml = Regex.Replace(shtml, pattern, "");
//shtml = Regex.Replace(shtml, apattern, "");
//shtml = shtml.Replace(" close=\"\" separators=\" | \">", "");
//shtml = shtml.Replace("\t", " ").Replace("", "").Replace("", "").Replace("dir=\"ltr\"", "");
//处理 标签中包含的空格字符
//处理题干
(List options, string question) = OptionProcess(shtml);
itemInfo.option = options;
itemInfo.opts = options.Count;
itemInfo.question = question;
itemInfo.objective = true;
//处理答案
//去除class 以及span标签"
string ahtml = x.answer;
ahtml = Regex.Replace(ahtml, classpattern, "");
ahtml = Regex.Replace(ahtml, pattern, "");
ahtml = Regex.Replace(ahtml, apattern, "");
ahtml = ahtml.Replace(" close=\"\" separators=\" | \">", "");
ahtml = ahtml.Replace("\t", " ").Replace("", "").Replace("", "").Replace("dir=\"ltr\"", "");
HashSet ans = new HashSet();
var anstr = BlankTag(ahtml);
for (int idx = 0; idx < 26; idx++)
{
anstr = anstr.Replace(aza[idx], azh[idx]);
}
anstr.Select(s => s.ToString()).ToList().ForEach(x =>
{
ans.Add(x);
});
itemInfo.answerHtml = x.answer;
itemInfo.answer = ans.ToList();
if (itemInfo.opts > 0)
{
if (ans.Count > 1)
{
itemInfo.type = "multiple";
}
else
{
itemInfo.type = "single";
}
}
else {
if (itemInfo.answer.Any()
&& (itemInfo.answer.First().Equals("√") || itemInfo.answer.First().Equals("×")
|| itemInfo.answer.First().Equals("对") || itemInfo.answer.First().Equals("错")
|| itemInfo.answer.First().Equals("正确") || itemInfo.answer.First().Equals("错误")))
{
itemInfo.type = "judge";
itemInfo.opts = 2;
itemInfo.option = new List() { new CodeValue { code = "A", value = "对" }, new CodeValue { code = "B", value = "错" } };
itemInfo.answer = itemInfo.answer.First().Equals("√") || itemInfo.answer.First().Equals("对") || itemInfo.answer.First().Equals("正确") ? new List { "A" } : new List { "B" };
}
else
{
itemInfo.objective = false;
itemInfo.type = "subjective";
}
}
}
else
{
itemInfo.type = "subjective";
itemInfo.question = x.stem;
itemInfo.answer = new List() { HtmlHelper.DoUselessTag(x.answer.ToString()) };
if (itemInfo.answer.First().Equals("√") || itemInfo.answer.First().Equals("×")
|| itemInfo.answer.First().Equals("对") || itemInfo.answer.First().Equals("错")
|| itemInfo.answer.First().Equals("正确") || itemInfo.answer.First().Equals("错误"))
{
itemInfo.objective = true;
itemInfo.type = "judge";
itemInfo.opts = 2;
itemInfo.option = new List() { new CodeValue { code = "A", value = "对" }, new CodeValue { code = "B", value = "错" } };
itemInfo.answer = itemInfo.answer.First().Equals("√") || itemInfo.answer.First().Equals("对") || itemInfo.answer.First().Equals("正确") ? new List { "A" } : new List { "B" };
}
}
itemInfos.Add(itemInfo);
}
});
HashSet gradeNames = new HashSet();
gradeNames = itemInfos.SelectMany(x => x.gradeNames).ToHashSet();
// await _dingDing.SendBotMsg($"学科网推送消息:{authCode.ToJsonString()}", GroupNames.成都开发測試群組);
return Ok(new { xkwPapers, periodName = course.stage_name, subjectName = course.subject_name, gradeNames, knowledge = xkw_points.Select(x => x.name), itemInfos, paper });
} catch (Exception ex) {
if (ex.Message.Contains("客户已关闭") || ex.Message.Contains("403")||ex.Message.Contains("参数无效"))
{
return Ok(new { error = 403, message="学科网该账号授权已到期!" });
}
else
{
await _dingDing.SendBotMsg($"学科网组卷错误:{ex.Message}\n{ex.StackTrace}\n{json.ToJsonString()}", GroupNames.成都开发測試群組);
return BadRequest($"学科网组卷错误:{ex.Message}\n{ex.StackTrace}\n{json.ToJsonString()}");
}
}
}
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;
}
public (List options, string question) OptionProcess(string question) {
string Options = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string[] optionsKeys = Options.Select(s => s.ToString()).ToArray();
for (int idx = 0; idx < 26; idx++)
{
question = question.Replace(aza[idx], azh[idx]);
}
List options = new List();
string optsRgex = optionsKeys[0] + "\\s*(\\.|\\.|\\、|\\:|\\:)([\\s\\S]*?).*"; ;
string optsHtml = Regex.Match(question, optsRgex).Value;
StringBuilder textImg = new StringBuilder();
for (int i = 0; i < optionsKeys.Length - 1; i++)
{
string optRgex = optionsKeys[i] + "\\s*(\\.|\\.|\\、|\\:|\\:)([\\s\\S]*?)" + optionsKeys[i + 1] + "\\s*(\\.|\\.|\\、|\\:|\\:)";
string optHtml = Regex.Match(optsHtml, optRgex).Value;
if (string.IsNullOrWhiteSpace(optHtml))
{
optRgex = optionsKeys[i] + "\\s*(\\.|\\.|\\、|\\:|\\:).*";
optHtml = Regex.Match(optsHtml, optRgex).Value;
}
if (!string.IsNullOrEmpty(optHtml))
{
optHtml = Regex.Replace(optHtml, optionsKeys[i + 1] + "\\s*(\\.|\\.|\\、|\\:|\\:)", "");
optHtml = optHtml.Substring(2, optHtml.Length - 2);
optHtml = HtmlHelper.DoUselessTag(optHtml);
optHtml = optHtml.TrimStart().TrimEnd();
textImg.Append(HtmlHelper.DoTextImg(optHtml));
string table_pattern = "]{0,})>";
optHtml = Regex.Replace(optHtml, table_pattern, "").Replace("
", "").Replace("", "").Replace("", "").Replace(" | ", "").Replace("
", "");
options.Add(new CodeValue { code = optionsKeys[i], value = optHtml });
}
}
if (!string.IsNullOrWhiteSpace(optsHtml))
{ question = question.Replace(optsHtml, "").Replace("", "");
return (options, question);
}
else
{
question= question.Replace("","");
return (new List(), question);
}
}
}
}
|
|