代码之家  ›  专栏  ›  技术社区  ›  DevRj

检查java中文字的文本文件中是否包含字符串

  •  2
  • DevRj  · 技术社区  · 7 年前

    我有一个来自github项目的文本文件(所有有效英语单词的集合),看起来像这样 words.txt

    resources 我的项目中的文件夹。

    我还有一个从mysql中的表中获得的行列表。

    这是我迄今为止尝试的:

     public static void englishCheck(List<String> rows) throws IOException {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        int lenght, occurancy = 0;
        for ( String row : rows ){
    
           File file = new File(classLoader.getResource("words.txt").getFile());
    
    
           lenght = 0;
    
           if ( !row.isEmpty()  ){
               System.out.println("the row : "+row);
               String[] tokens = row.split("\\W+");
               lenght = tokens.length;
               for (String token : tokens) {
    
                   occurancy = 0;
                   BufferedReader br = new BufferedReader(new FileReader(file));
    
                   String line;
                   while ((line = br.readLine()) != null ){
    
    
                       if ((line.trim().toLowerCase()).equals(token.trim().toLowerCase())){
                           occurancy ++ ;
    
                       }
                       if (occurancy == lenght ){ System.out.println(" this is english "+row);break;}
    
                   }
    
               }
    
    
    
    
    
           }
    
       }
    }
    

    List<String> raws = Arrays.asList(raw1, raw2, raw3 ) 等等

    3 回复  |  直到 7 年前
        1
  •  2
  •   ΦXocę 웃 Пepeúpa ツ    7 年前

    你可以用这个方法 List#containsAll(Collection)

    如果此列表包含 指定的集合。

    假设你有两个列表 MyListFromResources公司

    List<String> myListFromRessources = Arrays.asList("A", "B", "C", "D");
    List<String> myListFromRessources = Arrays.asList("D", "B");
    
    boolean myInter = myListFromRessources.containsAll(myListFromSQL);
    System.out.println(myInter);
    myListFromSQL = Arrays.asList("D", "B", "Y");
    myInter = myListFromRessources.containsAll(myListFromSQL);
    System.out.println(myInter);
    
        2
  •  1
  •   diguage Stephen Paul Lesniewski    7 年前

    你可以阅读 words.txt HashSet

    使用 boolean contains(Object o) boolean containsAll(Collection<?> c); 方法比较每个单词。 O(n) .

    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream("words.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    List<String> wordList = new LinkedList<String>(); // You do not know word count, LinkedList is a better way.
    String line = null;
    while ((line = reader.readLine()) != null) {
      String[] words = line.toLowerCase().split("\\W+");
      wordList.addAll(Arrays.asList(words));
    }
    Set<String> wordSet = new HashSet<String>(wordList.size());
    wordSet.addAll(wordList);
    
    
    // then you can use the wordSet to check. 
    // You shold convert the tokens to lower case.
    String[] tokens = row.toLowerCase().split("\\W+");
    wordSet.containsAll(Arrays.asList(tokens)); 
    
        3
  •  1
  •   Klitos Kyriacou    7 年前

    occurancy

    如果你的话。txt文件不是太大,并且你有足够的RAM可用,你可以通过读取文字来加快处理速度。txt文件在开始时放入内存。此外,您只需要调用toLowerCase()一次,而不是每次比较。但是,请注意区域设置。只要您没有任何非英语字符,例如德语eszett或希腊语sigma,以下代码就可以工作。

    public static void englishCheck(List<String> rows) throws IOException {
        final URI wordsUri;
        try {
            wordsUri = ClassLoader.getSystemResource("words.txt").toURI();
        } catch (URISyntaxException e) {
            throw new AssertionError(e); // can never happen
        }
    
        final Set<String> words = Files.lines(Paths.get(wordsUri))
                .map(String::toLowerCase)
                .collect(Collectors.toSet());
    
        for (String row: rows)
            if (!row.isEmpty()) {
                System.out.println("the row : " + row);
                String[] tokens = row.toLowerCase().split("\\W+");
                if (words.containsAll(Arrays.asList(tokens)))
                    System.out.println(" this is english " + row);
            }
    }