• 字符分类器-海明分类器


    海明分类器 是给字符分类,应用场景有 url短链,可以给任意字符生成一个短链

    依赖md5.js

    function string10to62(number) {
        var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ'.split(''),
    radix = chars.length,
    qutient = +number,
    arr = [];
    do {
    mod = qutient % radix;
    qutient = (qutient - mod) / radix;
    arr.unshift(chars[mod]);
    } while (qutient);
    return arr.join('');
    }
    function string62to10(number_code) {
    var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ',
    radix = chars.length,
    number_code = String(number_code),
    len = number_code.length,
    i = 0,
    origin_number = 0;
    while (i < len) {
    origin_number += Math.pow(radix, i++) * chars.indexOf(number_code.charAt(len - i) || 0);
    }
    return origin_number;
    }
    function string10to63(number) {
    var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ,'.split(''),
    radix = chars.length,
    qutient = +number,
    arr = [];
    do {
    mod = qutient % radix;
    qutient = (qutient - mod) / radix;
    arr.unshift(chars[mod]);
    } while (qutient);
    return arr.join('');
    }
    function string63to10(number_code) {
    var chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ,',
    radix = chars.length,
    number_code = String(number_code),
    len = number_code.length,
    i = 0,
    origin_number = 0;
    while (i < len) {
    origin_number += Math.pow(radix, i++) * chars.indexOf(number_code.charAt(len - i) || 0);
    }
    return origin_number;
    }
    //海明分类器
    function HammingTag(s) {
    var s1=hex_md5(s)
    var rs='552cb8e71eea229fd1ea8ac0010057e6';//作者的md5
    let n='';
    for(var i=0;i<s1.length;i++){
    n=n+(s1[i]===rs[i]?'0':'1');
    }

    return parseInt(n,2);
    }


    
    
    //demo
    const tag=HammingTag(1);

    const id=string63to10(string10to62(tag)+',3')
    const key=string10to62(id);


    console.log(id)
    console.log(key)
    const nid=string62to10(key);
    const arr=string10to63(nid).split(',').map(function (str) {
    return string62to10(str)
    })
    console.log(arr)
  • 相关阅读:
    mysql高级之编程优化
    高性能产品必由之路
    linux下安装xhprof
    linux下安装apc
    linux下安装vld
    python装饰器通俗易懂的解释!
    python函数基础 与文件操作
    python基础入门一(语法基础)
    iOS Keychain,SSKeychain,使用 理解 原理
    起头
  • 原文地址:https://www.cnblogs.com/caoke/p/11872870.html
Copyright © 2020-2023  润新知