Skip to content

RambleBot!!#26

Open
SketchRudy wants to merge 11 commits intogrc-cohort-21:mainfrom
SketchRudy:main
Open

RambleBot!!#26
SketchRudy wants to merge 11 commits intogrc-cohort-21:mainfrom
SketchRudy:main

Conversation

@SketchRudy
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@auberonedu auberonedu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

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+");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(".");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny or no, great logic!

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);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great logic!

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice null/empty checks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants