2011-03-10 89 views
0

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

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

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

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

为了达到上述目的,我需要什么?我的意思是代码观点。对不起,我的编码很慢。不,我不希望获得免费代码。我只想了解指南的内容。我已经问过我的讲师,但这还不够。我只是想知道如何在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 
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 

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

+2

到目前为止你有什么?即使是伪代码也可以。哪些部件有问题? – Farrell 2011-03-10 14:51:26

+1

问题的所有部分都是自我解释性的。一步一步地做好工作。 – Deepak 2011-03-10 14:54:07

+0

是的,上面的文本几乎是伪代码,只是做他在那里写的东西,只是在java代码;) – Tobias 2011-03-10 15:01:25

回答

0

我刚接手一个快速浏览一下你的代码,但它看起来好像你的变量“matchedWord”计数的话一切准备就绪的数量。您只需更改定义它的位置,并在方法结尾处编写相应的println语句。

0

您的报价没有多大意义。然而,找到一个文件中的术语最简单的方法是做

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

我认为这是足够的信息来开始你的工作。您的讲师详细介绍了您需要执行的每个步骤。只需要为它编写代码。

+0

这将有助于http://www.dreamincode.net/forums/topic/48905-search-inside-a-text-file/ – ashishjmeshram 2011-03-10 14:54:31

0

唯一合乎逻辑的答案就是你有1个主循环,你在其中运行不同的文本,并在里面循环遍历文本的文字。当找到该术语时使用break;命令停止内部循环并增加计数器。所以,你需要2路,1个阵列/文本ArrayList和反