首页 > 解决方案 > 比较两个不同的文本文件并替换相似的词

问题描述

我最近开始学习 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

标签: java

解决方案


映射器的示例实现:

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;
  }

推荐阅读