1

我的问题是我有一个输入文件,我必须在没有 4 个单词的输出文件中重写文本(“a”),(“the”),(“A”),(“The”)。我设法解决“a”和“the”,但不能解决“A”和“The”。你能帮我写代码吗?提前致谢。下面是问题,输入和我的代码:

问题:

英语中的“a”和“the”大部分都可以从句子中去掉而不影响意思。这是压缩文本文件大小的机会!编写一个程序,逐行输入一个文本文件,然后写出一个新的文本文件,其中每一行都删除了无用的单词。

首先编写一个简单版本的程序,将每行中的子字符串“a”和“the”替换为一个空格。这将删除许多单词,但有时这些单词出现在行首或行尾,有时单词以大写字母开头。因此,请改进您的第一个程序,以便它也能处理这些情况。

C:>java Remover <verbose.txt> terse.txt

注意:String 类有多种 replace() 方法可以简化这个程序。尝试在不使用它们的情况下编写此程序。

输入文件:

小说是描述虚构人物和事件的长篇散文叙事,通常采用连续故事的形式。这种类型的历史根源于中世纪和早期现代浪漫主义领域以及中篇小说的传统。

代码:

import java.util.Scanner;
import java.io.*;

class File_Compressor
{
 public static void main(String[]args) throws IOException
  {  
  int loc=0;
  String line="";

   File input=new File ("input.txt");
   Scanner scan=new Scanner(input);
   File output=new File("Hello2.java");
   PrintStream print=new PrintStream(output);

   while (scan.hasNext())
       {line=scan.nextLine().trim();

            while(line.indexOf("A")>0||line.indexOf("The")>0||line.indexOf(" a")>0||line.indexOf(" the ")>0)
   {
   if (line.indexOf("A")>0)
     {loc=line.indexOf("A");
     line=line.substring(loc+1);}

     else if (line.indexOf("The")>0)
     {loc=line.indexOf("The");
     line=line.substring(loc+3);
         }

     else if (line.indexOf(" a ")>0)
     {loc=line.indexOf(" a ");
     left=line.substring(0,loc+1);
     right=line.substring(loc+2);
     line=left+right;}

     else if (line.indexOf(" the ")>0)
     {loc=line.indexOf(" the ");
     left=line.substring(0,loc+1);
     right=line.substring(loc+4);
     line=left+right;}
     }
     print.println(line);
     }
 }

}

4

4 回答 4

1

由于您正在逐行读取文件,因此将每一行分成一个单词数组

line=scan.nextLine().trim();
String[] words = line.split("\\s+");
String sentence = "";
for (int i = 0; i < words.length; i++) {
    if(!(words[i].equalsIgnoreCase("a") || words[i].equalsIgnoreCase("the"))){
        sentence += words[i] + " ";
    }
}
System.out.println(sentence);
于 2013-07-31T13:08:22.207 回答
0

你应该使用 hasSet 类,它有 remove 方法,所以我希望这个小例子对你有帮助

我的文字:

The a a dssfdsfd The a the an fdfdggth
gtrfhtrht a the The fdsfddg

我的输出:

[fdfdggth, dssfdsfd, fdsfddg, gtrfhtrht]

公共类定名{

     private static  HashSet<String> hS = new HashSet<String>();    

     public static void main(String[]args) throws IOException
      {  
      int loc=0;
      String line="";

       File input=new File ("C:\\deneme\\inputstack.txt");
       Scanner scanner=new Scanner(input);
       File output=new File("Hello2.java");
       PrintStream print=new PrintStream(output);


        while (scanner.hasNext()) {
            if (scanner.hasNextDouble()) {
                Double doubleValue = scanner.nextDouble();


            }
            else {

                String stringValue = scanner.next();
                  hS.add(stringValue);

                    hS.remove("the");
                    hS.remove("a");
                    hS.remove("The");
                    hS.remove("an");          

            }

        }

         System.out.println(hS);
}       


}
于 2013-07-31T13:01:38.713 回答
0

您可以使用 RegEx 一步完成此操作。但我没有时间创建一个快递。对不起。但是对于那些简单的任务,我通常使用 apache commons lang。在实际的 3.1 版本中,您会发现 Class StringUtils 带有方法 removeStartIgnoreCase,您可以使用该方法。

例子:

line = StringUtils.removeStartIgnoreCase(line,"a ");
line = StringUtils.removeStartIgnoreCase(line,"the ");

我认为这很简单明了。我首选的解决方案包括将要删除的单词打包成一个数组或类似的东西,然后遍历它们以从行首删除。

以下是 apache commons lang 的链接:

http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/index.html

http://commons.apache.org/proper/commons-lang/

于 2013-07-31T13:02:02.453 回答
0

对您的代码稍作修改可能已经可以解决问题。我没有机会彻底阅读它,但你可以试试这个(扩展The等):

if (line.startsWith("A ")) {
 loc=line.indexOf("A ");
 line=line.substring(loc+2);
}

不过有几个假设:

  • 每行包含一个句子
  • 只有空格用作空格(没有制表符)

附带说明:您的内部 while 条件应该与内部的测试匹配,即您应该寻找" a "而不是" a".

另一种选择是通过PatternandMatcher类使用正则表达式,即实现String.replaceAll(...)你自己的逻辑——如果允许的话。

于 2013-07-31T13:08:44.967 回答