将PHP方法转换为Java代码

时间:2013-12-22 11:37:22

标签: java indexoutofboundsexception

目前我正在将此代码转换为java编程语言。此代码检查句子是否包含关键短语

示例:

   sentence = "Hello world. Programming for every one"
    key phrase = "helo wrld"

这将返回true

function containsKeyPhrase($phrase,$key_phrase){
        $min_words = 1; 
        $key = preg_split("/[' ]/", strtolower($key_phrase));  
        $max_words = count($key);  
        $ngrams = array(); 
        $words  = str_word_count(strtolower($phrase), 1);

        print_r(array_slice($words, 1, 1));

        $word_count = count($words);  
        for ($i = 0; $i <= $word_count - $min_words; $i++) {
            for ($j = $min_words; $j <= $max_words && ($j + $i) <= $word_count; $j++) { 
                $ngrams[] = implode(' ',array_slice($words, $i, $j));
            }
        }

        $n_grams = array_unique($ngrams);

        print_r($n_grams);

        foreach ($n_grams as $ngram) {  
            if (levenshtein( strtolower ($key_phrase),  strtolower ($ngram)) < 5 && soundex( strtolower ($key_phrase))== soundex( strtolower ($ngram))) {
                return true; 
            }
        }
        return false;
}

我已经创建了它的java版本。但问题是我一直收到错误ArrayIndexOutOfBoundsException

我已经测试了所有功能并且运行正常。还有我可以使用的java中的array_unique,levenshtein和soundex版本吗?

import java.util.ArrayList;
import java.util.HashSet;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        containsKeyPhrase("Hello World","hello");
    }

    private static boolean containsKeyPhrase(String msg, String key_phrase) { 
            String[] tokens = key_phrase.toLowerCase().split("[\\s']");
            String[] words = msg.toLowerCase().split("[\\s']");
            ArrayList<String> nGrams = new ArrayList<String>();
            int min_words = 1;

            for (int i = 0; i <= words.length - min_words; i++) {
                for (int j = min_words; j <= tokens.length
                        && (j + i) <= words.length; j++) {
                    System.out.println(implode(" ",stringArraySlice(words, i, j)));
                }
            }

            HashSet hs = new HashSet();
            hs.addAll(nGrams);
            nGrams.clear();
            nGrams.addAll(hs); 

        //Still Implementing    
        // $n_grams = array_unique($ngrams);
        //
        // print_r($n_grams);
        //
        // foreach ($n_grams as $ngram) {
        // if (levenshtein( strtolower ($key_phrase), strtolower ($ngram)) < 5
        // && soundex( strtolower ($key_phrase))== soundex( strtolower
        // ($ngram))) {
        // return true;
        // }
        // }

        return false;

    }

    private static String implode(String separator, String... data) {
    StringBuilder sb = new StringBuilder();
    System.out.println("lenght:" + data.length);
    if (data.length > 0) {
        for (String str : data) { 
            sb.append(str);
            sb.append(separator);
        }  
    }
    return sb.toString();
}
    public static String[] stringArraySlice(String[] array, int offset,
        int lenght) {
    List<String> newArr = new ArrayList<String>();

    for (int i = 0; i < lenght; i++) {
        if (offset > array.length - 1) {
            break;
        }
        newArr.add(array[offset++]);
    }

    String[] arrs = (String[]) newArr.toArray(new String[newArr.size()]);
    return arrs;
}
}

1 个答案:

答案 0 :(得分:1)

无需实施stringArraySlice。您可以使用Array.copyofRange()

话虽如此,你有一个错误:

for (int i = offset; i < array.length && i <= lenght; i++) {

如果offset为3且length为2,那么您将获得一个空数组,因为i <= length在循环开始之前为true。如果数组为空,那么您在implode方法中遇到问题:

sb.append(data[data.length - 1]);

等于

sb.append(data[-1]);