嗨,我已经使用 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 但它包含一些特殊字符,所以请建议我获取其中包含单词的行。
谢谢
最佳答案
遍历每个字符并
if ((int(character)>=65 && int(character)<=90) || (int(character)>=97 && int(character) <=122))
continue
else {
//skip that line
}
关于java - 如何使用java删除包含非单词字符的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17830109/