需要帮助才能理解本指南

时间:2022-02-03 22:07:16

this guide is given by one of my lecturer and i wanted to implement in java. But i do not quite understand it though on how to implement in on java. Can someone try to explain it further for me? I appreciate a good tutorial though. :)

本指南由我的一位讲师提供,我想在java中实现。但我不太了解它如何在java上实现。有人可以试着为我进一步解释吗?我很欣赏一个很好的教程。 :)

"Create an array for the text file, look through each text file for the term, once you got the term, you break the inner loop, change the file name and loop again to search for the term. then create a counter to count the number of text file that contain the term. Lastly you will get the number of document containing the term from the counter value."

“为文本文件创建一个数组,查看每个文本文件中的术语,一旦你得到了术语,你就打破内部循环,更改文件名并再次循环以搜索术语。然后创建一个计数器来计算包含该术语的文本文件的数量。最后,您将从计数器值中获取包含该术语的文档编号。“

I has a collection of text files where i need to check whether a specific term is found on the file. If the term is found, i need to increment the fileFound to 1.

我有一组文本文件,我需要检查文件中是否找到特定的术语。如果找到该术语,我需要将fileFound增加到1。

Or more specifically inverse document frequency.

或者更具体地说是逆文档频率。

What do i need in order to achieve the above ? I mean in code perspective. Sorry, i am slow in codings. No, i do not wish for free codes. I just want to understand what the guide is given. I have asked my lecturer but it does not sufficient though. I just wanted to know how to implement in java. Thanks for your time.

为了达到上述目的,我需要什么?我的意思是代码透视。对不起,我编码很慢。不,我不希望免费代码。我只想了解指南的内容。我问过我的讲师,但这还不够。我只是想知道如何在java中实现。谢谢你的时间。

System.out.println("Please enter the required word  :");
    Scanner scan2 = new Scanner(System.in);
    String word2 = scan.nextLine();
    String[] array2 = word2.split(" ");

    int numofDoc = 0;
    for (int i = 0; i < filename; i++) {

        for (int b = 0; b < array2.length; b++) {

            try {

                BufferedReader in = new BufferedReader(new FileReader(
                        "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                                + i + ".txt"));

                int matchedWord = 0;

                Scanner s2 = new Scanner(in);

                {
                    while (s2.hasNext()) {
                        if (s2.next().contains(word2))
                            matchedWord++;
                    }

                }
                if (matchedWord > 0)
                    numofDoc++;

                System.out.println("This file contain the term  "
                        + numofDoc);

            } catch (IOException e) {
                System.out.println("File not found.");
            }

        }

    }

The output is :

输出是:

Please enter the required word :

请输入所需的单词:

the
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File not found
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1

i would like the output to printout single output which shows number of File containing the term is 10. It means it already sum up the total. And only print one line out in the output. Any idea sir ?

我想输出打印输出单输出,显示包含该项的文件数为10.这意味着它已经总结了总数。并且只在输出中打印一行。任何想法先生?

4 个解决方案

#1


0  

I just took a quick glance at your code, but it looks as if your variable 'matchedWord' is counting the number of words all ready. You just have to change where you define it and write the appropriate println statement at the end of your method.

我只是快速浏览了一下你的代码,但看起来你的变量'matchedWord'正在计算所有准备好的单词数。您只需更改定义它的位置,并在方法结束时编写相应的println语句。

#2


0  

Your quote doesn't make much sense. However the simplest way to find a term in a file is to do

你的报价没有多大意义。但是,在文件中查找术语的最简单方法是执行此操作

if(FileUtils.readFileToString(filename).contains(term)) 
   counter++;

#3


0  

I think this is sufficient information to start your work on this. Your lecturer has given details of each step that you need to do. Just need to write the code for it.

我认为这是开始你的工作的充分信息。您的讲师已经详细说明了您需要执行的每个步骤。只需要为它编写代码。

#4


0  

Only logical answer will be that you have 1 main loop in which you run through the different texts and inside that a loop to run through the words of the text . When the term is found use break; command to stop inner loop and increment the counters. So you need 2 loops , 1 array/arraylist of texts and the counter

只有逻辑上的答案是你有一个主循环,你在其中运行不同的文本,并在循环内部运行文本的单词。当找到该术语时使用break;命令停止内循环并增加计数器。所以你需要2个循环,1个数组/文本的arraylist和计数器

#1


0  

I just took a quick glance at your code, but it looks as if your variable 'matchedWord' is counting the number of words all ready. You just have to change where you define it and write the appropriate println statement at the end of your method.

我只是快速浏览了一下你的代码,但看起来你的变量'matchedWord'正在计算所有准备好的单词数。您只需更改定义它的位置,并在方法结束时编写相应的println语句。

#2


0  

Your quote doesn't make much sense. However the simplest way to find a term in a file is to do

你的报价没有多大意义。但是,在文件中查找术语的最简单方法是执行此操作

if(FileUtils.readFileToString(filename).contains(term)) 
   counter++;

#3


0  

I think this is sufficient information to start your work on this. Your lecturer has given details of each step that you need to do. Just need to write the code for it.

我认为这是开始你的工作的充分信息。您的讲师已经详细说明了您需要执行的每个步骤。只需要为它编写代码。

#4


0  

Only logical answer will be that you have 1 main loop in which you run through the different texts and inside that a loop to run through the words of the text . When the term is found use break; command to stop inner loop and increment the counters. So you need 2 loops , 1 array/arraylist of texts and the counter

只有逻辑上的答案是你有一个主循环,你在其中运行不同的文本,并在循环内部运行文本的单词。当找到该术语时使用break;命令停止内循环并增加计数器。所以你需要2个循环,1个数组/文本的arraylist和计数器