【问题标题】:java string matching from a large text file issue来自大型文本文件问题的java字符串匹配问题
【发布时间】:2023-03-23 16:14:01
【问题描述】:

我想从一个大的文本文件中实现一个字符串匹配的任务。 1.替换所有非字母数字字符 2.统计文本文件中特定词条的数量。例如,匹配词“tom”。匹配不区分大小写。所以我应该算上“汤姆”这个词。但是不应该计算明天这个词。

code template one:
    try {
           in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
        } catch (FileNotFoundException e1) {
           System.out.println("Not found the text file: "+inputFile);
         }
    Scanner scanner = null;
    try {
        while (( line = in.readLine())!=null){  
               String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
               scanner = new Scanner(newline);
               while (scanner.hasNext()){
                       String term = scanner.next();
                   if (term.equalsIgnoreCase(args[1]))
                   countstr++;
               }
         }
     } catch (IOException e) {
    e.printStackTrace();
    }

code template two:
   try {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
       } catch (FileNotFoundException e1) {
           System.out.println("Not found the text file: "+inputFile);
         }
   Scanner scanner = null;
   try {
        while (( line = in.readLine())!=null){  
               String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
               String[] strArray=newline.split(" ");//split by blank space
                       for (int =0;i<strArray.length;i++)
                               if (strArray[i].equalsIgnoreCase(args[1]))
                                      countstr++;
               }
         }
     } catch (IOException e) {
    e.printStackTrace();
   }

通过运行这两个代码,我得到了不同的结果,Scanner 似乎得到了正确的一个。但是对于大文本文件,Scanner 的运行速度比后者慢得多。谁能告诉我原因并给出更有效的解决方案。

【问题讨论】:

    标签: java string file text


    【解决方案1】:

    在您的第一个方法中。您不需要使用两个扫描仪。带“”的扫描仪不适合大线条。

    您的行已转换为小写。所以你只需要在外面做一次小写的键。并在循环中执行等于

    或者获取线路

    String key = String.valueOf(".*?\\b" + "Tom".toLowerCase() + "\\b.*?");
            Pattern p = Pattern.compile(key);
            word = word.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
            Matcher m = p.matcher(word);
            if (m.find()) {
                countstr++;
            } 
    

    我个人会为大文件选择 BufferedReader 方法。

    String key = String.valueOf(".*?\\b" + args[0].toLowerCase() + "\\b.*?");
             Pattern p = Pattern.compile(key);
             try (final BufferedReader br = Files.newBufferedReader(inputFile,
                        StandardCharsets.UTF_8)) {
                    for (String line; (line = br.readLine()) != null;) {
                        // processing the line.
                        line = line.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
                        Matcher m = p.matcher(line);
                        if (m.find()) {
                            countstr++;
                        }           
                    }
             }
    

    在 Java 7 中提供示例。根据需要进行更改!

    【讨论】:

      猜你喜欢
      • 2011-11-25
      • 2014-09-18
      • 1970-01-01
      • 2011-06-15
      • 2016-11-11
      • 2020-07-14
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多