CHM帮助文件转换为AI知识库可用的Html

一、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就可以使用了。

八、修改\eingeering\html\下的chmviewer.aspx.cs及default.aspx中的相应内容,如下所示,是通过目录下的catalog.txt文件来显示内容的,该文件可通过本人的采集程序生成。

        { "hainan-2025-quota-docs", "海南省2025预算定额章节说明" },
        { "hainan-2023-quota-docs", "海南省2023概算定额章节说明及文件汇编" },
        { "hainan-2017-quota-docs", "海南省2017定额章节说明" }, // Ensure this directory name matches exactly
        { "guangdong-2018-quota-docs", "广东省2018定额章节说明" },
        { "guangxi-2024-quota-docs", "广西清单定额说明及计算规则" }, // Original name was "广西2024定额章节说明及文件汇编" - ensure consistency
        { "jiangsu-2014-quota-docs", "江苏定额说明" },
        { "jiangsu-municipal-2021-quota-docs", "江苏省市政工程消耗量定额(2021年)章节说明" },
        { "zhejiang-2018-quota-docs", "浙江省2018序列定额章节说明" },
        { "zhejiang-policy-docs", "浙江省政策性文件汇总" },
        { "hubei-2024-quota-docs", "湖北2024序列定额章节说明" }, // Original name was "湖北省2024序列定额章节说明"
        { "henan-2016-quota-docs", "河南2016序列定额章节说明" },
        { "beijing-housing-repair-2023-calc-docs", "北京房屋修缮工程工程量计算标准(2023)说明" },
        { "beijing-housing-repair-2021-budget-docs", "北京市房屋修缮工程计价依据预算消耗量标准(2021)" },
        { "beijing-construction-2021-budget-docs", "北京市建设工程计价依据预算消耗量标准(2021)" },

        { "highway-2018-compilation-docs", "2018公路系列定额编制办法及说明" },
        { "inland-shipping-2019-budget-quota", "内河航运工程预算定额(2019)" },
        { "dredging-2019-budget-quota", "疏浚工程预算定额(2019)" },
        { "coastal-port-hydraulic-2019-quota", "沿海港口水工建筑工程定额(2019)" },
        { "distribution-network-20kv-2022-docs", "20kV及以下配电网工程预规及定额章节说明(2022年版)" },
            { "hydropower-budget-quota-docs-2024", "水电工程概预算定额说明文档(2024)" },
{ "water-conservancy-budget-rules-docs-2024", "水利概预算工程及编规说明文档" },
{ "soil-water-conservation-estimate-quota-docs-2024", "水土保持工程概算定额(2024)说明文档" },

0

评论0

请先
DG/TJ 08-2422-2023 节约集约建设用地标准.pdf
DG/TJ 08-2422-2023 节约集约建设用地标准.pdf
9分钟前 有人购买 去瞅瞅看
没有账号?注册  忘记密码?

社交账号快速登录

微信扫一扫关注
扫码关注后会自动登录网站