【问题标题】:How to remove lines with non word characters using java?如何使用java删除带有非单词字符的行?
【发布时间】:2013-07-24 09:29:02
【问题描述】:

您好,我已经使用 Apache tika 抓取了一些 html 文件并将文本内容写入文本文件,当我将内容写入文本文件时,我得到一些空格和一些不同的符号,所以当我使用 opennlp 分块解析器时试图解析这些文件行我在下面的代码中的ParserTool.parseLine 收到错误,这些行是非单词。

InputStream is = new FileInputStream("en-parser-chunking.bin");

    ParserModel model = new ParserModel(is);

    opennlp.tools.parser.Parser parser = ParserFactory.create(model);
    File dir = new File("C://htmlmetadata");
    File listDir[] = dir.listFiles();
    System.out.println("no of files:"+listDir.length);
    for (int i = 0; i < listDir.length; i++) 
    {

    String path=listDir[i].getAbsolutePath();
     System.out.println("file name"+listDir[i].getName());
      Scanner scanner = new Scanner(new FileInputStream(path), "UTF-8");

      while (scanner.hasNextLine())
        {
              String line=scanner.nextLine();
             if(line!=null)
                 {
                     Parse topParses[] = ParserTool.parseLine(line, parser, 1);
                        for (Parse p : topParses)
                        {
                            p.show();

                        }
                    System.out.println("line in if"+line);
                    System.out.println("line length in if"+line.length());
                 }
        }
}

我已经尝试检查 line.length>0 它也不起作用,因为行长度大于 0 但它包含一些特殊字符,所以请建议我获取其中包含单词的行。

谢谢

【问题讨论】:

    标签: java apache-tika opennlp


    【解决方案1】:

    遍历每个字符并

    if ((int(character)>=65 && int(character)<=90) || (int(character)>=97 && int(character)    <=122))
    continue
    
    else {
    
    //skip that line
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2023-03-10
      • 2010-12-09
      相关资源
      最近更新 更多