Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A project to exercise Java, JUnit, git, GitHub, and code-reading skills. Student

### Academic Honesty

THIS IS AN INDIVIDUAL PROJECT. The following is not allowed:
THIS IS AN INDIVIDUAL PROJECT. The following is not allowed:
- You MAY NOT copy any code from an AI.
- You MAY NOT paste any of the project or your code into an AI.
- You MAY NOT copy another student's code.
Expand All @@ -20,7 +20,7 @@ You may:

### Commits

YOU ARE EXPECTED TO MAKE SMALL, FREQUENT COMMITS. Doing so is good practice and helps me see that it's less likely you pasted in a large part of your solution from elsewhere.
YOU ARE EXPECTED TO MAKE SMALL, FREQUENT COMMITS. Doing so is good practice and helps me see that it's less likely you pasted in a large part of your solution from elsewhere. (Changing Read me for the first commit)

### Timeline
This is a large, difficult project. Start early, and get help when you need it.
Expand Down
89 changes: 89 additions & 0 deletions goodMusic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
There was a blooming spider
Went up a blooming spout
And down came the rain
And washed the spider out
Out came the sun
And dried up all the rain
But that bloody blooming son of a gun
Went up that spout again
Humpty Dumpty sat on a wall,
Humpty Dumpty had a great fall.
All the king’s horses and all the king’s men
Couldn’t put Humpty together again.
The wheels on the bus go round and round
Round and round, round and round
The wheels on the bus go round and round
All through the town
The wipers on the bus go “Swish, swish, swish,
Swish, swish, swish, swish, swish, swish”
The wipers on the bus go “Swish, swish, swish”
Comment on lines +17 to +19

Choose a reason for hiding this comment

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

Definitely good music. Looks like you might be missing the rambleOutput.txt though asked for in Wave 6?

All through the town.
The people on the bus go, “chat, chat, chat,
cha,,chat chat,chat chat ,chat
The people on the bus go, “, chat,chat,chat
All through the town.
The horn on the bus go “Beep, beep, beep
Beep, beep, beep, beep, beep, beep”
The horn on the bus go “Beep, beep, beep”
All through the town.
The wheels on the bus go round and round
Round and round, round and round
The wheels on the bus go round and round
All through the town
The wipers on the bus go “Swish, swish, swish,
Swish, swish, swish, swish, swish, swish”
The wipers on the bus go “Swish, swish, swish”
All through the town.
The people on the bus go, “chat, chat, chat,
cha,,chat chat,chat chat ,chat
The people on the bus go, “, chat,chat,chat
All through the town.
The horn on the bus go “Beep, beep, beep
Beep, beep, beep, beep, beep, beep”
The horn on the bus go “Beep, beep, beep”
All through the town.
As your bright and tiny spark,
Lights the traveller in the dark.
Though I know not what you are,
Twinkle, twinkle, little star.
Twinkle, twinkle, little star.
How I wonder what you are.
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star.
How I wonder what you are.
How I wonder what you are.
Old MacDonald had a farm
Ee i ee i o
And on his farm he had some cows
Ee i ee i oh
With a moo-moo here
And a moo-moo there
Here a moo, there a moo
Everywhere a moo-moo
Old MacDonald had a farm
Ee i ee i o
Old MacDonald had a farm
Ee i ee i o
And on his farm he had some chicks
Ee i ee i o
With a cluck-cluck here
And a cluck-cluck there
Here a cluck, there a cluck
Everywhere a cluck-cluck
Old MacDonald had a farm
Ee i ee i o
Old MacDonald had a farm
Ee i ee i o
And on his farm he had some pigs
Ee i ee i o
With an oink-oink here
And an oink-oink there
Here an oink, there an oink
Everywhere an oink-oink
Old MacDonald had a farm
Ee i ee i o
London Bridge is falling down,
Falling down, falling down,
London Bridge is falling down,
My fair lady
46 changes: 44 additions & 2 deletions src/LowercaseSentenceTokenizer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* @author Shawn Nguru
* SDEV 301 RambleBot
* 1-21-25
*
* Problems/Bugs/Issues:
*
*
* Notes/plans:
* Iterate through the String (1)
* split(String regex) a Split method by the spaces and symbols (2)
* Insert the strings into a list of strings (3)
*/

/**
* A tokenizer that converts text input to lowercase and splits it
* into a list of tokens, where each token is either a word or a period.
Expand Down Expand Up @@ -29,8 +44,35 @@ public class LowercaseSentenceTokenizer implements Tokenizer {
* @return a list of tokens, where each token is a word or a period
*/
public List<String> tokenize(Scanner scanner) {
// TODO: Implement this function to convert the scanner's input to a list of words and periods
return null;
List<String> textList =new ArrayList<String>();

String text = scanner.nextLine(); //put the input text in a string array

String[] arr = text.split(" "); //split the array based on spaces in the text

for(int i = 0; i<= arr.length-1; i++) //add the content of the array into the list of strings
{



if(arr[i] == " " || arr[i] == " " || arr[i] == "") //filtering out the extra spaces inside the arrayList

Choose a reason for hiding this comment

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

This works! scanner also has the functionality built in to skip whitespace with scanner.next

{

}
else if(arr[i].endsWith("."))
{
arr[i] = arr[i].replace('.', ' ');
textList.add(arr[i].trim().toLowerCase());
textList.add(".");
}
Comment on lines +62 to +67

Choose a reason for hiding this comment

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

Nice logic

else
{
textList.add(arr[i].toLowerCase());
}
}


return textList;
}
}

16 changes: 11 additions & 5 deletions src/LowercaseSentenceTokenizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class LowercaseSentenceTokenizerTest {

// Wave 1
@Test
void testTokenizeWithNoCapitalizationOrPeriod() {
void testTokenizeWithNoCapitalizationOrPeriod()
{
LowercaseSentenceTokenizer tokenizer = new LowercaseSentenceTokenizer();
Scanner scanner = new Scanner("this is a lowercase sentence without a period");
List<String> tokens = tokenizer.tokenize(scanner);
Expand All @@ -16,10 +17,15 @@ void testTokenizeWithNoCapitalizationOrPeriod() {
}

// Wave 2
/*
* Write your test here!
*/

@Test
void testTokenizeWithMultipleSpaces()
{
LowercaseSentenceTokenizer tokenizer = new LowercaseSentenceTokenizer();
Scanner scanner = new Scanner("hello hi hi hi hello hello");
List<String> tokens = tokenizer.tokenize(scanner);

assertEquals(List.of("hello","hi", "hi","hi", "hello","hello"), tokens);
}
Comment on lines +20 to +28

Choose a reason for hiding this comment

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

Nice test!


// Wave 3
@Test
Expand Down
87 changes: 83 additions & 4 deletions src/UnigramWordPredictor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/**
* @author Shawn Nguru
* SDEV 301 RambleBot
* 1-21-25
*
*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

/**
Expand Down Expand Up @@ -33,6 +41,11 @@ public UnigramWordPredictor(Tokenizer tokenizer) {
* If the input text is: "The cat sat. The cat slept. The dog barked."
* After tokenizing, the tokens would be: ["the", "cat", "sat", ".", "the", "cat", "slept", ".", "the", "dog", "barked", "."]
*
* key: "the" value: ["cat","cat","dog"]
* key: "cat" value: ["sat", "slept"]
*
*
*
* The resulting map (neighborMap) would be:
* {
* "the" -> ["cat", "cat", "dog"],
Expand All @@ -50,11 +63,49 @@ public UnigramWordPredictor(Tokenizer tokenizer) {
*/
public void train(Scanner scanner) {
List<String> trainingWords = tokenizer.tokenize(scanner);
List<String> valueWords = new ArrayList<>();
neighborMap = new HashMap<>();

// TODO: Convert the trainingWords into neighborMap here
String keyText = "";
for(int i = 0; i < trainingWords.size(); i++)
{
keyText = trainingWords.get(i);
for(int j= 0; j < trainingWords.size()-1; j++)
{
if(keyText.equals(trainingWords.get(j)))
{
valueWords.add(trainingWords.get(j+1));
}
}
neighborMap.put(trainingWords.get(i), valueWords);
valueWords = new ArrayList<String>();
Comment on lines +70 to +81

Choose a reason for hiding this comment

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

Nice logic! As an alternative, can you think of how this could be done without a nested loop?

}
}

/**
*
*
the quick fox the slow dog the slow cat
predictor.train()
|
v
this.neighborMap = {
"the": ["quick", "slow", "slow"],
"quick": ["fox"]
"slow": ["dog", "cat"]
}

---------------

predictor.predictNextWord(["I", "saw", "the"])
followingWords = neighborMap.get("the") -> ["quick", "slow", "slow"]

rng(3) -> # from [0-2]
randomNumber = rng(3)
followingWords.get(randomNumber) -> ?
*
* *
*
* Predicts the next word based on the given context.
* The prediction is made by randomly selecting from all words
* that follow the last word in the context in the training data.
Expand All @@ -73,6 +124,8 @@ public void train(Scanner scanner) {
* "barked" -> ["."]
* }
*
*
*
* When predicting the next word given a context, the predictor should use
* the neighbor map to select a word based on the observed frequencies in
* the training data. For example:
Expand All @@ -98,10 +151,36 @@ public void train(Scanner scanner) {
* @param context a list of words representing the current context
* @return the predicted next word, or null if no prediction can be made
*/
public String predictNextWord(List<String> context) {
public String predictNextWord(List<String> context)
{
// TODO: Return a predicted word given the words preceding it
// Hint: only the last word in context should be looked at
return null;
/**
*
*/

String probableWord = "";

String text = context.get(context.size()-1); //getting the last piece of text in the context

List<String> probaleList = new ArrayList<>(); //having the value of the list equal another arraylist

for (String key : neighborMap.keySet()) //looping through the map to get the most likely list
{
if(text == key)
{
probaleList = neighborMap.get(key);
}
}

Random wordProb = new Random(); //instantiating a random number generator
int max = probaleList.size()-1;

for (int i = 0; i < probaleList.size(); i++) //having the returned probable word return the most likely word from the random number
{
probableWord = probaleList.get(wordProb.nextInt(max - 0 + 1));
}
Comment on lines +178 to +181

Choose a reason for hiding this comment

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

We don't need the for loop here - getting a random word once is enough


return probableWord;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/UnigramWordPredictorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void testTrainAndGetNeighborMap() {

// Wave 5
/**

* Tests the predictNextWord method using a different example to verify that the correct word
* is predicted based on the training data.
*
Expand Down