问题1,如何判断一个英文单词拼写是否正确
问题描述:例如一个用户输入了一个字符串:hellu,那它是不存在于字典中的,那么我们怎么判断出来呢?
要求:
(1)定义存储所有正确单词的数据结构。
(2)写一个录入单词的函数。
(3)写一个判断用户输入的单词是否正确的函数。
注:忽略大小写,可以认为都是小写。
问题2,如何对拼写错误的单词进行纠错
问题描述:出错的情况下,提示出正确的单词(可能有多个)。出错的情况很多,前中后的位置都有可能出错,可能错一个字母,也可能多一个或少一个字母。
要求:
(1)找出跟错误单词相差一个字母的正确单词即可。
(2)定义存储单词的数据结构。
(3)写出查询函数,返回n个正确的单词。
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/*
* Q1:字典树存储;为加快每个节点的搜索速度,使用HashMap存储子节点
* Q2:
* 第一种:生成所有相差为一个字母的字符串,在字典中搜索过滤;
* 第二种:直接根据条件在字典树中搜索相差一个字母的正确单词
* 两种算法对比:当字典树存储的词越多时(正确的词越多),使用第一种方法越快;反之错误的词越多,直接从字典树中搜索正确的词越快;这里实现第一种;
* */
class Node{
protected boolean isTail=false;
protected String value="";
protected HashMap<Character, Node> child=new HashMap<>();
}
public class TrieTree extends Node{
private final Node root; //字典树的根节点
public TrieTree() {
root = new Node();
}
/**
* 检查字典树中是否完全包含字符串word
* @param word
* @return
*/
public boolean hasStr(String word) {
if (word == null || word.isEmpty()) {
return false;
}
Node pNode = this.root;
for (int i = 0; i < word.length(); i++) {
char temp=word.charAt(i);
//在字典树中没有对应的节点,或者word字符串的最后一个字符在字典树中检测对应节点的isTail属性为false,则返回false
try{
if (!pNode.child.containsKey(temp)
|| (i + 1 == word.length() && !pNode.child.get(temp).isTail)) {
return false;
}
}catch(NullPointerException e){
return false;
}
pNode = pNode.child.get(temp);
}
return true;
}
/**
* 在字典树中插入一个单词
* @param word
*/
public void insert(String word) {
if (word == null || word.isEmpty()) {
return;
}
Node pNode = this.root;
for (int i = 0; i < word.length(); i++) {
if(pNode.child==null){
pNode.child=new HashMap<>();
}
char temp=word.charAt(i);
if (pNode.child.getOrDefault(temp,null) == null) { //如果不存在节点,则new一个一节点插入字典树
Node tmpNode = new Node();
pNode.child.put(temp,tmpNode) ;
}
pNode = pNode.child.get(temp);
}
pNode.isTail = true;
pNode.value=word;
}
}
class Corrector extends TrieTree{
private static final char[] LETTERS={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z'};
private static final int LETTER_NUM= LETTERS.length;
List<String> result=new LinkedList<>();
private void getPossibleWordsByErrorWord(String word){
if (word == null || word.isEmpty()) {
return;
}
for (int i = 0; i < word.length(); i++) {
for(int j=0;j<LETTER_NUM;j++){
//替换
this.result.add(word.substring(0,i)+LETTERS[j]+word.substring(i+1));
//在当前字母前增加
this.result.add(word.substring(0,i)+LETTERS[j]+word.substring(i));
if(i+1==word.length()){
//最后一个字母时在最后也增加
this.result.add(word+LETTERS[j]);
}
}
//删除
this.result.add(word.substring(0,i)+word.substring(i+1));
}
}
public List<String> getRightWords(String word){
if (word == null || word.isEmpty()) {
return null;
}
getPossibleWordsByErrorWord(word);
return this.result.stream().distinct().filter(this::hasStr).collect(Collectors.toList());
}
}
class Q1test{
public static void main(String []args){
TrieTree trieTree=new TrieTree();
trieTree.insert("hello");
trieTree.insert("hell");
System.out.println(trieTree.hasStr("hellow"));
System.out.println(trieTree.hasStr("hello"));
System.out.println(trieTree.hasStr("hell"));
System.out.println(trieTree.hasStr("hel"));
System.out.println(trieTree.hasStr(""));
}
}
class Q2test{
public static void main(String []args){
Corrector corrector = new Corrector();
corrector.insert("hello");
corrector.insert("hell");
corrector.insert("ael");
corrector.getRightWords("hel").forEach(System.out::println);
}
}