简体   繁体   English

用其全貌替换缩写/俚语

[英]Replacing abbreviations/slangs with their fullforms

I am using a HashMap to store the full forms for abbreviations. 我使用HashMap存储缩写的完整表单。

public class Test {
    public static void main(String[] args) {
        Map<String, String> slangs = new HashMap<String, String>();
        slangs.put("lol", "laugh out loud");
        slangs.put("r", " are ");
        slangs.put("n", " and ");
        slangs.put("idk", " I don't know ");
        slangs.put("u", " you ");
        Set set = slangs.entrySet();
        Iterator i = set.iterator();

        String sentence = "lol how are you";
        StringBuilder sb = new StringBuilder();

        for (String word : sentence.split(" ")) {
            while(i.hasNext()) {
                Map.Entry<String, String> me = (Map.Entry)i.next();
                if (word.equalsIgnoreCase(me.getKey())) {
                    sb.append(me.getValue());
                    continue;
                }
                sb.append(word);
            }
        }
        System.out.println(sb.toString());
    }
}

The Output is: 输出是:

lollollollaugh out loudlol

What is wrong here and how do I solve it? 这里有什么问题,如何解决?

You are not supposed to iterate over the entries to find a match, you are supposed to use get(Object key) or getOrDefault(Object key, V defaultValue) to get the full form of a given abbreviation, otherwise instead of getting your full form with a time complexity of O(1) , you will get it with a O(n) which is of course not good in term of performances, you would lose the real benefit of having your key/value pairs in a Map . 您不应该迭代条目来查找匹配项,您应该使用get(Object key)getOrDefault(Object key, V defaultValue)来获取给定缩写的完整形式,否则不要获取完整的表单如果时间复杂度为O(1) ,你会得到一个O(n) ,这当然在表现方面不好,你将失去在Map中拥有你的键/值对的真正好处。 If you did it because of the case, simply put your keys only in lower case in your map and call get or getOrDefault with the word in lower case as below: 如果您因为这种情况而这样做,只需将您的密钥仅以小写字母放在地图中,并使用小写字母调用getgetOrDefault ,如下所示:

So your loop should be something like: 所以你的循环应该是这样的:

for (String word : sentence.split(" ")) {
    // Get the full form of the value of word in lower case otherwise use
    // the word itself
    sb.append(slangs.getOrDefault(word.toLowerCase(), String.format(" %s", word)));
}

Output: 输出:

laugh out loud how are you

Using the Stream API , it could simply be: 使用Stream API ,它可能只是:

String result = Pattern.compile(" ")
    .splitAsStream(sentence)
    .map(word -> slangs.getOrDefault(word.toLowerCase(), word))
    .collect(Collectors.joining(" "));

Don't loop over the keys in the dictionary. 不要遍历字典中的键。 Instead, just check whether the key is in the map and get the corresponding value. 相反,只需检查键是否在地图中并获取相应的值。 Also, don't forget to add the spaces back into the combined sentence. 另外,不要忘记将空格添加回组合句子中。

for (String word : sentence.split(" ")) {
    if (slangs.containsKey(word.toLowerCase())) {
        sb.append(slangs.get(word.toLowerCase()));
    } else {
        sb.append(word);
    }
    sb.append(" ");
}

If you are using Java 8, you can also use String.join , Map.getOrDefault and Streams: 如果您使用的是Java 8,还可以使用String.joinMap.getOrDefault和Streams:

String s = String.join(" ", Stream.of(sentence.split(" "))
        .map(word -> slangs.getOrDefault(word.toLowerCase(), word))
        .toArray(n -> new String[n]));

This latter approach also has the benefit of not adding a space before the first or after the last word in the sentence. 后一种方法还具有不在句子中的第一个单词之后或之后添加空格的益处。

Simply, I think you just need to check if slangs contain this keyword or not. 简单地说,我认为您只需要检查slangs包含此关键字。 Please check my code. 请检查我的代码。

 public class Test {
    public static void main(String[] args) {

      Map<String, String> slangs = new HashMap<String, String>();
      slangs.put("lol", "laugh out loud");
      slangs.put("r", " are ");
      slangs.put("n", " and ");
      slangs.put("idk", " I don't know ");
      slangs.put("u", " you ");

      String sentence = "lol how are you";
      String[] words = sentence.split(" ");

      for (String word : words) {
        String normalizeWord = word.trim().toLowerCase();
        if(slangs.containsKey(normalizeWord)) {
            sentence = sentence.replace(word, slangs.get(normalizeWord));
        }
    }
    System.out.println(sentence);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM