Open
Conversation
auberonedu
reviewed
Feb 18, 2025
Comment on lines
+36
to
+40
| while (scanner.hasNextLine()) { | ||
| String sentence = scanner.nextLine().toLowerCase(); | ||
|
|
||
| // Split the sentence by whitespace (s+ means one or more) | ||
| String[] words = sentence.split("\\s+"); |
There was a problem hiding this comment.
This works well! You can also use scanner.next to have the whitespace automatically handled for you.
Comment on lines
+53
to
+61
| if (withoutPeriod.contains(".")) { | ||
| // keep words that like dr.smith | ||
| tokens.add(word); | ||
| } else { | ||
| // split regular words ending with period as two separate elements | ||
| // This logics pretty funny | ||
| tokens.add(withoutPeriod); | ||
| tokens.add("."); | ||
| } |
Comment on lines
+25
to
+35
| @Test | ||
| void testTokenizerWithExtraSpaces() { | ||
| // Arrange | ||
| String input = "hello hi hi hi hello hello"; | ||
| Scanner scanner = new Scanner(input); | ||
| LowercaseSentenceTokenizer tokenizer = new LowercaseSentenceTokenizer(); | ||
| // Act | ||
| List<String> tokens = tokenizer.tokenize(scanner); | ||
| // Assert | ||
| assertEquals(List.of("hello","hi","hi","hi","hello","hello"), tokens); | ||
| } |
Comment on lines
+55
to
+70
| Map<String, List<String>> words = new HashMap<>(); | ||
|
|
||
|
|
||
| // Iterate through the first token up until the second to last | ||
| for (int i = 0; i < trainingWords.size() - 1; i++) { | ||
|
|
||
| String currentWord = trainingWords.get(i); | ||
| String nextWord = trainingWords.get(i+1); | ||
|
|
||
| // If current word isn't in map, add it | ||
| if (!words.containsKey(currentWord)) { | ||
| words.put(currentWord, new ArrayList<>()); | ||
| } | ||
| words.get(currentWord).add(nextWord); | ||
| } | ||
| neighborMap = words; |
Comment on lines
+120
to
+139
| if (context == null || context.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| // get last word from input | ||
| String lastWord = context.get(context.size()-1); | ||
|
|
||
| //Check words that come after the input | ||
| List<String> neighbors = neighborMap.get(lastWord); | ||
|
|
||
| if (neighbors == null || neighbors.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| //Select one of the next words | ||
| // Save it in variable called predictedWord | ||
| int index = (int)(Math.random() * neighbors.size()); | ||
| String predictedWord = neighbors.get(index); | ||
|
|
||
| return predictedWord; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.