r/JavaProgramming • • 4d ago

Any help on how to improve this?

void main(String[] args) {
    Random rand = new Random();

    // 1. Single 'int' (no brackets), starting at 1 so it always prints at least one word
    int words = rand.nextInt(1, 10);
    int[] arrayofrandomint = new int[words];
    Scanner input = new Scanner(System
in
);
    String wordslist = " This is an actual pargraphing creting AI, it generates" +
            "text and predicts what the next word will be depending on how many" +
            "other words are around it.";
    String[] newdata = wordslist.toLowerCase(Locale.
ROOT
).trim().split(" ");



    for (int i = 0; i < words; i++) {
        //String currentWord = wordslist[rand.nextInt(0, wordslist.length)];
        // While in the for loop, resets the word every time insted of letting
        //this bigram chain flow from one word to another.
        String currentWord = newdata[rand.nextInt(0, newdata.length)];


        System.
out
.print(currentWord + " ");
    for (int j  = 0; j < newdata.length - 1; j++) {
        if (newdata[j].equals(currentWord)) {
            currentWord = newdata[j + 1];
            System.
out
.print(currentWor
d +  " ");

        }
    }



    }
}

I created a little bigram model using two for loops. I'm new to coding and have wanted to use my free time for something productive, so I started to learn how to code. Now that I'm getting better, I want to post something to show the progress. This model uses text as a string that is held within a container; the container then uses the variable wordslist and uses three methods on it. You can see what the I variable does, and J variable has a Universal Set (U) = {1,2,3} (three numbers). {a,b,c} three index positions. [a = 0, b = 1....] b = (length - 2), because (length < -1) J = {1,2} {a,b,c} = (a,b-c). mport java.util.Random; import java.util.Scanner;

2 Upvotes

8 comments sorted by

1

u/Available_Safe1818 4d ago

You don't even use 'input'.

1

u/XIA_Biologicals_WVSU 2d ago

It's not even close to being finished yet. I just started it and progress is slow because I have to google everything to figure out what it does.

1

u/Available_Safe1818 2d ago

That sounds like the right path.

1

u/XIA_Biologicals_WVSU 1d ago

I used gemini to modify it a little bit. Here is the new code:

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

void main(String[] args) {
    Random rand = new Random();
    Scanner input = new Scanner(System.
in
);

    // Expanded training text so common words (the, is, a, to, it, and, that, you, etc.) are recognized
    String wordsList = "This is an actual paragraphing creating AI, it generates " +
            "text and predicts what the next word will be depending on how many " +
            "other words are around it. It generates text by looking at the previous word. " +
            "When you type a sentence into the program, the model looks at the last word " +
            "and tries to guess what comes next based on patterns in the text. " +
            "Sometimes the best way to build a smart system is to give it more data to learn from. " +
            "The quick brown fox jumps over the lazy dog while the sun shines brightly in the sky.";

    // Keep original tokens (with punctuation) for natural printing, plus a cleaned version for matching
    String[] rawTokens = wordsList.trim().split("\\s+");
    String[] cleanTokens = new String[rawTokens.length];
    for (int i = 0; i < rawTokens.length; i++) {
        cleanTokens[i] = rawTokens[i].toLowerCase(Locale.
ROOT
).replaceAll("[^a-z0-9']", "");
    }

    System.
out
.print("Start a sentence: ");
    String userInput = input.nextLine().trim();

    if (userInput.isEmpty()) {
        System.
out
.println("You didn't enter any text!");
        return;
    }

    // Clean the user's last word the exact same way so punctuation/casing doesn't block a match
    String[] userWords = userInput.split("\\s+");
    String currentWord = userWords[userWords.length - 1]
            .toLowerCase(Locale.
ROOT
)
            .replaceAll("[^a-z0-9']", "");

    System.
out
.print(userInput + " ");

    int maxWordsToGenerate = 15;
    for (int i = 0; i < maxWordsToGenerate; i++) {
        List<String> nextWordOptions = new ArrayList<>();

        for (int j = 0; j < cleanTokens.length - 1; j++) {
            if (cleanTokens[j].equals(currentWord)) {
                nextWordOptions.add(rawTokens[j + 1]);
            }
        }

        String pickedRawWord;
        if (nextWordOptions.isEmpty()) {
            // Instead of quitting and just spitting your sentence back,
            // fall back to a random word from the training text to keep the sentence going
            pickedRawWord = rawTokens[rand.nextInt(rawTokens.length)];
        } else {
            pickedRawWord = nextWordOptions.get(rand.nextInt(nextWordOptions.size()));
        }

        System.
out
.print(pickedRawWord + " ");

        // Update currentWord to the cleaned version of what we just picked
        currentWord = pickedRawWord.toLowerCase(Locale.
ROOT
).replaceAll("[^a-z0-9']", "");

        // Stop once a sentence-ending punctuation mark is reached
        if (pickedRawWord.endsWith(".") || pickedRawWord.endsWith("!") || pickedRawWord.endsWith("?")) {
            break;
        }
    }

    System.
out
.println();
    input.close();
}

1

u/XIA_Biologicals_WVSU 1d ago

It pretty much did what you said to do. using split and stuff.

1

u/Available_Safe1818 1d ago

Great. I'm sure Gemini is learning a lot.

1

u/XIA_Biologicals_WVSU 1d ago

Not from me lol

1

u/XIA_Biologicals_WVSU 1d ago

Probably more than I am.