检测胡言乱语的JavaScript脚本

检测胡言乱语的JavaScript脚本,javascript,Javascript,我构建了一个应用程序来建议电子邮件地址修复,我需要检测基本上不是真实的现有电子邮件地址的电子邮件地址,如下所示: 14370afcdc17429f9e418d5ffbd0334a@magic.com ce06e817-2149-6cfd-dd24-51b31e93ea1a@stackoverflow.org.il 87c0d782-e09f-056f-f544-c6ec9d17943c@microsoft.org.il root@ns3160176.ip-151-106-35.欧盟 ds4-f1

我构建了一个应用程序来建议电子邮件地址修复,我需要检测基本上不是真实的现有电子邮件地址的电子邮件地址,如下所示:

14370afcdc17429f9e418d5ffbd0334a@magic.com
ce06e817-2149-6cfd-dd24-51b31e93ea1a@stackoverflow.org.il
87c0d782-e09f-056f-f544-c6ec9d17943c@microsoft.org.il
root@ns3160176.ip-151-106-35.欧盟
ds4-f1g-54-h5-dfg-yk-4gd-htr5-fdg5h@outlook.com
h-rt-dfg4-sv6-fg32-dsv5-vfd5-ds312@gmail.com
test@454-fs-ns-dff4-xhh-43d-frfs.com

我可以进行多个正则表达式检查,但我不认为我会达到可疑的“非真实”电子邮件地址的良好比率%,因为我每次都会使用特定的正则表达式模式

我进去看了看:



最后我看了一下:
我想它似乎适合我的需要。一个脚本,将给我一些分数的可能性,每个部分的电子邮件地址是一个胡言乱语(或不是真实的)电子邮件地址

所以我想要的是输出:

const strings = ["14370afcdc17429f9e418d5ffbd0334a", "gmail", "ce06e817-2149-6cfd-dd24-51b31e93ea1a", 
                 "87c0d782-e09f-056f-f544-c6ec9d17943c", "space-max", "ns3160176.ip-151-106-35", 
                 "ds4-f1g-54-h5-dfg-yk-4gd-htr5-fdg5h", "outlook", "h-rt-dfg4-sv6-fg32-dsv5-vfd5-
                  ds312", "system-analytics", "454-fs-ns-dff4-xhh-43d-frfs"];

for (let i = 0; i < strings.length; i++) {
   validateGibbrish(strings[i]);
}
最后我将循环所有字符串,得到如下结果:

"14370afcdc17429f9e418d5ffbd0334a"                  ->    8.9073
"gmail"                                             ->    1.0044
"ce06e817-2149-6cfd-dd24-51b31e93ea1a"              ->    7.4261
"87c0d782-e09f-056f-f544-c6ec9d17943c"              ->    8.3916
"space-max"                                         ->    1.3553
"ns3160176.ip-151-106-35"                           ->    6.2584
"ds4-f1g-54-h5-dfg-yk-4gd-htr5-fdg5h"               ->    7.1796
"outlook"                                           ->    1.6694
"h-rt-dfg4-sv6-fg32-dsv5-vfd5-ds312"                ->    8.5734
"system-analytics"                                  ->    1.9489
"454-fs-ns-dff4-xhh-43d-frfs"                       ->    7.7058
有没有人能提供如何做的提示和帮助?
非常感谢:)

更新(12-22-2020)

我根据@Konstantin Pribluda answer编写了一些代码,Shannon熵计算:

const getFrequencies = str => {
    let dict = new Set(str);
    return [...dict].map(chr => {
        return str.match(new RegExp(chr, 'g')).length;
    });
};

// Measure the entropy of a string in bits per symbol.
const entropy = str => getFrequencies(str)
    .reduce((sum, frequency) => {
        let p = frequency / str.length;
        return sum - (p * Math.log(p) / Math.log(2));
    }, 0);

const strings = ['14370afcdc17429f9e418d5ffbd0334a', 'or', 'sdf', 'test', 'dave coperfield', 'gmail', 'ce06e817-2149-6cfd-dd24-51b31e93ea1a',
    '87c0d782-e09f-056f-f544-c6ec9d17943c', 'space-max', 'ns3160176.ip-151-106-35',
    'ds4-f1g-54-h5-dfg-yk-4gd-htr5-fdg5h', 'outlook', 'h-rt-dfg4-sv6-fg32-dsv5-vfd5-ds312', 'system-analytics', '454-fs-ns-dff4-xhh-43d-frfs'];

for (let i = 0; i < strings.length; i++) {
    const str = strings[i];
    let result = 0;
    try {
        result = entropy(str);
    }
    catch (error) { result = 0; }
    console.log(`Entropy of '${str}' in bits per symbol:`, result);
}
它仍然没有像预期的那样工作,因为《戴夫·科普菲尔德》得到的分数与其他胡言乱语的结果差不多


还有谁对如何做有更好的逻辑或想法吗?

这就是我想到的:
//胡言乱语检测器js
(职能(h){
函数e(c,b,a){返回ca?(b=c-a,Math.log(100-a)/Math.log(b)*100):0}函数k(c){for(var b={,a=”,d=0;d(freq[c]=(freq[c]| | 0)+1)和&freq,{})).reduce((sum,f)=>sum-f/str.length*Math.log2(f/str.length),0)
}
//元音计数器
函数计数元音(单词){
var m=word.match(/[aeiou]/gi);
返回m==null?0:m.length;
}
//虚拟函数
函数isTrue(值){
返回值
}
//通过多个测试验证字符串
函数detectGibberish(str){
var strWithoutPunct=str.replace(/[,\/\\!$%\^&*;:{}=\-\-\\`~(())]/g,”);
var entropyValue=熵(str)<3.5;
var GibbershValue=胡言乱语。检测(str)<50;
var-vovelValue=30<100/strWithoutPunct.length*countvowells(strWithoutPunct)和&100/strWithoutPunct.length*countvowells(str)<35;
返回[entropyValue,GibbershValue,vovelValue]。筛选器(isTrue)。长度>1
}
//主要功能
功能验证电子邮件(电子邮件){
返回validateSyntax(电子邮件)?detectGibberish(email.split(“@”)[0]):false
}
//测验
document.write(validateEmail(“dsfghjdhjs@gmail.com“”+“
”)
文件。写(验证邮件(“jhon。smith@gmail.com“你可以考虑做的事情是每次检查每个字符串是如何随机的,然后根据它们的分数对结果进行排序,并给出一个排除高随机性的阈值。你难免会错过一些

有一些用于检查字符串随机性的实现,例如:

在应用其中一些之前,您可能必须创建一个散列(将字符和符号映射到整数序列),因为有些仅用于整数,因为它们测试随机数生成器的属性

另一个可以提供帮助的堆栈交换链接是:


另外,我在一项服务中遇到了类似的问题,因为机器人用这些类型的假电子邮件创建帐户。经过多年处理这个问题(基本上是从数据库中手动删除假电子邮件),我现在正在考虑引入一种视觉检查(captcha)在注册页面中,避免沮丧。

如果
14370afcdc17429f9e418d5ffbd0334a@domain.com
电子邮件是否有效?您的问题是什么?
reallymyemail@gmail.com
如果一个人有这样的电子邮件地址,那可能是假的,但我得到了你想要的。这似乎是你可以训练/使用人工智能的东西。我不知道手动编码是否会成功,因为总会有奇怪的异常。顺便说一句,这可能与“使用苹果登录”冲突。谢谢你,朋友。我这样做是为了我的私人项目,不是为了注册什么的。我正在考虑也许要为此编写一个新的NPM包。你的信息链接可能很有用。再次感谢。感谢您花时间和精力为我编写此函数。虽然它没有像我预期的那样100%工作,但它已经足够满足我的需要了。
const getFrequencies = str => {
    let dict = new Set(str);
    return [...dict].map(chr => {
        return str.match(new RegExp(chr, 'g')).length;
    });
};

// Measure the entropy of a string in bits per symbol.
const entropy = str => getFrequencies(str)
    .reduce((sum, frequency) => {
        let p = frequency / str.length;
        return sum - (p * Math.log(p) / Math.log(2));
    }, 0);

const strings = ['14370afcdc17429f9e418d5ffbd0334a', 'or', 'sdf', 'test', 'dave coperfield', 'gmail', 'ce06e817-2149-6cfd-dd24-51b31e93ea1a',
    '87c0d782-e09f-056f-f544-c6ec9d17943c', 'space-max', 'ns3160176.ip-151-106-35',
    'ds4-f1g-54-h5-dfg-yk-4gd-htr5-fdg5h', 'outlook', 'h-rt-dfg4-sv6-fg32-dsv5-vfd5-ds312', 'system-analytics', '454-fs-ns-dff4-xhh-43d-frfs'];

for (let i = 0; i < strings.length; i++) {
    const str = strings[i];
    let result = 0;
    try {
        result = entropy(str);
    }
    catch (error) { result = 0; }
    console.log(`Entropy of '${str}' in bits per symbol:`, result);
}
Entropy of '14370afcdc17429f9e418d5ffbd0334a' in bits per symbol: 3.7417292966721747
Entropy of 'or' in bits per symbol: 1
Entropy of 'sdf' in bits per symbol: 1.584962500721156
Entropy of 'test' in bits per symbol: 1.5
Entropy of 'dave coperfield' in bits per symbol: 3.4565647621309536
Entropy of 'gmail' in bits per symbol: 2.3219280948873626
Entropy of 'ce06e817-2149-6cfd-dd24-51b31e93ea1a' in bits per symbol: 3.882021446536749
Entropy of '87c0d782-e09f-056f-f544-c6ec9d17943c' in bits per symbol: 3.787301737252941
Entropy of 'space-max' in bits per symbol: 2.94770277922009
Entropy of 'ns3160176.ip-151-106-35' in bits per symbol: 3.1477803284561103
Entropy of 'ds4-f1g-54-h5-dfg-yk-4gd-htr5-fdg5h' in bits per symbol: 3.3502926596166693
Entropy of 'outlook' in bits per symbol: 2.1280852788913944
Entropy of 'h-rt-dfg4-sv6-fg32-dsv5-vfd5-ds312' in bits per symbol: 3.619340871812292
Entropy of 'system-analytics' in bits per symbol: 3.327819531114783
Entropy of '454-fs-ns-dff4-xhh-43d-frfs' in bits per symbol: 3.1299133176846836