【问题标题】:compare two string in java result in percentage [closed]比较java中的两个字符串导致百分比[关闭]
【发布时间】:2015-06-30 10:07:56
【问题描述】:

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

我需要百分比的结果(即 70% - 80% 匹配),在 java 中。

请给我建议任何解决方案。

问候

【问题讨论】:

  • 我建议您开始编写解决方案,并在遇到实际问题时回来。
  • 分析、实施、运行。
  • 嗨 Mena,我是 java 新手,请给我一些提示。
  • @zidan007:这仍然没有理由让我们为你做功课,甚至没有先尝试一下。
  • @zidan007 那么现在是开始学习它的时候了。第 1 步:从基础开始,而不是从这类问题开始。

标签: java string string-comparison


【解决方案1】:

这是比较两个字符串并以 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_match(string1, string2) ,它将返回匹配的整数值。如果字符串的大小更大,则增加总名称变量大小 在代码中。 喜欢 int 总计=1000 然后将给出 0 到 1000 之间的结果。 此代码区分大小写。 大写或小写两个字符串都可以摆脱这个问题。 源代码在:lock match

【讨论】:

  • 我正在使用您提供的代码 sn-p 并且它很有用,感谢发布
【解决方案2】:

您可以使用 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%是准确的)

【讨论】:

    【解决方案3】:

    org.apache.commons.lang3.StringUtils.getJaroWinklerDistance(first, second) 自 commons-lang3:3.6 起已弃用

    使用 new org.apache.commons.text.similarity.JaroWinklerDistance().apply(left, right) 代替 left 和 right 分别代表第一和第二。请参阅下面的 maven 依赖项

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      相关资源
      最近更新 更多