java - 比较两个不同的文本文件并替换相似的词
问题描述
我最近开始学习 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
解决方案
映射器的示例实现:
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;
}
推荐阅读
- typescript - 验证类:如果存在字段,则验证失败
- django - Django ValidationError EmailField
- google-chrome - 如何从 Chrome 中删除“很危险,因此 Chrome 已阻止它”块?
- java - 在java中使用递归计算pi
- python - AttributeError: 'NoneType' 对象没有属性 'split' 错误
- algorithm - 如果保持节点之间的关系,则两个 K-Way 树是等价的
- java - Spring + Vue 登录:超时和错误处理 [已解决]
- python - 在套接字中读取文件
- angular - 如何在同一个 Observable 中正确返回错误或响应?
- flutter - 在类方法中使用 Navigator.push (MaterialPageRoute)