What's the significance of random name generator Java?

334    Asked by Amitraj in Java , Asked on Oct 11, 2022

 wrote a working random name generator in Java. Here's my code:


NameGenerator.java:


package pl.hubot.dev.name Generator;


import java.util.*;
import java.util.stream.Collectors;
public class NameGenerator {
    private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
    private static Random random = new Random();
    void generate() {
        WordChecker wordChecker = new WordChecker();
        StringSimilarity stringSimilarity = new StringSimilarity();
        Dictionary dictionary = new Dictionary();
        final String filepath = "/usr/share/dict/american-english";
        dictionary.build(filepath);
        List syllabes = combination(CHARACTERS, 3).parallelStream()
                .filter((x) -> wordChecker.countSyllables(x) > 1).collect(Collectors.toList());
        String word = "";
        while (stringSimilarity.similarity(word, dictionary.getRandomEntries(1)[0]) < 0>            word = syllables.get(random.nextInt(syllabes.size())) + syllabes.get(random.nextInt(syllabes.size()));
        }
        System.out.println(word);
    }
    private Set combination(String input, int n) {
        List letters = new ArrayList();
        for (int i = 0; i < input.length(); ++i)
            letters.add(input.charAt(i));
        Set results = new HashSet();
        combination("", letters, n, results);
        return results;
    }
    private void combination(String soFar, List letters, int n,
                            Set results) {
        if (n == 0) {
            results.add(soFar);
            return;
        }
        int startIndex = soFar.length();
        if (startIndex >= letters.size())
            return;
        for (int i = startIndex; i < letters>            // ch is the next candidate to add to the result that we're
            // building (soFar)
            char ch = letters.get(i);
            // Swap the characters at positions i and position startIndex.
            char temp = letters.get(startIndex);
            letters.set(i, temp);
            letters.set(startIndex, ch);
            // add ch to soFar, compute combinations of length n-1.
            // as startIndex is essentially soFar.length() this means that
            // the recursive call will only process the remainder of the list.
            combination(soFar + ch, letters, n - 1, results);
            // Swap the characters back - restore the original state.
            letters.set(i, ch);
            letters.set(startIndex, temp);
        }
    }
}


Could I ask about reviewing this code?

Consider Regex for random name generator java

Your WordChecker class has numerous helper methods that all bubble up to just pattern matching. This is exactly why Regex exists, Java has Pattern and Matcher classes in java.util.regex that can help you do this fairly succinctly. For example, to check for only vowels or only consonants you can have this:

public class WordChecker {
    private static final Pattern VOWEL_PATTERN = Pattern.compile("^[aeiouy]+$");
    private static final Pattern CONSONANT_PATTERN = Pattern.compile("^[bcdfghjklmnpqrstvwxz]+$");
    private static boolean onlyVowels(String input) {
        return VOWEL_PATTERN.matcher(input).matches();
    }
    private static boolean onlyConsonants(String input) {
        return CONSONANT_PATTERN.matcher(input).matches();
    }
}

Do note, however, that I make these methods static, so call them with WordChecker.onlyVowels("..."); As it stands all the content consists of helper functions independent of any particular implementation so being static fits well.

For further reference look at the documentation here.

Use meaningful names; avoid deceptive names.

Naming things can be surprisingly difficult especially when striving to keep it short and indicate full meaning, yet only returning 0 or 1 seems completely unexpected from a method that is meant to return the similarity between two things (similarity implies a multitude of outputs on a range, the way you apply it here essentially turn it into a boolean).

Leverage the API

One of the premier merits of using any language in particular is the available libraries. Unless this was done as an exercise the majority of the Dictionary class can be eliminated by using a HashMap.



Your Answer

Interviews

Parent Categories