java - 需要一些管理字符串的帮助

标签 java string hashtable

不用担心哈希表,只要给我一些如何管理字符串的想法即可。

我需要使用哈希表对用户在字典中输入的单词进行拼写检查。我从哈希表中得到了一个名为 checkDictionary() 的方法来检查给定的单词是否存在于字典中。如果该单词存在,则返回 boolean 值;如果不存在,则返回 false。

我想做的是,我只想在字典中检查拼写错误的单词,进行一些可能的更正。

可能的更正:

更改一个字母:例如,如果拼写错误的单词是“kest”,我想尝试所有可能性 一次改变一个字符,然后在字典中查找修改后的单词。这 可能性将是“aest”、“best”、...、“zest”、“kast”、...、“kzst”等。

---我如何一次更改一个字符,并将其从 a 更改为 z。

交换相邻字母:例如,如果拼写错误的单词是“ebst”,请尝试“best”、esbt” 和“ebts”。

---我怎样才能改变相邻的字母,需要交换什么的?..

删除一个字母:例如,如果拼写错误的单词是 “tbird”,尝试一次删除一个字母的所有可能性,然后查找修改后的单词 在字典中,它们是:“bird”、“tird”、“tbrd”和“tbir”。

---如何每次删除每个字母?

请记住,输入的单词可以是任意长度。

我需要在检查字典中的单词后将此建议返回给用户。 Strings 中是否有任何方法可以用来实现这些功能。 请帮助实现上述方法更改、交换和删除。

     import java.util.*;
     import java .io.*;

     public class HashTableDemo
   {
     public static void main(String [] args)
  {

   // constructs a new empty hashtable with default initial capacity
     HashTable hashtable = new HashTable();
     Scanner keyboard = null;
     Scanner input=null;
     try
     {
        System.out.println("Enter a word to check in dictionary");
        keyboard = new Scanner(System.in);
        String word = (keyboard.nextLine().toUpperCase());

     //Adding aal dictionary words from a text file to hash table.
        input=new Scanner(new FileInputStream("TWL.txt"));
            int i=1;

            // adding value into hashtable
            while(input.hasNextLine())
             {
             String hello = input.nextLine();
                            hashtable.put( hello, new Integer(i) ); 
             i++;
             }
      );


        if(hashtable.checkDictionary(word))
           System.out.println("The word "+word+" is there in the dictionary.");
        else
           System.out.println("The word "+word+" is not there in the dictionary.");
     }//try



     //Here I need to implement the required methods if the word is not in dictionary and misspelled.





         catch(FileNotFoundException e)
        {
           System.out.println("Cannot open file");
           System.exit(0);
        }//end catch

最佳答案

对于您想要实现的目标,没有简单的解决方案。可以用于拼写检查的一个很好的数学概念称为 Edit Distance ,在尝试编写一些代码之前,您绝对应该阅读一些理论。

关于java - 需要一些管理字符串的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10362383/

相关文章:

java - 从微调器中选择项目,对 ListView 中的 JSON 结果进行过滤/排序

java - 是否应该通过 AsyncTask 类调用 DialogFragment?

java - VSCode : Issue installing Gradle distribution

c - 运行简单的字符串 C 程序时出现总线错误

python - 为什么Python中的字符串在列表内部和外部显示不同?

java - Java 中的二维数组,按字符索引

swift - String.cString(使用 : String. Encoding.utf16)是否正常工作?

scala - 使用 scala 调用 java.util.Hashtable#put

c# - 在 c sharp 中广泛使用哈希表

Java哈希表搜索函数