npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tal-mathgpt-struct

v0.0.1-beta

Published

mathgpt 解析结构化

Readme

tal-mathgpt-struct

一个用于结构化三段式/随时问格式解析的 JavaScript 工具包。

安装

npm install tal-mathgpt-struct

或者使用 yarn:

yarn add tal-mathgpt-struct

使用方法

基本用法

import getStructRes from 'tal-mathgpt-struct';

// 简单示例
const text = '【分析】这是分析内容【详解】这是详解内容【点睛】这是点睛内容【答案】这是答案内容';
const result = getStructRes(text);

console.log(result);
// 输出:
// {
//   analysis: '这是分析内容',
//   explanations: '这是详解内容',
//   point: '这是点睛内容',
//   answer: '这是答案内容'
// }

解析步骤

const text = '【分析】分析内容【详解】【1】步骤一内容【2】步骤二内容【3】步骤三内容【点睛】点睛内容';
const result = getStructRes(text);

console.log(result.explanations);
// 输出:
// {
//   steps: [
//     { step: '1', content: '步骤一内容' },
//     { step: '2', content: '步骤二内容' },
//     { step: '3', content: '步骤三内容' }
//   ]
// }

解析多问题场景

const text = `【分析】整体分析
【详解】
【问题1】
【1】问题1的第一步
【2】问题1的第二步
【问题2】
【1】问题2的第一步
【2】问题2的第二步
【点睛】重点提示
【答案】最终答案`;

const result = getStructRes(text);

console.log(result.explanations);
// 输出:
// {
//   questions: [
//     {
//       title: '问题1',
//       steps: [
//         { step: '1', content: '问题1的第一步' },
//         { step: '2', content: '问题1的第二步' }
//       ]
//     },
//     {
//       title: '问题2',
//       steps: [
//         { step: '1', content: '问题2的第一步' },
//         { step: '2', content: '问题2的第二步' }
//       ]
//     }
//   ]
// }

API 文档

getStructRes(str)

主函数,用于解析和结构化文本。

参数:

  • str (String): 需要结构化的字符串

返回值:

  • (Object): 结构化后的对象

支持的标记:

  • 【分析】: 解析为 analysis 字段
  • 【详解】: 解析为 explanations 字段
  • 【点睛】: 解析为 point 字段
  • 【答案】: 解析为 answer 字段

详解部分的特殊处理:

  • 如果详解部分包含步骤标记(如 【1】【2】),会被解析为步骤数组
  • 如果详解部分包含问题标记(如 【问题1】),会被解析为问题数组,每个问题包含其对应的步骤

示例场景

场景一:基础解析

const text = '【分析】这是分析【详解】这是详解【点睛】这是点睛【答案】这是答案';
const result = getStructRes(text);

场景二:带步骤的解析

const text = '【分析】分析内容【详解】【1】第一步【2】第二步【点睛】点睛内容';
const result = getStructRes(text);

场景三:多问题解析

const text = '【分析】分析内容【详解】【问题一】【1】步骤1【问题二】【1】步骤1【点睛】点睛';
const result = getStructRes(text);

场景四:在 React 组件中使用

import React from 'react';
import getStructRes from 'tal-mathgpt-struct';

function MathSolution({ solutionText }) {
  const structuredData = getStructRes(solutionText);

  return (
    <div className="math-solution">
      {structuredData.analysis && (
        <section className="analysis">
          <h3>分析</h3>
          <p>{structuredData.analysis}</p>
        </section>
      )}

      {structuredData.explanations && (
        <section className="explanations">
          <h3>详解</h3>
          {structuredData.explanations.steps && (
            <ol>
              {structuredData.explanations.steps.map((step, index) => (
                <li key={index}>{step.content}</li>
              ))}
            </ol>
          )}

          {structuredData.explanations.questions && (
            <div>
              {structuredData.explanations.questions.map((question, qIndex) => (
                <div key={qIndex} className="question">
                  <h4>{question.title}</h4>
                  <ol>
                    {question.steps.map((step, sIndex) => (
                      <li key={sIndex}>{step.content}</li>
                    ))}
                  </ol>
                </div>
              ))}
            </div>
          )}
        </section>
      )}

      {structuredData.point && (
        <section className="point">
          <h3>点睛</h3>
          <p>{structuredData.point}</p>
        </section>
      )}

      {structuredData.answer && (
        <section className="answer">
          <h3>答案</h3>
          <p>{structuredData.answer}</p>
        </section>
      )}
    </div>
  );
}

export default MathSolution;

场景五:处理带有 LaTeX 公式的文本

import getStructRes from 'tal-mathgpt-struct';
import renderMathInElement from 'katex/dist/contrib/auto-render';

// 假设文本中包含 LaTeX 公式
const text = '【分析】函数 $f(x) = x^2$ 的导数是 $f\'(x) = 2x$【详解】【1】利用导数公式 $\\frac{d}{dx}x^n = nx^{n-1}$【2】代入 $n=2$ 得到 $f\'(x) = 2x$【点睛】幂函数求导要用幂的导数公式';

// 先结构化文本
const result = getStructRes(text);

// 然后可以在 DOM 中渲染并处理 LaTeX 公式
document.getElementById('analysis').innerHTML = result.analysis;
renderMathInElement(document.getElementById('analysis'));

// 对于步骤也可以类似处理
if (result.explanations && result.explanations.steps) {
  const stepsContainer = document.getElementById('steps');
  result.explanations.steps.forEach(step => {
    const stepElement = document.createElement('li');
    stepElement.innerHTML = step.content;
    stepsContainer.appendChild(stepElement);
    renderMathInElement(stepElement);
  });
}

常见问题解答

如何处理标记不完整的情况?

如果输入文本中缺少某些标记,函数会正常处理已有的标记,缺失的部分不会出现在结果对象中。

const text = '【分析】只有分析部分【答案】只有答案部分';
const result = getStructRes(text);
// 结果只包含 analysis 和 answer 字段

如何处理嵌套的标记?

本库设计为处理顶层标记(分析、详解、点睛、答案)和详解部分内的步骤和问题标记。如果有更复杂的嵌套,可能需要对结果进行进一步处理。

是否支持自定义标记?

当前版本不支持自定义标记。如果需要支持自定义标记,可以考虑 fork 项目并修改源代码中的 keyMap 对象。

贡献指南

欢迎对本项目进行贡献!以下是贡献的步骤:

  1. Fork 本仓库
  2. 创建你的特性分支 (git checkout -b feature/amazing-feature)
  3. 提交你的更改 (git commit -m 'Add some amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 开启一个 Pull Request

开发环境设置

# 克隆仓库
git clone https://github.com/yourusername/tal-mathgpt-struct.git

# 安装依赖
cd tal-mathgpt-struct
npm install

# 运行测试
npm test

# 构建项目
npm run build

代码风格

本项目使用 ESLint 进行代码风格检查。请确保你的代码符合项目的代码风格规范。

许可证

MIT