我写了一个小程序,它从文件中读取文本并打印文本文件中每个单词的反面。
现在我想尝试有效地查找所有反向的单词是否是英语词典中的实际单词,并将其打印为按字符串长度排序的列表,每对出现一次。
这是我到目前为止所写的内容
String word;
String[] reverse;
int wordLength;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO code application logic here
Map<Integer, String> aMap2 = new HashMap<Integer, String>();
String mString = "";
int mInt = 0;
String word;
String[] reverse = new String[1];
int wordLength;
FileInputStream fileIn = new FileInputStream ("example.txt”);
//text in the text file - "what he saw was not part of a trap just a ton of crazy snow"
Scanner scan = new Scanner(fileIn);
Scanner lineScanner;
Scanner wordscanner = null;
String aLine;
String aWord;
while(scan.hasNextLine()){
aLine = scan.nextLine();
lineScanner = new Scanner(aLine);
lineScanner.useDelimiter(",");
word = lineScanner.next();
System.out.println(word);
try{
wordscanner = new Scanner(new File("example.txt"));
} catch(FileNotFoundException x){
x.printStackTrace();
}
while(wordscanner.hasNext()){
Scanner newWord = new Scanner(wordscanner.next());
boolean b;
while(b = newWord.hasNext()){
String foundWord = newWord.next();
wordLength = foundWord.length();
char ch = ' ';
String reverseWord = “";
for(int i = wordLength-1; i >= 0; i--){
ch = foundWord.charAt(i);
//System.out.println(ch);
reverseWord = reverseWord + ch;
}
for(int k = 0; k < reverse.length; k++){
reverse[k] = foundWord + ", " + reverseWord;
mString = reverse[k];
mInt = reverse[k].length();
aMap2.put(mInt, mString);
}
}
}
}
Map<Integer, String> sorted = sortByKeys(aMap2);
System.out.println(sorted);
}
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
//LinkedHashMap will keep the keys in the order they are inserted
//which is currently sorted on natural ordering
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(K key: keys){
sortedMap.put(key, map.get(key));
}
return sortedMap;
// http://javarevisited.blogspot.co.uk/2012/12/how-to-sort-hashmap-java-by-key-and-value.html
}
这给了我以下结果
他所看到的并不是陷阱的一部分只是一堆疯狂的雪{4 = a,a,6 = of,fo,8 = ton,not,10 = snow,wons,12 = crazy,yzarc}
现在,我的问题是如何用字典文本文件交叉检查单词的反面(最好是逐个字符),以查看反向单词是否构成有意义的单词?
我在这里看了一下,我看到了一些建议,但我找不到一个帮助我理解如何解决当前问题的建议。
我想过使用二进制搜索树,我的想法是将字典文件加载到二叉树中并搜索树以查看反向字是否构成现有单词然后将其打印出来。但由于我不理解如何从一个文本文件中获取字符串然后将其与第二个文本文件进行比较,我无法继续。
最后,你能帮我指出正确的方向让我们在2D数组中出现这些词吗?我尝试了几次,但它只是不起作用,我没有想法:(!
提前谢谢。
答案 0 :(得分:1)
您可以逐行阅读字典文件。假设每行包含一个单词,您需要将该行放入哈希集。然后你可以翻看你从第一个文件中读取的单词,反转每个单词并检查反转的单词是否在该哈希集中。
public Set<String> readFileIntoSet(String fileName) {
Set<String> result = new HashSet<String>();
for (Scanner sc = new Scanner(new File(fileName)); sc.hasNext(); )
result.add(sc.nextLine());
return result;
}
在您的主要方法中添加对readFileIntoSet
的调用:
Set<String> dictionary = readFileIntoSet("your_dictionary_file");
然后,在找到反转的单词后,检查它是否出现在字典中:
if (dictionary.contains(reverseWord))
system.out.println("this word: " + reverseWord + " appears in the dictionary!");
请注意String类提供了“反向”方法。因此,您可以摆脱for(int i = wordLength-1; i >= 0; i--){ ... }
循环,只使用reverseWord = foundWord.reverse();