gpt4 book ai didi

java - 如何从文本文件加载文本并与另一个文本文件进行比较?

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:44 24 4
gpt4 key购买 nike

我编写了一个小程序,它从文件中读取文本并打印文本文件中每个单词的相反内容。

现在我想尝试有效地查找所有反向单词是否都是英语词典中的实际单词,并将它们打印为按字符串长度排序的列表,每对出现一次。

这是我到目前为止能写的内容

    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
}

这给了我以下结果

what he saw was not part of a trap just a ton of crazy snow

{4=a, a, 6=of, fo, 8=ton, not, 10=snow, wons, 12=crazy, yzarc}

现在,我的问题是如何使用字典文本文件交叉检查单词的反向(最好逐个字符),以查看反向单词是否构成有意义的单词?

我环顾四周,看到了一些建议,但找不到可以帮助我了解如何解决当前问题的建议。

我想过使用二叉搜索树,我的想法是将字典文件加载到二叉树中并搜索树以查看反向单词是否构成现有单词然后将其打印出来。但我无法继续,因为我不明白如何从一个文本文件中获取字符串,然后将其与第二个文本文件进行比较。

最后,您能帮我指出正确的方向,让单词出现在二维数组中吗?!我尝试了几次,但就是行不通,我没有主意了:(!

提前致谢。

最佳答案

您可以逐行读取字典文件。假设每一行包含一个单词,您需要将该行放入哈希集中。然后,您可以检查从第一个文件中读取的单词,反转每个单词,并检查反转的单词是否在该哈希集中。

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();

关于java - 如何从文本文件加载文本并与另一个文本文件进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com