比较java中的两个字符串的百分比结果

比较java中的两个字符串的百分比结果,java,string,string-comparison,Java,String,String Comparison,我必须比较两个4000-5000个字符的字符串 我需要百分比的结果。70%-80%匹配,java语言 请给我建议解决办法 这是比较两个字符串并得到0到100的整数形式的结果的代码 /** * * @author WARLOCK */ public class LockMatch { public static void main(String arg[]) { //---Provide source and target strings to lock_match

我必须比较两个4000-5000个字符的字符串

我需要百分比的结果。70%-80%匹配,java语言

请给我建议解决办法


这是比较两个字符串并得到0到100的整数形式的结果的代码

/**
 *
 * @author WARLOCK
 */
public class LockMatch {

    public static void main(String arg[]) {
        //---Provide source and target strings to lock_match function to compare--//
        System.out.println("Your Strings are Matched="+lock_match("The warlock","The warlock powered by WTPL")+"%");  
    }

    public static int lock_match(String s, String t) {



        int totalw = word_count(s);
        int total = 100;
        int perw = total / totalw;
        int gotperw = 0;

        if (!s.equals(t)) {

            for (int i = 1; i <= totalw; i++) {
                if (simple_match(split_string(s, i), t) == 1) {
                    gotperw = ((perw * (total - 10)) / total) + gotperw;
                } else if (front_full_match(split_string(s, i), t) == 1) {
                    gotperw = ((perw * (total - 20)) / total) + gotperw;
                } else if (anywhere_match(split_string(s, i), t) == 1) {
                    gotperw = ((perw * (total - 30)) / total) + gotperw;
                } else {
                    gotperw = ((perw * smart_match(split_string(s, i), t)) / total) + gotperw;
                }
            }
        } else {
            gotperw = 100;
        }
        return gotperw;
    }

    public static int anywhere_match(String s, String t) {
        int x = 0;
        if (t.contains(s)) {
            x = 1;
        }
        return x;
    }

    public static int front_full_match(String s, String t) {
        int x = 0;
        String tempt;
        int len = s.length();

        //----------Work Body----------//
        for (int i = 1; i <= word_count(t); i++) {
            tempt = split_string(t, i);
            if (tempt.length() >= s.length()) {
                tempt = tempt.substring(0, len);
                if (s.contains(tempt)) {
                    x = 1;
                    break;
                }
            }
        }
        //---------END---------------//
        if (len == 0) {
            x = 0;
        }
        return x;
    }

    public static int simple_match(String s, String t) {
        int x = 0;
        String tempt;
        int len = s.length();


        //----------Work Body----------//
        for (int i = 1; i <= word_count(t); i++) {
            tempt = split_string(t, i);
            if (tempt.length() == s.length()) {
                if (s.contains(tempt)) {
                    x = 1;
                    break;
                }
            }
        }
        //---------END---------------//
        if (len == 0) {
            x = 0;
        }
        return x;
    }

    public static int smart_match(String ts, String tt) {

        char[] s = new char[ts.length()];
        s = ts.toCharArray();
        char[] t = new char[tt.length()];
        t = tt.toCharArray();


        int slen = s.length;
        //number of 3 combinations per word//
        int combs = (slen - 3) + 1;
        //percentage per combination of 3 characters//
        int ppc = 0;
        if (slen >= 3) {
            ppc = 100 / combs;
        }
        //initialising an integer to store the total % this class genrate//
        int x = 0;
        //declaring a temporary new source char array
        char[] ns = new char[3];
        //check if source char array has more then 3 characters//
        if (slen < 3) {
        } else {
            for (int i = 0; i < combs; i++) {
                for (int j = 0; j < 3; j++) {
                    ns[j] = s[j + i];
                }
                if (cross_full_match(ns, t) == 1) {
                    x = x + 1;
                }
            }
        }
        x = ppc * x;
        return x;
    }

    /**
     *
     * @param s
     * @param t
     * @return
     */
    public static int  cross_full_match(char[] s, char[] t) {
        int z = t.length - s.length;
        int x = 0;
        if (s.length > t.length) {
            return x;
        } else {
            for (int i = 0; i <= z; i++) {
                for (int j = 0; j <= (s.length - 1); j++) {
                    if (s[j] == t[j + i]) {
                        // x=1 if any charecer matches
                        x = 1;
                    } else {
                        // if x=0 mean an character do not matches and loop break out
                        x = 0;
                        break;
                    }
                }
                if (x == 1) {
                    break;
                }
            }
        }
        return x;
    }

    public static String split_string(String s, int n) {

        int index;
        String temp;
        temp = s;
        String temp2 = null;

        int temp3 = 0;

        for (int i = 0; i < n; i++) {
            int strlen = temp.length();
            index = temp.indexOf(" ");
            if (index < 0) {
                index = strlen;
            }
            temp2 = temp.substring(temp3, index);
            temp = temp.substring(index, strlen);
            temp = temp.trim();

        }
        return temp2;
    }

    public static int word_count(String s) {
        int x = 1;
        int c;
        s = s.trim();
        if (s.isEmpty()) {
            x = 0;
        } else {
            if (s.contains(" ")) {
                for (;;) {
                    x++;
                    c = s.indexOf(" ");
                    s = s.substring(c);
                    s = s.trim();
                    if (s.contains(" ")) {
                    } else {
                        break;
                    }
                }
            }
        }
        return x;
    }
}
只需将这两个字符串作为参数提供给lock_matchstring1、string2,它将返回匹配的整数值。如果字符串的大小更大,则增加名称变量的总大小 在代码中。 比如int总数=1000 然后从0到1000给出结果。 此代码区分大小写。 将两个字符串都大写或小写以避免此问题。
源代码位于:

您可以使用Apache Commons Lang 3

Maven依赖项:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>${commons.lang.version}</version>
</dependency>

@Test
public void test_stringDistance() throws Exception {
    String teamName = "Partizn Belgrade";
    String propositionName = "Partizan Belgrade";

    // This one seems better
    double distance = StringUtils.getJaroWinklerDistance(teamName, propositionName);
    System.out.println(distance);
}
这是打印百分比,越大越好100%是精确的

org.apache.commons.lang3.StringUtils.getJaroWinklerDistancefirst,second在commons-lang3:3.6中被弃用

使用新的org.apache.commons.text.similarity.JaroWinklerDistance.applyleft,将其改为right,其中left和right分别代表第一个和第二个。请参见下面的maven依赖项

<dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>{text-version}</version>
</dependency>

我恳请您开始编写解决方案,当您遇到实际问题时再回来。分析、实现、运行。嗨,梅纳,我是java新手,请给我一些提示。@zidan007:这仍然没有理由要求我们为您做作业,甚至不先尝试自己。@zidan007现在是开始学习它的时候了。第1步:从基础开始,而不是这些问题。我正在使用您提供的代码片段及其有用之处,感谢您的发布