qq_39483237 2023-11-12 16:33 采纳率: 0%
浏览 6
已结题

在油猴脚本中用讯飞的中文分词服务

我的油猴脚本需要用中文分词,目前用的是Chrome自带的Intl.Segmenter
const str_separated = Array.from(new Intl.Segmenter('cn', { granularity: 'word' }).segment(str));
但是感觉准确率特别低,很多常见词汇都分不出来。

所以我试了下讯飞的在线分词:

function sending_xml(input_text) {
    // const input_text = "你好";
    const XAppid = "..."
    const APIKey = "...";
    const XCurTime = Math.floor(new Date().getTime() / 1000);
    const XParam = btoa(JSON.stringify({"type": "dependent"}));
    const XCheckSum = md5(APIKey + XCurTime + XParam);
    const headers = {
            "Content-Type": "application/json",
            "X-Appid": XAppid,
            "X-CurTime": XCurTime,
            "X-Param":XParam,
            "X-CheckSum": XCheckSum
    };
    var data = {};
    data.text = input_text;
    GM_log(data);
    GM.xmlHttpRequest ({
        method:     "POST",
        url:        "https://ltpapi.xfyun.cn/v1/cws",
        data:       JSON.stringify(data),
        headers:    headers,
        onload:     function (response) {
            if (response.status === 200) {
                GM_log(response.responseText);
            } else {
                GM_log("Error: " + response.statusText);
            }
        }
    });
}

结果是:
{"code":"10109","data":{},"desc":"illegal text length","sid":"..."}

请问饿为什么是illegal text length?这个请求哪里不对?

  • 写回答

12条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-11-12 23:07
    关注
    获得0.60元问题酬金

    引自强大而免费的微信小程序:皆我百晓生

    问题分析:
    根据错误提示 "illegal text length",可以得出是输入的文本长度不合法导致的错误。接口文档中可能对文本长度有限制,所以需要检查一下文本的长度是否符合要求。

    解决方案:

    1. 检查文本的长度是否超出限制。
    2. 确保请求头参数正确设置。

    代码实现:

    function sending_xml(input_text) {
        const XAppid = "..."; // 填写讯飞应用ID
        const APIKey = "..."; // 填写讯飞应用API密钥
        const XCurTime = Math.floor(new Date().getTime() / 1000);
        const XParam = btoa(JSON.stringify({ "type": "dependent" }));
        const XCheckSum = md5(APIKey + XCurTime + XParam);
        const headers = {
            "Content-Type": "application/json",
            "X-Appid": XAppid,
            "X-CurTime": XCurTime,
            "X-Param": XParam,
            "X-CheckSum": XCheckSum
        };
        if (input_text.length <= 512) { // 检查文本长度是否超出限制
            var data = {};
            data.text = input_text;
            GM_log(data);
            GM.xmlHttpRequest({
                method: "POST",
                url: "https://ltpapi.xfyun.cn/v1/cws",
                data: JSON.stringify(data),
                headers: headers,
                onload: function (response) {
                    if (response.status === 200) {
                        GM_log(response.responseText);
                    } else {
                        GM_log("Error: " + response.statusText);
                    }
                }
            });
        } else {
            GM_log("Text length exceeds the limit. Please make sure the input text is within 512 characters.");
        }
    }
    

    注意事项:

    • 请确保讯飞应用ID(XAppid)和API密钥(APIKey)的填写正确。
    • 限制了文本长度的判断是根据接口文档提供的512个字符作为示例,如果实际文本长度限制不同,请根据实际情况进行调整。
    • 这段代码只是请求接口和处理返回的示例,并没有包含具体的中文分词处理逻辑,请根据需要自行补充代码。
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 11月20日
  • 修改了问题 11月12日
  • 赞助了问题酬金15元 11月12日
  • 修改了问题 11月12日
  • 展开全部

悬赏问题

  • ¥15 Windows Script Host 无法找到脚本文件"C:\ProgramData\Player800\Cotrl.vbs”
  • ¥15 matlab自定义损失函数
  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图