죄송합니다. 바보 같은 질문 인 경우지도로 작업하는 것이 좋습니다.Hashmap이 예상 값을받지 못합니다.
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)));
}
}
}
목적은 제공된 문자열 목록 "텍스트"에 따라 키와 값의 특정 세트로, 주어진 하늘의 맵, whatComesNext을 조립하는 것입니다. 각 키는 텍스트의 단어 쌍을 포함하는 문자열 목록입니다. 예를 들어, 7 개의 요소 목록이 주어지면, 키는 요소 [5]와 요소 [5]를 포함 할 때까지 요소 [0]과 요소 [1], 요소 [1]과 요소 [2] [6].
각 키에 할당 된 값은 키의 두 문자열 바로 뒤에 오는 텍스트의 요소가됩니다. 예를 들어 < 0 1> 키의 값은 < 2>입니다. "Hello there StackOverflow"문자열 목록이 있다면 도움이 필요합니다. " 해시 맵이
,것 (어디) ""(.split로 문자열을 분리) [안녕하세요/거기에 StackOverflow]
[이/StackOverflow에 ,, I]
[StackOverflow에,/I, 필요]
[I/필요, 당신]
[필요가/당신의 도움.]
여기서 슬래시는 두 요소가 포함 된 문자열 목록을 나타내며 슬래시 앞뒤의 문자열입니다.
여기서 약간의 잡기는 모든 문자열 목록 "텍스트"가 처음 두 요소를 ""로, 마지막 요소를 ""로 가정 할 수 있다는 것입니다. 이것들은 여전히 일반 키와 값으로 처리됩니다 (따라서 [/ Hello, StackOverflow]의 해시 쌍을 설득 할 수 있습니다). "이없는 첫 번째 키에는 다음 두 문자열의 값 목록이 있어야합니다.
잘하면 나는 당신을 아직 잃지 않았다. 여기에 내가 생각하고있는 것을 테스트하는 코드이다 :이를 위해
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!")));
, 나는 모든 어떻게 든 내가 대신 다음 두의 내 "masterHash"키 다음 다음 네 가지 요소를 반환한다는 사실을 제외하고 작동합니다 생각합니다.
EDIT : 첫 번째 두 개의 비 문자열을 포함하는 키가 나중에 루프에 덮어 쓰여질 경우 (덮어 쓰는 문자열이 같은 순서로 다시 표시되는 경우) 덮어 쓰기 된 값 목록 에 여전히 다음 두 개의 문자열.
질문을 완전히받지 못했지만 순서를 유지하기 위해 ArrayList 대신 LinkedList를 사용해야하며 문제를 해결할 수 있다고 생각합니다. –
질문을 완전히 변경하지 마십시오. 새 제품이있는 경우 별도로 게시하십시오. – shmosel