Sha256: 5f0cad20fc2721838051c796b1925f5f85dd484f8de63bd24ac11e098c064578

Contents?: true

Size: 804 Bytes

Versions: 9

Compression:

Stored size: 804 Bytes

Contents

package wordcram;

public class WordBag implements WordSource {

  int numWords;
  String[] wordStrings;
  double weightDistributionPower = 2;

  public WordBag(int numWords, String... wordStrings) {
    this.numWords = numWords;
    this.wordStrings = wordStrings;
  }

  public WordBag weightDistributionPower(float wdp) {
    this.weightDistributionPower = wdp;
    return this;
  }

  @Override
  public Word[] getWords() {
    Word[] words = new Word[numWords];
    java.util.Random rand = new java.util.Random();
    
    for (int i = 0, wi = 0; i < words.length; i++, wi = (wi + 1) % wordStrings.length) {
      String word = wordStrings[wi];
      double weight = Math.pow(rand.nextDouble(), weightDistributionPower);
      words[i] = new Word(word, (float)weight);
    }
    return words;
  }
}

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby_wordcram-2.1.1 src/wordcram/WordBag.java
ruby_wordcram-2.1.0 src/wordcram/WordBag.java
ruby_wordcram-2.0.6 src/wordcram/WordBag.java
ruby_wordcram-2.0.5 src/wordcram/WordBag.java
ruby_wordcram-2.0.4 src/wordcram/WordBag.java
ruby_wordcram-2.0.3 src/wordcram/WordBag.java
ruby_wordcram-2.0.2 src/wordcram/WordBag.java
ruby_wordcram-2.0.1 src/wordcram/WordBag.java
ruby_wordcram-2.0.0 src/wordcram/WordBag.java