zoukankan      html  css  js  c++  java
  • 字符分类器-海明分类器

    海明分类器 是给字符分类,应用场景有 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)
  • 相关阅读:
    007 使用SpringMVC开发restful API五--异常处理
    006 使用SpringMVC开发restful API四--用户信息的修复与删除,重在注解的定义
    033 Url中特殊字符的处理
    005 使用SpringMVC开发restful API三--处理创建请求
    004 使用SpringMVC开发restful API二--编写用户详情
    003 使用SpringMVC开发restful API--查询用户
    032 报错
    018 easygui的使用
    017 异常的处理
    012 pandas与matplotlib结合制图
  • 原文地址:https://www.cnblogs.com/caoke/p/11872870.html
Copyright © 2011-2022 走看看