简体   繁体   English

使用Hashmaps了解两个或更多键

[英]Understand two or more keys with Hashmaps

I am having an issue with a hashmap. 我有一个hashmap的问题。 In my hashmap method i want to have two or more keywords as a key, oppose to having one. 在我的hashmap方法中,我希望有两个或更多关键字作为关键字,反对拥有一个关键字。 For example I want the user to input some sentence containing two or more keywords assuming "professor name" is a keyword. 例如,我希望用户输入包含两个或更多关键字的句子,假设“教授名称”是关键字。 For example 例如

    String[] temp3 = { "instructor","teacher","mentor" };
    responses.put("professor name", temp3);

And the user enters "what is the professor name" the code just hangs. 并且用户输入代码刚刚挂起的“教授姓名是什么”。 But on the other hand, if 但另一方面,如果

  String[] temp3 = { "instructor","teacher","mentor" };
    responses.put("professor", temp3);

the code works. 代码有效。 So I want to be able to enter some sentence containing two or more keywords oppose to one. 所以我希望能够输入一个包含两个或更多关键词的句子。 I will appreciate any sort of assistance. 我会感谢任何形式的帮助。

Here is my hashmap method 这是我的hashmap方法

  private static HashMap<String, String[]> populateSynonymMap() {

    String[] temp1 = { "instructor","teacher","mentor" };
    responses.put("professor name", temp1);
    String[] temp2 = { "amount of test","test load","quantity of test" };

    return responses;
}

and here is my main method 这是我的主要方法

    public static void main(String args[]) throws ParseException, IOException {
    /* Initialization */
    HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();
    synonymMap = populateSynonymMap(); // populate the map


    Scanner scanner = new Scanner(System.in);
    String input = null;
   /*End Initialization*/
    System.out.println("Welcome To DataBase ");
    System.out.println("What would you like to know?");

    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();
    String[] inputs = input.split(" ");
      for (String ing : inputs) { // iterate over each word of the sentence.
        boolean found = false;
        for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) {
            String key = entry.getKey();
            String[] value = entry.getValue();
            if (key.equals(ing) || Arrays.asList(value).contains(ing)) {

                found = true;
                parseFile(entry.getKey());``
            }
          break;
     }
                if (found) {
            break;
        }


    }
}

Assuming that the ParseFile method works 假设ParseFile方法有效

Use the type HashMap<List<String>,String[]> or HashMap<Set<String>,String[]> for the cases where the order does and does not matter respectively, in place of HashMap<String,String[]> . 使用类型HashMap<List<String>,String[]>HashMap<Set<String>,String[]>用于命令和无关紧要的情况,代替HashMap<String,String[]>

In the first case, your keys are of type List<String> , and in the second case they are of type Set<String> . 在第一种情况下,键的类型为List<String> ,在第二种情况下,它们的类型为Set<String>

Best practice would be to wrap all keys with Collections.unmodifiable* methods before using them as keys - if they are modified while in HashMap entires, the behavior of your program will be undefined. 最佳做法是在将所有键用作键之前用Collections.unmodifiable *方法包装它们 - 如果它们在HashMap中被修改,则程序的行为将是未定义的。

I would use String contains method to check multiple words. 我会使用String contains方法来检查多个单词。 This will solve your problem of checking single word like 'professor' or multiple words like 'What is your professor name' problem. 这将解决您检查单个单词(如“教授”)或多个单词(如“您的教授名称是什么”)问题的问题。

    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();

    for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) {
        String key = entry.getKey();
        String[] value = entry.getValue();
        if (input.contains(key) || key.contains(input)) {
            System.out.println("Input found in database -> " +Arrays.asList(value));
            break;
        }
    }

Now this will make it work with single word or multiple word. 现在,这将使它与单个单词或多个单词一起使用。 But this works when words are entered in same order. 但是,当以相同的顺序输入单词时,这可以工作。 You can make it as elaborate as you like. 你可以随心所欲地制作它。

For example you may extend this to separate words of input and output from sentence from the sets and then find how many words matches etc.And I recommend that you use some different data structure which makes this task natural looking. 例如,您可以将其扩展为从集合中的句子中分隔输入和输出的单词,然后查找有多少单词匹配等。我建议您使用一些不同的数据结构,这使得此任务看起来很自然。 You may think about having your database organized by words and synonyms relation so then you don't have to worry about cases where value from one synonym list has other synonym etc. And lookup can be just based on words. 您可以考虑通过单词和同义词关系来组织数据库,这样您就不必担心来自一个同义词列表的值具有其他同义词等的情况。并且查找可以仅基于单词。

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

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