您的足迹:首页 > 我的代码 >5W标题5W关键字批量替换代码 , 只要30多秒(AARDIO代码)

5W标题5W关键字批量替换代码 , 只要30多秒(AARDIO代码)

代码不是太精简,但是作用已经很明显了。最普通的forin循环需要二千多秒,这样就知道这代码的作用了吧?

废话不说了,直接上代码,复制完直接运行,自己找文件看效果。

 

import win.ui;
/*DSG{{*/
mainForm = win.form(text="批量过滤关键词";right=415;bottom=183;max=false)
mainForm.add(
btn_filter={cls="button";text="浏览";left=336;top=64;right=400;bottom=89;z=7};
btn_save={cls="button";text="浏览";left=336;top=104;right=400;bottom=129;z=10};
btn_source={cls="button";text="浏览";left=336;top=24;right=400;bottom=49;z=4};
btn_start={cls="button";text="开始";left=168;top=136;right=234;bottom=171;z=11};
progress2={cls="progress";left=16;top=136;right=400;bottom=166;edge=1;hide=1;max=100;min=0;z=1};
static={cls="static";text="原始数据:";left=16;top=24;right=90;bottom=48;transparent=1;z=2};
static2={cls="static";text="过滤数据:";left=16;top=64;right=90;bottom=86;transparent=1;z=5};
static3={cls="static";text="保存结果:";left=16;top=104;right=90;bottom=126;transparent=1;z=8};
txt_filter={cls="edit";left=80;top=64;right=322;bottom=86;edge=1;z=6};
txt_save={cls="edit";left=80;top=104;right=322;bottom=126;edge=1;z=9};
txt_source={cls="edit";left=80;top=24;right=322;bottom=46;edge=1;z=3}
)
/*}}*/

import fsys.dlg;
import thread.command;

var listener = thread.command();
listener.initProgress = function(...) {
    mainForm.progress2.max = ...;
}
listener.stepProgress = function() {
    mainForm.progress2.stepIt();
}

var works = function(source_src, filter_src, save_src) {
    import console; //debug
    import thread.command;

    //切割过滤字符为2维1元树
    var filter = string.load(filter_src);
    filter = string.fromto(filter,0,65001);
    var filter_table = string.splitEx(filter);
    filter = null;
    var filter_hash_table = {};
    for(_,v in filter_table){
        var k = string.left(v, 1, true);
        if (filter_hash_table[k] == null) {
            filter_hash_table[k] = {};
        }
        table.push(filter_hash_table[k], v);
    }
    filter_table = null;

    var source = string.load(source_src);
    source = string.fromto(source,0,65001);
    var source_table = string.splitEx(source);
    //console.log(table.tostring(source_table));

    //分行处理
    var result_table = {};
    thread.command.initProgress(#source_table);
    for(_,txt in source_table){
        var line_table = string.split(txt);
        //分字处理
        var result = "";
        for(i=1;#line_table;1){
            //没有索引的直接加入结果
            if (filter_hash_table[line_table[i]] !== null) {
                var filter_table = filter_hash_table[line_table[i]];
                //遍历判断是否有匹配字符
                var has_match = false;
                for(_,v in filter_table){
                    var len = string.len(v);
                    var s = string.left(txt,len,true);
                    if (s == v) {
                        i = i + len -1;
                        has_match = true;
                        break;
                    }
                }
                if (has_match == false) {
                    result = result ++ line_table[i];
                }
            } else {
                result = result ++ line_table[i];
            }
        }
        table.push(result_table, result);
        thread.command.stepProgress();
    }

    string.save(save_src, string.fromto(string.join(result_table,'\r\n'),65001,0));
}

mainForm.btn_start.oncommand = function(id,event){
    if (mainForm.txt_source.text == '') {
        mainForm.msgbox('请设置原始数据文件路径');
        return ;
    }
    if (mainForm.txt_filter.text == '') {
        mainForm.msgbox("请设置过滤数据文件路径");
        return;
    }
    if (mainForm.txt_save.text == '') {
        mainForm.msgbox("请设置保存数据文件路径");
        return;
    }

    //disable_btn
    mainForm.btn_source.disabled = true;
    mainForm.btn_filter.disabled = true;
    mainForm.btn_save.disabled = true;
    mainForm.btn_start.disabled = true;
    mainForm.btn_start.hide = true;
    mainForm.progress2.hide = false;

    var start_time = tonumber(time());

    var working = thread.create(works, mainForm.txt_source.text, mainForm.txt_filter.text, mainForm.txt_save.text);
    thread.waitOne(working);

    var spend_time = tonumber(time()) - start_time;
    mainForm.msgbox("完成! 耗时:"+spend_time+"秒");

    //enable_btn
    mainForm.btn_source.disabled = false;
    mainForm.btn_filter.disabled = false;
    mainForm.btn_save.disabled = false;
    mainForm.btn_start.disabled = false;
    mainForm.progress2.hide = true;
    mainForm.btn_start.hide = false;
}

mainForm.btn_save.oncommand = function(id,event){
    mainForm.txt_save.text = fsys.dlg.save('文本文件|*.txt|所有文件|*.*|');
}

mainForm.btn_filter.oncommand = function(id,event){
    mainForm.txt_filter.text = fsys.dlg.open('文本文件|*.txt|所有文件|*.*|', '选择过滤数据文件');
}

mainForm.btn_source.oncommand = function(id,event){
    mainForm.txt_source.text = fsys.dlg.open('文本文件|*.txt|所有文件|*.*|', '选择原始数据文件');
}

mainForm.show() 
//调试
/*
mainForm.txt_source.text = "D:\test\标题.txt";
mainForm.txt_filter.text = "D:\test\品牌.txt";
mainForm.txt_save.text = "D:\test\save.txt";
*/

return win.loopMessage(); 

 ===============================================

最新进展,又有了新版本,整齐了一些。。而且23秒就可以搞定了。超强。 

//过滤关键字
//5W+只需23秒就搞定了
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="button";left=536;top=336;right=720;bottom=432;z=1}
)
/*}}*/

import console;

//来源文档转换成表
Str2Table = function(str){
    var tbTemp = string.splitEx(str);
    var tbFrom = {};
    for i=1;#tbTemp {
        table.push(tbFrom,tbTemp[i]);
    }
    return tbFrom;  
}

//关键字转换成表
Key2Table = function(str){
    var tbTemp = string.splitEx(str);
    var tbKey = {};
    for i=1;#tbTemp {
        var first = string.left(tbTemp[i],1,true);
        if tbKey[first] {
            table.push(tbKey[first],tbTemp[i]);
        }
        else {
            tbKey[first] = {tbTemp[i]};
        }
    }
    return tbKey;   
}

//处理字符串
ReplaceStr = function(str,tbKey){
    var szResult = "";
    var tb = string.split(str);
    for i=1;#tb {
        var key = tb[i];
        var IsMatch = false;
        if tbKey[key] {
            for _,szKey in tbKey[key] {
                var len = string.len(szKey);
                if string.sub(str,i,len,true) == szKey {
                    i = i + len - 1;
                    IsMatch = true;
                    break ;
                }
            }
        }
        if not IsMatch {
            szResult = szResult ++ tb[i];
        }
    }
    return szResult; 
}

Replace = function(tbFrom,tbKey){
    var tbResult = {};
    for _,str in tbFrom {
        var szResult = ReplaceStr(str,tbKey);
        table.push(tbResult,szResult);
    }
    return string.join(tbResult,'\r\n'); 
}

winform.button.oncommand = function(id,event){
    var tm = time.now();
    var str = string.load("\标题.txt");
    str = string.fromto(str,0,65001);
    var str2 = string.load("\品牌.txt");
    str2 = string.fromto(str2,0,65001); 
    var tbFrom = Str2Table(str);
    var tbKey = Key2Table(str2);
    var szSave = Replace(tbFrom,tbKey);
    string.save("\test.txt",szSave);
    var tm2 = time.now();
    console.log(tonumber(tm2)-tonumber(tm));
}

winform.show() 
win.loopMessage();

【感谢 怪才&鹰 二位奉献自己代码】

 

本博客所有文章如无特别注明均为原创。作者:恶猫复制或转载请以超链接形式注明转自 恶猫的博客
原文地址《5W标题5W关键字批量替换代码 , 只要30多秒(AARDIO代码)

相关推荐

分享本文至:

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(0)

恶猫的博客 -记录自己日常,代码,美图,电影,音乐,新闻,只是个人博客而已

浙ICP备15011757号-4 网站地图 联系我