PHP similar_text() in java

怎甘沉沦 提交于 2019-12-01 11:55:24

问题


Do you know any strictly equivalent implementation of the PHP similar_text function in Java?


回答1:


Here is my implementation in java :

package comwebndesignserver.server;

import android.util.Log;

/*
 * 
 * DenPashkov 2012 
 * http://www.facebook.com/pashkovdenis
 *  * PhP Similar String  Implementation 
 * 30.07.2012 
 * 
 */

public class SimilarString {

    private String  string = "" ;
    private String string2 = ""; 
    public int procent = 0 ; 
    private int position1 =0 ; 
    private int position2 =0;

    // Similar String 
    public SimilarString(String str1,  String str2){
        this.string = str1.toLowerCase();   
        this.string2 = str2.toLowerCase(); 
    }
    public SimilarString() {

    }
    // Set string 
    public SimilarString setString(String str1,  String str2){
        this.string = str1.toLowerCase(); 
        this.string2 = str2.toLowerCase(); 
        return this ; 
    }

 //get Similar 
    public int  similar(){
        string= string.trim() ; 
        string2= string2.trim();
     int len_str1 = string.length() ;
        int len_str2 = string2.length() ; 

        int max= 0; 
        if (string.length()>1 && string2.length()>1 ){
            // iterate 
            for (int p=0  ; p<=len_str1; p++){
                for (int q=0  ; q<=len_str2; q++){
                    for(int l=0 ; (p + l < len_str1) && (q + l < len_str2) && (string.charAt(l) == string2.charAt(l)); l++){
                        if (l>max){
                            max=l ; 
                            position1 = p ; 
                            position2 = q; 
                        }
                    }
                }
            }

         //sim * 200.0 / (t1_len + t2_len)
        this.procent = max * 200 / ((string.length()) + (string2.length())  - (max) + (position2 - position1)   ) - (max*string.length() ) ;
        if (procent>100) procent = 100; 
        if (procent<0) procent = 0; 
        }
        return this.procent ; 
    }
}



回答2:


this works the same as php similar_text function as is in php_similar_str, php_similar_char, PHP_FUNCTION(similar_text) in string.c file of php sources

private float similarText(String first, String second)   {
    first = first.toLowerCase();
    second = second.toLowerCase();
    return (float)(this.similar(first, second)*200)/(first.length()+second.length());
}

private int similar(String first, String second)  { 
    int p, q, l, sum;
    int pos1=0;
    int pos2=0;
    int max=0;
    char[] arr1 = first.toCharArray();
    char[] arr2 = second.toCharArray();
    int firstLength = arr1.length;
    int secondLength = arr2.length;

    for (p = 0; p < firstLength; p++) {
        for (q = 0; q < secondLength; q++) {
            for (l = 0; (p + l < firstLength) && (q + l < secondLength) && (arr1[p+l] == arr2[q+l]); l++);            
            if (l > max) {
                max = l;
                pos1 = p;
                pos2 = q;
            }

        }
    }
    sum = max;
    if (sum > 0) {
        if (pos1 > 0 && pos2 > 0) {
            sum += this.similar(first.substring(0, pos1>firstLength ? firstLength : pos1), second.substring(0, pos2>secondLength ? secondLength : pos2));
        }

        if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
            sum += this.similar(first.substring(pos1 + max, firstLength), second.substring(pos2 + max, secondLength));
        }
    }       
    return sum;
}



回答3:


As for Java, your best bet might be the StringUtils class from the Apache Commons Lang library, which contains the LevensteinDistance method that the other SO posts mention.




回答4:


  1. Download the source code for PHP (http://php.net/downloads.php)
  2. Uncompress it.
  3. Convert the similar_text() function in ext\standard\string.c to Java.
  4. Then eat some ice-cream for tea :D



回答5:


I think you can take a look on this post : PHP similar_text function in Javascript

That's a javascript equivalent for PHP similar_text. You only need to adapt it in Java. sorry if that's not help since I think Javascript syntax and Java has only a little difference.

At least, you know the implementation algorithm



来源:https://stackoverflow.com/questions/2000440/php-similar-text-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!