我有 100 个文本文件。其中 50 个称为 text_H,另一个称为 text_T。我想做的是以下打开两个文本文件 text_T_1 和 text_H_1 并找到常用词的数量并将其写入文本文件然后打开 text_H_2 和 text_T_2 并找到常用词的数量....然后打开 text_H_50 和text_T_50 并找出常用词的数量。
我编写了以下代码来打开两个文本文件并查找常用词并返回两个文件之间常用词的数量。结果写在文本文件中
无论出于何种原因,它都没有给我打开的文本文件的常用词数,而是给了我所有文件的常用词数。例如,如果 fileA_1 和 fileB_1 之间的共同词数是 10,fileA_2 和 fileB_2 之间的共同词数是 5,那么我得到的后两个文件的共同词数结果是 10+5=15。 我希望这里有人能捕获我所缺少的任何东西,因为我已经多次通过这段代码但没有成功。提前感谢您的帮助!
代码:
package xml_test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class app {
private static ArrayList<String> load(String f1) throws FileNotFoundException
{
Scanner reader = new Scanner(new File(f1));
ArrayList<String> out = new ArrayList<String>();
while (reader.hasNext())
{
String temp = reader.nextLine();
String[] sts = temp.split(" ");
for (int i = 0;i<sts.length;i++)
{
if(sts[i] != "" && sts[i] != " " && sts[i] != "\n")
out.add(sts[i]);
}
}
return out;
}
private static void write(ArrayList<String> out, String fname) throws IOException
{
FileWriter writer = new FileWriter(new File(fname));
//int count=0;
int temp1=0;
for (int ss= 1;ss<=3;ss++)
{
int count=0;
for (int i = 0;i<out.size();i++)
{
//writer.write(out.get(i) + "\n");
//writer.write(new Integer(count).toString());
count++;
}
writer.write("count ="+new Integer(temp1).toString()+"\n");
}
writer.close();
}
public static void main(String[] args) throws IOException
{
ArrayList<String> file1;
ArrayList<String> file2;
ArrayList<String> out = new ArrayList<String>();
//add for loop to loop through all T's and H's
for(int kk = 1;kk<=3;kk++)
{
int count=0;
file1 = load("Training_H_"+kk+".txt");
file2 = load("Training_T_"+kk+".txt");
//int count=1;
for(int i = 0;i<file1.size();i++)
{
String word1 = file1.get(i);
count=0;
//System.out.println(word1);
for (int z = 0; z <file2.size(); z++)
{
//if (file1.get(i).equalsIgnoreCase(file2.get(i)))
if (word1.equalsIgnoreCase(file2.get(z)))
{
boolean already = false;
for (int q = 0;q<out.size();q++)
{
if (out.get(q).equalsIgnoreCase(file1.get(i)))
{
count++;
//System.out.println("count is "+count);
already = true;
}
}
if (already==false)
{
out.add(file1.get(i));
}
}
}
//write(out,"output_"+kk+".txt");
}
//count=new Integer(count).toString();
//write(out,"output_"+kk+".txt");
//write(new Integer(count).toString(),"output_2.txt");
//System.out.println("count is "+count);
}//
}
}
最佳答案
让我向您展示您的代码在做什么,看看您是否能发现问题。
List wordsInFile1 = getWordsFromFile();
List wordsInFile2 = getWordsFromFile();
List foundWords = empty;
//Does below for each compared file
for each word in file 1
set count to 0
compare to each word in file 2
if the word matches see if it's also in foundWords
if it is in foundWords, add 1 to count
otherwise, add the word to foundWords
//Write the number of words
prints out the number of words in foundWords
提示:问题在于 foundWords
以及您要添加到 count
的位置。 arunmoezhi 的评论是正确的,board_reader 在他的回答中的第 3 点也是如此。
就目前而言,您的代码对任何 count
变量都没有做任何有意义的事情
关于java - 在多个文本文件中查找常用词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23349157/