简体   繁体   English

使用递归的手机文本预测(Java)

[英]Cell phone text prediction using recursion (Java)

I'm having some trouble on a Java assignment for school. 我在学校的Java作业上遇到了麻烦。 We need to write a program that does T9 Word text prediction used by certain cell phones. 我们需要编写一个程序来执行某些手机使用的T9 Word文本预测。 It should take a user's input of numbers, find every possible combination of letters corresponding to those numbers, search a dictionary for each of those possible combinations, and display those which were found in the dictionary. 它应该接受用户输入的数字,找到与这些数字相对应的字母的每种可能组合,为每种可能的组合搜索词典,并显示在词典中找到的那些组合。 Most of this program was already written for us by the professor, I just need to fill in the predictText method which does the combining of possibilities. 该程序的大部分内容已经由教授为我们编写,我只需要填写predictText方法就可以了。 It does the searching by calling another method that uses a recursive binary search, which I also had to fill in myself, although I'm quite certain my search method is working well. 它通过调用另一种使用递归二进制搜索的方法来进行搜索,尽管我非常确定自己的搜索方法运行良好,但我也必须自己填写。 Here is my predictText method, where parameter "letter" is the letter being currently processed, parameter input is the user's input, and parameter "wordMatches" is the list of words that have been found in the dictionary. 这是我的预报文本方法,其中参数“字母”是当前正在处理的字母,参数输入是用户的输入,参数“ wordMatches”是在词典中找到的单词列表。

 public static void predictText(String letter, String input, ArrayList<String> wordMatches) 
  {    
    String[] two = new String[] {"a", "b", "c"};
    String[] three = new String[] {"d", "e", "f"};
    String[] four = new String[] {"g", "h", "i"};
    String[] five = new String[] {"j", "k", "l"};
    String[] six = new String[] {"m", "n", "o"};
    String[] seven = new String[] {"p", "q", "r", "s"};
    String[] eight = new String[] {"t", "u", "v"};
    String[] nine = new String[] {"w", "x", "y", "z"};
    char firstDigit;
    String finalWord = "";

    finalWord += letter;

    if (input.equals(""))
    {
      int lookup = search(finalWord.trim());
      if (lookup != -1)
      {
        wordMatches.add(allWords[lookup]);
      }
    }

    else
    {      
      firstDigit = input.charAt(0);
      input = input.substring(1);

      if (firstDigit == '2')
      {
        for (int i = 0; i < two.length; i++)
        {
          letter = two[i];
          predictText(letter, input, wordMatches);
        }

      }
      else if (firstDigit == '3')
      {
        for (int i = 0; i < three.length; i++)
        {
          letter = three[i];
          predictText(letter, input, wordMatches);
        }

      }
      // And so forth, up to 9
    }
  }

The result that I'm getting is that it's only displaying single letters, it doesn't seem to be combining the letters into words and searching the dictionary for those complete words. 那我得到的结果是,它仅显示单个字母,它似乎并没有字母被组合成词和搜索字典对那些完整的词。 Just in case some further explanation is needed, here are the assignment instructions: 万一需要进一步解释,以下是分配说明:

"You will take the first digit from the input string, and call predictText again, once for each of the possible letters the first digit could represent, with the letter added to the variable word, and the first digit stripped from the input. For example, if the first time into predictText word is empty and input is “4663”, you would call predictText recursively 3 times with: • word as “g” and input as “663”, • word as “h” and input as “663”, • word as “i” and input as “663” The process would repeat, and you'd keep building up the word (so for word as “g” and the next digit being 6, you would recursively call with “gm”, “gn”, and “go” with the input “63”) Once there is no more input to process recursively, you've reached a base case so then call the search method to see if the word you generated exists in the dictionary. If it does, add it to the wordMatches list." “您将从输入字符串中提取第一个数字,然后再次调用预报文本,对于每个可能的字母,第一个数字可以表示一次,将字母添加到可变词中,并且从输入中去除第一个数字。例如,如果第一次进入ForecastText的单词为空,并且输入为“ 4663”,则您将递归调用预测文本3次,其中:•word为“ g”,输入为“ 663”,•word为“ h”,输入为“ 663” ”,•单词为“ i”,输入为“ 663”,该过程将重复进行,您将继续构建单词(因此对于单词“ g”,下一个数字为6,您将递归调用“ gm” ”,“ gn”和“ go”(输入为“ 63”))一旦没有更多输入要递归处理,您就已经达到了基本情况,因此请调用搜索方法以查看生成的单词是否存在于字典。如果有,请将其添加到wordMatches列表中。”

EDIT: I also thought I should include my search method code, just in case it actually is causing my problem. 编辑:我还认为我应该包括我的搜索方法代码,以防万一它实际上导致了我的问题。

public static int search(String key) 
  {
    return search(allWords, key, 0, allWords.length-1);
  }


  public static int search(String[] dictionary, String key, int start, int end) 
  {
    int middle = start + ((end - start) / 2);
    int index = -1;

    if (start > end)
    {
      index = -1;
    }
    else if (key.compareToIgnoreCase(dictionary[middle]) == 0)
    {
      index = middle;
    }
    else if (key.compareToIgnoreCase(dictionary[middle]) < 0)
    {
      index = search(dictionary, key, start, middle - 1);
    }
    else
    {
      index = search(dictionary, key, middle + 1, end);
    }

    return index;
  }

You're looking for only one-letter words: 您只在寻找一个字母的单词:

String finalWord = "";

finalWord += letter;

Append into a String , you are doing it just for one letter word. 追加到String ,您只需要一个字母单词即可。 if its unique try StringBuffer 如果其唯一尝试StringBuffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM