哈希图未收到预期值
Hashmap not receiving expected value
抱歉,如果这是一个愚蠢的问题,我对使用地图还很陌生。
static void redacted(Map<List<String>, List<String>> whatComesNext, List<String> text) {
List<String> maurice = new ArrayList<String>(); //feeds the double string key to the map whatComesNext
List<String> masterHash = new ArrayList<String>(); //keeps track of the first two non "<START>" keys
List <String> bryan = new ArrayList<String>(); //returns a double value to the map when masterhash appears
masterHash.add(text.get(2)); //adds third element of the list to masterHash
masterHash.add(text.get(3)); //adds fourth element to masterHash
for (int i=0; i<=text.size()-3; i++) {
maurice.clear();
maurice.add(text.get(i)); //gets nth element of the list and adds to maurice
maurice.add(text.get(i+1)); //gets element following the nth one
if (maurice.equals(masterHash)) { //assigns two strings to masterHash key instead of one
bryan.add(text.get(i+2));
bryan.add(text.get(i+3));
whatComesNext.put(masterHash, bryan);
}
else {
whatComesNext.put(maurice, Arrays.asList(text.get(i+2)));
}
}
}
目的是 assemble 给定的空映射 whatComesNext,根据提供的字符串列表“文本”具有一组特定的键和值。每个键都是一个字符串列表,其中包含一对来自文本的单词。例如,给定一个包含 7 个元素的列表,键将是一个包含元素 [0] 和元素 [1]、元素 [1] 和元素 [2] 等的列表,直到最后一个键包含元素 [5] 和[6].
分配给每个键的值将是文本中紧跟在键中两个字符串之后的元素。例如,键 <0 1> 的值为 <2>。如果您有字符串列表“你好,Whosebug,我需要你的帮助。” (你用 .split(" ") 分隔字符串的地方)散列图将是
[Hello/there, Whosebug]
[there/Whosebug,, I]
[Whosebug,/I, need]
[I/need, your]
[need/your, help.]
其中斜线表示包含两个元素的字符串列表,即斜线前后的字符串。
虽然这里有一个小问题,但可以假定每个字符串列表“文本”的前两个元素为“”,最后一个元素为“”。这些仍然被视为常规键和值(因此 [/Hello, Whosebug] 的散列对是合理的)第一个不包含 "" 的键必须具有以下两个字符串的值列表。
希望我还没有失去你。这是测试我的想法的代码:
List<String> prisoner =
Arrays.asList("<START> <START> I am not a number. I am a free man! <END>".split(" "));
Map<List<String>, List<String>> whatComesNext = new LinkedHashMap<>();
MarkovText.learnFromText(whatComesNext, prisoner);
System.out.println(whatComesNext);
assertEquals(10, whatComesNext.size());
assertEquals(Arrays.asList("not", "a"),
whatComesNext.get(Arrays.asList("I", "am")));
assertEquals(Arrays.asList("free"),
whatComesNext.get(Arrays.asList("am", "a")));
assertEquals(Arrays.asList("<END>"),
whatComesNext.get(Arrays.asList("free","man!")));
为此,我认为一切正常,除了我 return 我的“masterHash”键后面的四个元素而不是接下来的两个元素。
编辑:我还需要提到,如果包含前两个非“”字符串的键在循环中稍后被覆盖(如果这些字符串再次以相同的顺序出现),那么被覆盖的值列表 仍然包含以下两个字符串。
您的代码的一个大问题是您正在更改地图键的状态,在将该键放入地图后,您 永远不要 这样做。合同被打破,它影响了地图的每一个操作。
您应该为每对单词创建一个新的键对象。一个更好的选择是使用不可变对象作为映射键。
抱歉,如果这是一个愚蠢的问题,我对使用地图还很陌生。
static void redacted(Map<List<String>, List<String>> whatComesNext, List<String> text) {
List<String> maurice = new ArrayList<String>(); //feeds the double string key to the map whatComesNext
List<String> masterHash = new ArrayList<String>(); //keeps track of the first two non "<START>" keys
List <String> bryan = new ArrayList<String>(); //returns a double value to the map when masterhash appears
masterHash.add(text.get(2)); //adds third element of the list to masterHash
masterHash.add(text.get(3)); //adds fourth element to masterHash
for (int i=0; i<=text.size()-3; i++) {
maurice.clear();
maurice.add(text.get(i)); //gets nth element of the list and adds to maurice
maurice.add(text.get(i+1)); //gets element following the nth one
if (maurice.equals(masterHash)) { //assigns two strings to masterHash key instead of one
bryan.add(text.get(i+2));
bryan.add(text.get(i+3));
whatComesNext.put(masterHash, bryan);
}
else {
whatComesNext.put(maurice, Arrays.asList(text.get(i+2)));
}
}
}
目的是 assemble 给定的空映射 whatComesNext,根据提供的字符串列表“文本”具有一组特定的键和值。每个键都是一个字符串列表,其中包含一对来自文本的单词。例如,给定一个包含 7 个元素的列表,键将是一个包含元素 [0] 和元素 [1]、元素 [1] 和元素 [2] 等的列表,直到最后一个键包含元素 [5] 和[6].
分配给每个键的值将是文本中紧跟在键中两个字符串之后的元素。例如,键 <0 1> 的值为 <2>。如果您有字符串列表“你好,Whosebug,我需要你的帮助。” (你用 .split(" ") 分隔字符串的地方)散列图将是
[Hello/there, Whosebug]
[there/Whosebug,, I]
[Whosebug,/I, need]
[I/need, your]
[need/your, help.]
其中斜线表示包含两个元素的字符串列表,即斜线前后的字符串。
虽然这里有一个小问题,但可以假定每个字符串列表“文本”的前两个元素为“”,最后一个元素为“”。这些仍然被视为常规键和值(因此 [/Hello, Whosebug] 的散列对是合理的)第一个不包含 "" 的键必须具有以下两个字符串的值列表。
希望我还没有失去你。这是测试我的想法的代码:
List<String> prisoner =
Arrays.asList("<START> <START> I am not a number. I am a free man! <END>".split(" "));
Map<List<String>, List<String>> whatComesNext = new LinkedHashMap<>();
MarkovText.learnFromText(whatComesNext, prisoner);
System.out.println(whatComesNext);
assertEquals(10, whatComesNext.size());
assertEquals(Arrays.asList("not", "a"),
whatComesNext.get(Arrays.asList("I", "am")));
assertEquals(Arrays.asList("free"),
whatComesNext.get(Arrays.asList("am", "a")));
assertEquals(Arrays.asList("<END>"),
whatComesNext.get(Arrays.asList("free","man!")));
为此,我认为一切正常,除了我 return 我的“masterHash”键后面的四个元素而不是接下来的两个元素。
编辑:我还需要提到,如果包含前两个非“”字符串的键在循环中稍后被覆盖(如果这些字符串再次以相同的顺序出现),那么被覆盖的值列表 仍然包含以下两个字符串。
您的代码的一个大问题是您正在更改地图键的状态,在将该键放入地图后,您 永远不要 这样做。合同被打破,它影响了地图的每一个操作。
您应该为每对单词创建一个新的键对象。一个更好的选择是使用不可变对象作为映射键。