我最近开始学习Java,我需要比较一个文本文件和一个同义词库文本文件中的1000个单词。同义词库文本文件中的每一行都有相似的单词,并且每行包含1000个单词中的一个单词。同义词词典中的每个单词都用逗号分隔。我想我快要成功了。我接下来要做的是检查一个单词是否包含在同义词词典中,如果是,将同义词词典中的一行单词映射到1000个单词的文本文件中的单词,我不确定如何做。
package ie.gmit.sw;
import java.io.*;
import java.util.*;
public class Parser {
private Map<String, String>map = new TreeMap<>();
private Collection<String>google = new TreeSet<>();
public void parseGoogle(String file) throws IOException
{
BufferedReader brGoogle = new BufferedReader(new FileReader("google-1000.txt"));
String word = null;
while((word = brGoogle.readLine())!= null)
{
google.add(word);
}
brGoogle.close();
}//parseGoogle
public void parse(String file)throws IOException
{
BufferedReader brMoby = new BufferedReader(new FileReader("MobyThesaurus2.txt"));
String line = null;
while((line = brMoby.readLine())!= null)
{
String[] words = line.split(",");
}
}
public String[] getGoogleWord(String[] words) {
if(google.contains(words))
{
}
return words;
}
}//class Parser发布于 2020-04-17 01:37:11
映射器的示例实现:
import java.util.*;
import java.util.stream.Collectors;
public Map<String, List<String>> mapWordsToThesaurus(Set<String> words, Set<String> thesaurus) {
Map<String, List<String>> result = new HashMap<>();
words.forEach(
word ->
result.put(
word,
thesaurus.stream()
.filter(line -> line.contains(word))
.collect(Collectors.toList())));
return result;
}https://stackoverflow.com/questions/61255466
复制相似问题