Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Get random line from a big file
    primarykey
    data
    text
    <p>I've seen <a href="https://stackoverflow.com/questions/2218005/how-to-get-a-random-line-of-a-text-file-in-java">how to get a random line from a text file</a>, but the method stated there (the accepted answer) is running horrendously slow. It runs very slowly on my 598KB text file, and still slow on my a version of that text file which has only one out of every 20 lines, at 20KB. I never get past the "a" section (it's a wordlist).</p> <p>The original file has 64141 lines; the shortened one has 2138 lines. To generate these files, I took the Linux Mint 11 <code>/usr/share/dict/american-english</code> wordlist and used <code>grep</code> to remove anything with uppercase or an apostrophe (<code>grep -v [[:upper:]] | grep -v \'</code>). </p> <p>The code I'm using is</p> <pre><code>String result = null; final Random rand = new Random(); int n = 0; for (final Scanner sc = new Scanner(wordList); sc.hasNext();) { n++; if (rand.nextInt(n) == 0) { final String line = sc.nextLine(); boolean isOK = true; for (final char c : line.toCharArray()) { if (!(constraints.isAllowed(c))) { isOK = false; break; } } if (isOK) { result = line; } System.out.println(result); } } return result; </code></pre> <p>which is slightly adapted from <a href="https://stackoverflow.com/questions/2218005/how-to-get-a-random-line-of-a-text-file-in-java/2218067#2218067">Itay's answer</a>.</p> <p>The object <code>constraints</code> is a <code>KeyboardConstraints</code>, which basically has the one method <code>isAllowed(char)</code>:</p> <pre><code>public boolean isAllowed(final char key) { if (allAllowed) { return true; } else { return allowedKeys.contains(key); } } </code></pre> <p>where <code>allowedKeys</code> and <code>allAllowed</code> are provided in the constructor. The <code>constraints</code> variable used here has <code>"aeouhtns".toCharArray()</code> as its <code>allowedKeys</code> with <code>allAllowed</code> off.</p> <p>Essentially, what I want the method to do is to pick a random word <em>that satisfies the constraints</em> (e.g. for these constraints, "outvote" would work, but not "worker", because "w" is not in <code>"aeouhtns".toCharArray()</code>).</p> <p>How can I do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload