0

同じ応答を返す「crash」や「crashes」などの特定のキーを持つHas​​hMapがあります。シノニムをresponseMapの一意のキーにマップする新しいHashMapを作成したいと思います(たとえば、synonymMapの「crash」、「crashes」、「crashed」を「crash」にマップします)。

 private void fillSynonymMap()
  {
    synonymMap.put("crash", "crash");
    synonymMap.put("crashes", "crash");
    synonymMap.put("crashed", "crash");
  }

私がこだわっているのは、以下のコードを単純化できるように、これらのキーを入力する方法です。

 private void fillResponseMap()
{
    responseMap.put("crash", 
                    "Well, it never crashes on our system. It must have something\n" +
                    "to do with your system. Tell me more about your configuration.");
    responseMap.put("crashes", 
                    "Well, it never crashes on our system. It must have something\n" +
                    "to do with your system. Tell me more about your configuration.");\
   responseMap.put("crashed", 
                    "Well, it never crashes on our system. It must have something\n" +
                    "to do with your system. Tell me more about your configuration.");
}



public String generateResponse(HashSet<String> words)
{
    for (String word : words) {
        String response = responseMap.get(word);
        if(response != null) {
            return response;
        }
    }

    // If we get here, none of the words from the input line was recognized.
    // In this case we pick one of our default responses (what we say when
    // we cannot think of anything else to say...)
    return pickDefaultResponse();
}
4

2 に答える 2

1

少しいじった後、デフォルトのメッセージを返す前に同義語を探す関数を書きました。

public String getResponse()
{
    HashMap<String, String> responseMap = new HashMap<String, String>();
    HashMap<String, String> synonymMap = new HashMap<String, String>();

    responseMap.put("crash", "Hello there");
    // Load the response value.
    synonymMap.put("crash", "crash");
    synonymMap.put("crashed", "crash");
    synonymMap.put("crashes", "crash");
    // Load the synonyms.

    String input = "crashed";
    // Select input value.

        if(responseMap.containsKey(input))
        {
            // Response is already mapped to the word.
            return responseMap.get(input);
        }
        else
        {
            // Look for a synonym of the word. 
            String synonym = synonymMap.get(input);
            if(!synonym.equals(input) && responseMap.containsKey(synonym))
            {
                // If a new value has been found that is a key..
                return responseMap.get(synonym);
            }
        }
        // If no response, set default response.
    input = "This is a default response";
    return input;
}

ご覧のとおり、関数は最初にキーが存在するかどうかを確認します。そうでない場合は、同義語を試みます。そのシノニムがテストに合格しない場合、下部のデフォルト コードに移動します。これにより、入力がデフォルト値に設定され、代わりにそれが返されます:)

于 2013-02-26T08:05:29.180 に答える
0

2 番目のマップを使用できます。

最初のマップはシノニムを基本キーに変換し、それを回答付きの 2 番目のマップに使用できます。

これにより、実際の応答マップを拡大することなく、シノニムを柔軟に拡張することもできます。

また、この場合、別のタイプを実際に answser マップのキーとして使用できます。シノニム マップの値の型と同じである必要があります。

そうすれば、あなたも使うことができます

Map<String, YourEnum> and Map<YourEnum, String> 

Map インターフェイスの実装として EnumMap を使用します。

于 2013-02-26T07:50:53.530 に答える