您好我有一项任务我无法搞清楚。以下是我想要做的概述:
编写一个程序,为三个文件创建一个倒排索引。假设程序已经读取了文件“one.txt”“two.txt”和“”three.txt“,如果用户搜索查询”燕麦大麦“,那么包含这两个单词的唯一文件就是两个.txt它将返回two.txt。
我需要为倒排索引定义一个接口。 这个接口将有3个实现
这是我到目前为止的程度,我真的不知道如何开始将文件中的数据输入到倒排索引中:
/*
* HW 2
*/
import java.util.*;
public class InvertedIndex {
//tester
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter file name #1");
String file1 = in.next();
try
{
Scanner inputStream = new Scanner(new File(file1));
while (inputStream.hasNext())
{
nextWord = inputStream.next();
nextWord.split(" ");
//add nextWord to inverted index
inputStream.nextLine();
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("Error opening file");
System.exit(0);
}
catch(IOException e)
{
System.out.println("Error Reading File");
}
System.out.println("Please enter file name #2");
String file2 = in.next();
try
{
Scanner inputStream = new Scanner(new File(file2));
while (inputStream.hasNext())
{
nextWord = inputStream.next();
nextWord.split(" ");
//add nextWord to inverted index
inputStream.nextLine();
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("Error opening file");
System.exit(0);
}
catch(IOException e)
{
System.out.println("Error Reading File");
}
System.out.println("Please enter file name #3");
String file3 = in.next();
try
{
Scanner inputStream = new Scanner(new File(file3));
while (inputStream.hasNext())
{
nextWord = inputStream.next();
nextWord.split(" ");
//add nextWord to inverted index
inputStream.nextLine();
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("Error opening file");
System.exit(0);
}
catch(IOException e)
{
System.out.println("Error Reading File");
}
}
}
任何帮助或建议都会非常棒。非常感谢!!