比较可能的字符串的Javascript返回% [英] Compare Strings Javascript Return %of Likely
问题描述
我在寻找一个可以比较两个字符串并返回likeliness,他们都是一样的JavaScript函数。我看过的同音但是这并不是真正伟大的多字串或非名。我要寻找一个功能,如:
功能比较(STRA,STRB){
}
比较(苹果,苹果)=有些X的百分比。
该功能将与所有类型的字符串,包括数字,多字值和名称。也许有一个简单的算法,我可以用?
最终没有这些服务我的目的,所以我用这个:
功能比较(C,U){
VAR incept = FALSE;
变种CA = c.split(,);
U =干净的(U);
(所有正确答案集合)// CA =正确答案阵列
// CAA =一个正确的答案字数组(集合中的一个唯一正确的答案的话)
// U =用户回答的话阵列使用自定义的清洗功能清洗
对于(VAR Z = 0; Z< ca.length; Z ++){
CAA = $ .trim(CA [Z])分裂()。
变种的pc = 0;
为(变种X = 0 X - 其中; caa.length; X ++){
为(变种Y = 0; Y< u.length; Y ++){
如果(SOUNDEX(U [Y])= NULL和放大器;!&安培;!SOUNDEX(CAA [X])= NULL){
如果(SOUNDEX(U [Y])== SOUNDEX(CAA [X])){
PC = PC + 1;
}
}
其他 {
如果(U [Y] .indexOf(CAA [X])> -1){
PC = PC + 1;
}
}
}
}
如果((PC / caa.length)0.5){
返回true;
}
}
返回false;
}
//创建对象列表的SOUNDEX值,每个字母
// -1表示,这封信是不是codeD,但用于编码
// 0表示,这封信被省略了现代普查档案
//但作用就像-1为老年人普查档案
// 1是用于BFPV
// 2是用于CGJKQSXZ
// 3是DT
// 4是对于L
// 5是锰我的家乡
// 6是对R
功能makesoundex(){
this.a = -1
this.b = 1
this.c = 2
this.d = 3
this.e = -1
this.f = 1
this.g = 2
this.h = 0
this.i = -1
this.j = 2
this.k = 2
this.l = 4
this.m = 5
this.n = 5
this.o = -1
this.p = 1
this.q = 2
this.r = 6
this.s = 2
this.t = 3
this.u = -1
this.v = 1
this.w = 0
this.x = 2
this.y = -1
this.z = 2
}
VAR SNDX =新makesoundex()
//检查输入的是有效的
功能isSurname(名字){
如果(名称==||名== NULL){
返回false
} 其他 {
对于(VAR I = 0; I< name.length;我++){
VAR信= name.charAt(I)
如果((字母GT;!='A'和;&安培;字母LT ='Z'||字母GT ='A'和;&安培;字母LT ='Z')){
返回false
}
}
}
返回true
}
//关闭了直接相邻的声音
// 1.假设surname.length> = 1
// 2。假设姓只包含小写字母
函数坍缩(姓){
如果(surname.length == 1){
返回姓
}
无功权=崩溃(surname.substring(1,surname.length))
如果(SNDX [surname.charAt(0)] == SNDX [right.charAt(0)]){
返回surname.charAt(0)+ right.substring(1,right.length)
}
返回surname.charAt(0)+右
}
使用新的国家档案馆方法//关闭了直接相邻的声音
// 1.假设surname.length> = 1
// 2。假设姓只包含小写字母
// 3,H和W被完全忽略
功能省略(姓){
如果(surname.length == 1){
返回姓
}
无功权=省略(surname.substring(1,surname.length))
如果(!SNDX [right.charAt(0)]){
返回surname.charAt(0)+ right.substring(1,right.length)
}
返回surname.charAt(0)+右
}
//输出codeD序列
功能output_sequence(SEQ){
无功输出= seq.charAt(0).toUpperCase()//保留第一个字母
输出+ = - //另一封信用破折号
VAR阶段2 = seq.substring(1,seq.length)
变种数= 0
对于(VAR I = 0; I< stage2.length和放大器;&放大器;数3;;我++){
如果(SNDX [stage2.charAt(ⅰ)]&0){
输出+ = SNDX [stage2.charAt(ⅰ)]
算上++
}
}
对于(;计数3;;计数++){
输出+ =0
}
回报输出
}
//计算SOUNDEX code为姓
函数SOUNDEX(值){
如果(!isSurname(值)){
返回NULL
}
VAR阶段1 =崩溃(value.toLowerCase())
//form.result.value=output_sequence(stage1);
VAR阶段1 =省略(value.toLowerCase())
VAR阶段2 =崩溃(阶段1)
返回output_sequence(阶段2);
}
功能干净的(U){
变种U = u.replace(/ \,/克,);
U = u.toLowerCase()分裂()。
变种CW = [数组词来被排除在比较];
变种N = [];
为(变种Y = 0; Y< u.length; Y ++){
VAR测试= FALSE;
对于(VAR Z = 0; Z< cw.length; Z ++){
如果(U [Y] =&放大器;!&安培;!U [Y] = CW [Z]){
测试= TRUE;
打破;
}
}
如果(试验){
//不要使用与放大器;或$比较
VAR VAL = U [Y] .replace($,).replace(与&,);
n.push(VAL);
}
}
返回N;
}
下面是一个很简单的功能,做了比较,并返回基于等效的百分比。虽然它尚未经过测试所有可能的方案,它可以帮助您开始。
相似的(A,B){功能
VAR lengthA =则为a.length;
VAR LENGTHB = b.length个;
变种等值= 0;
VAR的minLength =(则为a.length> b.length个)? b.length个:则为a.length;
VAR最大长度=(则为a.length< b.length个)? b.length个:则为a.length;
对于(VAR I = 0; I<的minLength;我++){
如果(A [1] == B〔Ⅰ〕){
等效++;
}
}
VAR重量=当量/最大长度;
返回(重量* 100)+%;
}
警报(类似的(测试,TES)); // 75%
警报(类似的(测试,测试)); // 100%
警报(类似的(测试,testt)); // 80%
警报(类似的(测试,苔丝)); // 75%
I am looking for a JavaScript function that can compare two strings and return the likeliness that they are alike. I have looked at soundex but that's not really great for multi-word strings or non-names. I am looking for a function like:
function compare(strA,strB){
}
compare("Apples","apple") = Some X Percentage.
The function would work with all types of strings, including numbers, multi-word values, and names. Perhaps there's a simple algorithm I could use?
Ultimately none of these served my purpose so I used this:
function compare(c, u) {
var incept = false;
var ca = c.split(",");
u = clean(u);
//ca = correct answer array (Collection of all correct answer)
//caa = a single correct answer word array (collection of words of a single correct answer)
//u = array of user answer words cleaned using custom clean function
for (var z = 0; z < ca.length; z++) {
caa = $.trim(ca[z]).split(" ");
var pc = 0;
for (var x = 0; x < caa.length; x++) {
for (var y = 0; y < u.length; y++) {
if (soundex(u[y]) != null && soundex(caa[x]) != null) {
if (soundex(u[y]) == soundex(caa[x])) {
pc = pc + 1;
}
}
else {
if (u[y].indexOf(caa[x]) > -1) {
pc = pc + 1;
}
}
}
}
if ((pc / caa.length) > 0.5) {
return true;
}
}
return false;
}
// create object listing the SOUNDEX values for each letter
// -1 indicates that the letter is not coded, but is used for coding
// 0 indicates that the letter is omitted for modern census archives
// but acts like -1 for older census archives
// 1 is for BFPV
// 2 is for CGJKQSXZ
// 3 is for DT
// 4 is for L
// 5 is for MN my home state
// 6 is for R
function makesoundex() {
this.a = -1
this.b = 1
this.c = 2
this.d = 3
this.e = -1
this.f = 1
this.g = 2
this.h = 0
this.i = -1
this.j = 2
this.k = 2
this.l = 4
this.m = 5
this.n = 5
this.o = -1
this.p = 1
this.q = 2
this.r = 6
this.s = 2
this.t = 3
this.u = -1
this.v = 1
this.w = 0
this.x = 2
this.y = -1
this.z = 2
}
var sndx = new makesoundex()
// check to see that the input is valid
function isSurname(name) {
if (name == "" || name == null) {
return false
} else {
for (var i = 0; i < name.length; i++) {
var letter = name.charAt(i)
if (!(letter >= 'a' && letter <= 'z' || letter >= 'A' && letter <= 'Z')) {
return false
}
}
}
return true
}
// Collapse out directly adjacent sounds
// 1. Assume that surname.length>=1
// 2. Assume that surname contains only lowercase letters
function collapse(surname) {
if (surname.length == 1) {
return surname
}
var right = collapse(surname.substring(1, surname.length))
if (sndx[surname.charAt(0)] == sndx[right.charAt(0)]) {
return surname.charAt(0) + right.substring(1, right.length)
}
return surname.charAt(0) + right
}
// Collapse out directly adjacent sounds using the new National Archives method
// 1. Assume that surname.length>=1
// 2. Assume that surname contains only lowercase letters
// 3. H and W are completely ignored
function omit(surname) {
if (surname.length == 1) {
return surname
}
var right = omit(surname.substring(1, surname.length))
if (!sndx[right.charAt(0)]) {
return surname.charAt(0) + right.substring(1, right.length)
}
return surname.charAt(0) + right
}
// Output the coded sequence
function output_sequence(seq) {
var output = seq.charAt(0).toUpperCase() // Retain first letter
output += "-" // Separate letter with a dash
var stage2 = seq.substring(1, seq.length)
var count = 0
for (var i = 0; i < stage2.length && count < 3; i++) {
if (sndx[stage2.charAt(i)] > 0) {
output += sndx[stage2.charAt(i)]
count++
}
}
for (; count < 3; count++) {
output += "0"
}
return output
}
// Compute the SOUNDEX code for the surname
function soundex(value) {
if (!isSurname(value)) {
return null
}
var stage1 = collapse(value.toLowerCase())
//form.result.value=output_sequence(stage1);
var stage1 = omit(value.toLowerCase())
var stage2 = collapse(stage1)
return output_sequence(stage2);
}
function clean(u) {
var u = u.replace(/\,/g, "");
u = u.toLowerCase().split(" ");
var cw = ["ARRAY OF WORDS TO BE EXCLUDED FROM COMPARISON"];
var n = [];
for (var y = 0; y < u.length; y++) {
var test = false;
for (var z = 0; z < cw.length; z++) {
if (u[y] != "" && u[y] != cw[z]) {
test = true;
break;
}
}
if (test) {
//Don't use & or $ in comparison
var val = u[y].replace("$", "").replace("&", "");
n.push(val);
}
}
return n;
}
Here is a very simple function that does a comparison and returns a percentage based on equivalency. While it has not been tested for all possible scenarios, it may help you get started.
function similar(a,b) {
var lengthA = a.length;
var lengthB = b.length;
var equivalency = 0;
var minLength = (a.length > b.length) ? b.length : a.length;
var maxLength = (a.length < b.length) ? b.length : a.length;
for(var i = 0; i < minLength; i++) {
if(a[i] == b[i]) {
equivalency++;
}
}
var weight = equivalency / maxLength;
return (weight * 100) + "%";
}
alert(similar("test","tes")); // 75%
alert(similar("test","test")); // 100%
alert(similar("test","testt")); // 80%
alert(similar("test","tess")); // 75%
这篇关于比较可能的字符串的Javascript返回%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!