【问题标题】:Scanning string for keywords of various lengths扫描各种长度关键字的字符串
【发布时间】:2018-01-31 16:08:43
【问题描述】:

我想扫描我的文档,将其拆分为某些关键字的单词数组,例如“燃料”、“车辆”、“车辆租赁”、“资产类型维护”等。问题是关键字的长度不同。一种是单字关键词,另一种是四字关键词。目前我正在逐字扫描,但这不喜欢多字关键字的想法,例如“车辆租赁”。

我可以做些什么来改进我的代码并使用多个单词关键字? 这就是现在的样子

public void findKeywords(POITextExtractor te, ArrayList<HashMap<String,Integer>> listOfHashMaps, ArrayList<Integer> KeywordsFound, ArrayList<Integer> existingTags) {

    String document = te.getText().toString();
    String[] words = document.split("\\s+");
    int wordsNo = 0;
    int keywordsMatched = 0;

    try {
        for(String word : words) {
            wordsNo++;

            for(HashMap<String, Integer> hashmap : listOfHashMaps) {
                if(hashmap.containsKey(word) &&  !KeywordsFound.contains(hashmap.get(word)) && !existingTags.contains(hashmap.get(word))) {
                    KeywordsFound.add(hashmap.get(word));
                    keywordsMatched++;
                    System.out.println(word);
                }
            }
        }
        System.out.println("New keywords found: " + KeywordsFound);
        System.out.println("Number of words in document = " + wordsNo);
        System.out.println("Number of keywords matched: " + keywordsMatched);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

我已经包含了我的方法。如果还有什么需要理解我的代码,请发表评论。

@更新

public void findKeywords(POITextExtractor te, ArrayList<HashMap<String,Integer>> listOfHashMaps, ArrayList<Integer> KeywordsFound, ArrayList<Integer> existingTags) {

    String document = te.getText().toString();
    String[] words = document.split("\\s+");
    int wordsNo = 0;
    int keywordsMatched = 0;

    for(HashMap<String, Integer> hashmap : listOfHashMaps) {
         Iterator it = hashmap.entrySet().iterator();
         while (it.hasNext()) {
             Map.Entry pair = (Map.Entry)it.next();
             //System.out.println(pair.getKey() + " = " + pair.getValue());
             it.remove(); // avoids a ConcurrentModificationException

             if(document.contains((CharSequence) pair.getKey()) && !KeywordsFound.contains(pair.getValue()) && !existingTags.contains(pair.getValue())) {
                 System.out.println(pair.getKey());
                 KeywordsFound.add((Integer) pair.getValue());
                 keywordsMatched++;  
             }
         }
    }

    System.out.println("New keywords found: " + KeywordsFound);
    System.out.println("Number of keywords matched: " + keywordsMatched);
}

【问题讨论】:

  • 为什么不使用 indexOf docs.oracle.com/javase/7/docs/api/java/lang/… ?这可以为您找到多个单词。像document.indexOf("Asset Type Maintenance"); 这应该返回找到的单词的索引。您不需要为此拆分字符串。
  • 我已经做了一些测试,并且在文档上为 3500 个关键字做 indexOf 并不是很有效。根据 3500 个关键字扫描每个单词并继续下一个关键字要快得多。这就是为什么我把它分成单词。

标签: java arrays search hashmap apache-poi


【解决方案1】:

另一种方法是通过搜索字符串分割字符串。 例如。

List<String> searchString = new ArrayList<>();
searchString.add("Fuel");
searchString.add("Asset Type Maintenance");
searchString.add("Vehicle Leasing");

String document=""; // Assuming that you complete string is initilaized here.

for (String str : searchString) {
    String[] tempDoc=document.split(str);
    System.out.println(str + " is repated "+ (tempDoc.length-1) + " times");

请注意,这可能会在垃圾收集中破坏 JVM。 您可以自己比较性能。

【讨论】:

  • 这是我最初的想法,大文件文档花了很长时间,最终内存不足。请看我更新的方法。
【解决方案2】:

我认为这是一种家庭作业。所以: 看看string search algorithms 在较大字符串中搜索子字符串(模式)。

然后假设您使用其中一种算法,但您没有在更大的字符序列中搜索一个字符序列(模式),而是在一个字符串(模式)序列中搜索更大的字符串序列。 (所以你只有一个不同的、更大的字母表)

【讨论】:

    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多