一、windows命令行实现chm帮助文件,转换为html文件,方法是:
D:\1>hh -decompile d:\1\html d:\1\AutoIt.chm
需要注意的是 :
d:\1\AutoIt.chm 这是chm绝对路径,必须是绝对路径
hh -decompile 是固定的
d:\1\html 这个目录是html存放的目标目录,必须是绝对路径;
二、html文件已可用,但其中的链接为JS生成,不方便AI处理。使用程序转换为AI容易抓取的html,如下:
private void button34_Click(object sender, EventArgs e) { //使用示例: OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Html Files|*.htm", Title = "请选择一个Html文件" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { var converter = new HtmlLinkConverter(); // 读取文件内容 string filePath = openFileDialog.FileName; string inputHtml = File.ReadAllText(filePath, Encoding.GetEncoding("GB2312")); string outputHtml = converter.ConvertToStaticLinks(inputHtml); // 保存处理后的内容 SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "Html Files|*.htm", Title = "保存处理后的文件", FileName = Path.GetFileNameWithoutExtension(filePath) + "_processed.htm" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { File.WriteAllText(saveFileDialog.FileName, outputHtml, Encoding.UTF8); MessageBox.Show("文件已成功保存!", "保存成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } public class HtmlLinkConverter { public string ConvertToStaticLinks(string htmlContent) { // 提取所有d.add节点 var nodes = ParseDTreeNodes(htmlContent); // 构建树结构 var nodeMap = BuildNodeMap(nodes); // 生成静态HTML列表 var staticContent = GenerateStaticHtml(nodeMap); // 替换原始动态内容 return ReplaceDynamicContent(htmlContent, staticContent); } private List<DTreeNode> ParseDTreeNodes(string html) { var nodes = new List<DTreeNode>(); var regex = new Regex(@"d\.add\((\d+),\s*(-?\d+),\s*""(.*?)"",\s*""(.*?)""\);"); foreach (Match match in regex.Matches(html)) { nodes.Add(new DTreeNode( int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value), match.Groups[3].Value, match.Groups[4].Value )); } return nodes; } private Dictionary<int, List<DTreeNode>> BuildNodeMap(List<DTreeNode> nodes) { var nodeMap = new Dictionary<int, List<DTreeNode>>(); foreach (var node in nodes) { if (!nodeMap.ContainsKey(node.ParentId)) nodeMap[node.ParentId] = new List<DTreeNode>(); nodeMap[node.ParentId].Add(node); } return nodeMap; } private string GenerateStaticHtml(Dictionary<int, List<DTreeNode>> nodeMap) { var sb = new StringBuilder(); sb.AppendLine("<ul>"); GenerateTree(nodeMap, -1, sb, 0); // 从根节点开始(父ID=-1) sb.AppendLine("</ul>"); return sb.ToString(); } private void GenerateTree(Dictionary<int, List<DTreeNode>> nodeMap, int parentId, StringBuilder sb, int level) { if (!nodeMap.ContainsKey(parentId)) return; foreach (var node in nodeMap[parentId]) { var indent = new string(' ', level * 4); sb.Append($"{indent}<li>"); if (!string.IsNullOrEmpty(node.Url)) { sb.Append($"<a href=\"{node.Url}\">{node.Name}</a>"); } else { sb.Append(node.Name); } if (nodeMap.ContainsKey(node.Id)) { sb.AppendLine("\n" + indent + " <ul>"); GenerateTree(nodeMap, node.Id, sb, level + 1); sb.AppendLine(indent + " </ul>"); } sb.AppendLine("</li>"); } } private string ReplaceDynamicContent(string originalHtml, string staticContent) { // 移除所有JavaScript代码 var regex = new Regex( @"<script[^>]*>[\s\S]*?</script>", RegexOptions.Multiline ); var cleanedHtml = regex.Replace(originalHtml, string.Empty); // 替换动态生成部分 var dynamicRegex = new Regex( @"<div class=""dtree"">[\s\S]*?</div>", RegexOptions.Singleline ); return dynamicRegex.Replace(cleanedHtml, staticContent); } } public class DTreeNode { public int Id { get; } public int ParentId { get; } public string Name { get; } public string Url { get; } public DTreeNode(int id, int parentId, string name, string url) { Id = id; ParentId = parentId; Name = name; Url = url; } }
使用上面的程序处理___left.htm文件,主htm文件类似下面并另存为utf-8的格式:
<HTML>
<HEAD>
<meta charset="gb2312">
<TITLE>海南省2023定额章节说明及文件汇编.chm</TITLE>
</HEAD>
<FRAMESET border=4 cols="100%">
<FRAME name=left marginWidth=5 marginHeight=5 src="海南省2023定额章节说明及文件汇编\___left.htm">
</FRAMESET>
</HTML>
因为早期微软的一些原因,CHM的编码并不是统一的,一般中文的是gb2312,实际编码为ansi,需要将___left.htm等文件另存为utf-8编码,并在文件头中加上下面的标识,程序才可以正确识别。
<meta charset="gb2312">
三、替换你不想要的一些版权信息,可以直接替换或使用程序都可。
四、上传上面处理的html文件到网站某个文件夹,包含所有文件及图片等。
五、使用https://calc.xycost.com/Chatgpt/ScrapeWeb.aspx来抓取相应的链接,如果链接含中文,也可用ai来生成列表
六、AI知识库-导入文本数据集-网站链接,粘贴上面生成的链接即可。
七、使用嵌入模型来进行向量化,然后AI就可以使用了。
评论0