Greasy Fork is available in English.

[bot]ログ型bot

ログ型bot用の学習データを使用してください。

// ==UserScript==
// @name         [bot]ログ型bot
// @version      2.5
// @description  ログ型bot用の学習データを使用してください。
// @match        *.x-feeder.info/*/
// @match        *.x-feeder.info/*/sp/
// @exclude      *.x-feeder.info/*/settings/**
// @match        http://drrrkari.com/room/
// @match        http://www.3751chat.com/ChatRoom*
// @match        https://pictsense.com/*
// @match        http://www.himachat.com/
// @match        https://discordapp.com/*
// @require      https://greasyfork.org/scripts/387509-yaju1919-library/code/yaju1919_library.js?version=722818
// @require      https://greasyfork.org/scripts/387372-chatbot/code/Chatbot.js?version=720412
// @require      https://greasyfork.org/scripts/387085-chatbottypeoflog/code/ChatbotTypeOfLog.js?version=714385
// @require      https://greasyfork.org/scripts/387084-readbigtextforeachline/code/readBigTextForEachLine.js?version=714202
// @require      https://cdnjs.cloudflare.com/ajax/libs/encoding-japanese/1.0.29/encoding.min.js
// @grant        GM.setValue
// @grant        GM.getValue
// @namespace https://greasyfork.org/users/263519
// ==/UserScript==
(()=>{
    'use strict';
    const yaju1919 = yaju1919_library;
    const Measure = (()=>{ // 処理時間計測
        let readed_line = 0;
        let learned_line = 0;
        let readed_log = 0;
        const count_read = () => {readed_line++};
        const count_learn = () => {learned_line++};
        const show = () => {
            const ar = [];
            ar.push("読込済み行:"+readed_line);
            const readed_sub = readed_line - readed_log;
            readed_log = readed_line;
            ar.push("読込速度[行/s]:" + readed_sub);
            ar.push("学習済み行:" + learned_line);
            return ar;
        };
        return {count_read: count_read, count_learn: count_learn, show: show};
    })();
    const removeDecorationTags = (str) => { // feeder特有の装飾を消す
        return str
            .replace(/>>[0-9]+/g, '')
            .replace(/\'\'\'/g, '')
            .replace(/\'\'/g, '')
            .replace(/%%%/g, '')
            .replace(/%%/g, '')
            .replace(/\{0:((.|\n)*?)\}/g, '') // 0サイズ文字は消す
            .replace(/\{([0-9]{1,2}):((.|\n)*?)\}/g, '$2')
            .replace(/\{(#[0-9A-F]{6}):((.|\n)*?)\}/g, '$2')
            .replace(/\{([0-9]{1,2}):?/g, '')
            .replace(/\{(#[0-9A-F]{6}):?/g, '')
    };
    const shapeStr = str => {
        const s = removeDecorationTags(removeDecorationTags(str.trim()));
        if(!s.replace(/\s+/,'')) return null;
        return s;
    };
    const MakeKeyword = (str) => { // 文字列をキーワードに変換
        const s = shapeStr(str);
        if(!s) return null;
        return s
            .replace(/(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/g,"[#url#]")
            .replace(/\[([PEFQ]):[0-9]+\]/g, "[#$1#]");
    };
    let NG_words = [];
    const ReadFileCallback = line => {
        Measure.count_read();
        const unicodeString = Encoding.convert(line, {
            to: 'UNICODE',
            from: 'UTF8',
            type: 'string' // 文字列 'string' を指定 (string で返ります)
        });
        const array = unicodeString.split(',');
        if(array.length !== 2) return;
        const keyword = array[0].trim().replace(/,/g,',');
        const answer = array[1].trim().replace(/,/g,',');
        for(const v of NG_words){
            if(keyword.indexOf(v) !== -1) return;
        }
        ChatbotTypeOfLog.input(keyword, answer);
        Measure.count_learn();
    };
    const setConfig = () => {
        const h = $("<div>");
        const f = yaju1919.appendInputNumber(h,{
            title: "精度",
            explain: "0~(任意)",
            value: 6,
            min: 0,
            save: "精度",
            change: n => ChatbotTypeOfLog.config(n)
        });
        ChatbotTypeOfLog.config(f());
        const read_file_elm = $("<input>",{
            type: 'file',
            multiple: true
        }).change(evt=>{
            const files = evt.target.files;
            for(const f of files) {
                readBigTextForEachLine(f, line => ReadFileCallback(line));
            }
        });
        const learn_btn = $("<button>",{text:"ログ型bot用の学習データから学習する"})
        .appendTo(h).click(()=>read_file_elm.click());
        $("<textarea>",{
            placeholder:"NGワードを改行で区切って入力(その単語が含まれている文は学習しない)\nファイルを読み込む前に先に入力しよう"
        })
        .appendTo(h).width("100%")
        .keyup(function(){$(this).height($(this).val().split('\n').length + 2 + 'em')})
        .change(function(){
            NG_words = $(this).val().split(/\n+/).filter(v=>v);
        });
        measure_elm = $("<div>").append(measure_log).appendTo(h);
        return h;
    };
    let measure_log, measure_elm;
    setInterval(()=>{
        if(!measure_elm) return;
        const now = Measure.show().join('<br>');
        if(now === measure_log) return;
        measure_log = now;
        measure_elm.empty().append(now);
    },1000);
    win.Chatbot["ログ型"] = {
        main: ({text}) => ChatbotTypeOfLog.output(MakeKeyword(text)),
        config: setConfig,
        priority: 5
    };
})();