zoukankan      html  css  js  c++  java
  • 文档中某些特定数据查询

    问题背景:

    需要在一个法律文档中查询出所有的法律法规,格式类似《这样滴》,找出文档中所有《xx》数据(去除重复后的数据)。

    当然,对于文档中非法律法规,例如一本书的名字《wireshark网络分析就这么简单》,就得人工的去除了,当然也可以解决,不过在于本次问题,本程序就可以解决了,手工就手工吧。

    案例:

    赛先生最近读了本《小王子》,不过他不喜欢《小王子》,他喜欢读《C语言超高级编程》之类的书,然后他就去图书馆借了《C语言超高级编程》、《网络爬虫没问题》、《治疗脊椎病那些事》、《SinuxOS》,对于上面的书他是非常的喜欢。

    程序输出结果:

        《C语言超高级编程》
        《网络爬虫没问题》
        《治疗脊椎病那些事》
        《小王子》
       《SinuxOS》

    解题思路:

    将所有文本copy到txt中,然后利用程序遍历该文档,每次读一行,若该行存在‘《’则将该行中所有的‘《’开始到‘》’结尾的数据全部输出(存放到list中)。以上只是对数据的查找,还没有去重处理,想到的是使用“HashSet(Java)”,进行数据去重。

    时间:

    估计时间:20分钟

    实际耗时:40+(原因:1.好久没写了;2.Java处理中文问题;3. 去重问题;4.想找个现成的,修改花了点时间)

    编程语言:Java

     1 package textProcess;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.util.ArrayList;
     7 import java.util.HashSet;
     8 import java.util.List;
     9 
    10 public class quChong {
    11     static List<String> list = new ArrayList<>();
    12 
    13     public static void searchLine(File file) {
    14         try {
    15             BufferedReader br = new BufferedReader(new FileReader(file));
    16             String line = null;
    17             while ((line = br.readLine()) != null) {
    18                 if (line.indexOf("《") != -1) {
    19                     addToList(line);
    20                 }
    21             }
    22             br.close();
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25         }
    26     }
    27 
    28     public static void addToList(String line) {
    29         int i = 0;
    30         String s = "";
    31         for (; i < line.length(); i++) {
    32             s = "";
    33             if (line.charAt(i) == '《') {
    34                 while (line.charAt(i) != '》') {
    35                     s += line.charAt(i);
    36                     i++;
    37                 }
    38                 s += "》";
    39 //                System.out.println(s);
    40             }
    41             list.add(s);
    42         }
    43 
    44     }
    45 
    46     public static void main(String[] args) {
    47         File file = new File("F:\a.txt");
    48         searchLine(file);
    49         deDuplication(list);
    50     }
    51 
    52     public static void deDuplication(List<String> list) {
    53         HashSet<String> set = new HashSet<>();
    54         set.addAll(list);
    55         for (String string : set) {
    56             System.out.println(string);
    57         }
    58     }
    59 }
  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/A--Q/p/10149569.html
Copyright © 2011-2022 走看看