Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to get random words from the dictionary databases like from the dict-wn or freedict's packages? I'm looking for a way to do that by either using a bash script or try a command tool if available.

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

If the wordlist is a plain text file with one word per line, then one option is the shuf command e.g.

$ shuf -n5 /usr/share/dict/american-english
resuscitated
Lawson
concatenate
nonsmoker's
balmiest

See man shuf

SHUF(1)                          User Commands                         SHUF(1)

NAME
       shuf - generate random permutations

SYNOPSIS
       shuf [OPTION]... [FILE]
       shuf -e [OPTION]... [ARG]...
       shuf -i LO-HI [OPTION]...

DESCRIPTION
       Write a random permutation of the input lines to standard output.
share|improve this answer
add comment

I'd favour shuf for plucking lines from a file. We also need to be careful with the words because it's a tab-delimited file, not just whitespace.

$ shuf -n5 /usr/share/dictd/wn.index | cut -d$'\t' -f1
table game
parsi
tetraneuris grandiflora
synonymously
decimal digit

shuf takes 0.011s here. sort -R (even without the awk) takes 2.427s. This is because shuf is just seeking through the file n times while sort is reading and processing every line... In this case that's 147311 lines.

share|improve this answer
add comment

There are plenty of ways to get a random word. Example to retrieve five words:

$ sort -R /usr/share/dictd/freedict-nld-eng.index | awk 'NR <= 5 { print $1 }'
verslappen
sport
libretto
golf
kerk

See man sort for the meaning of the -R option (hint: Random). The awk expression filters on the first five records (lines) and prints the first field (not the other columns).

share|improve this answer
1  
@rusty No problem, sort is not the most efficient program for this purpose either, it creates temporary files for large files. A better program (in terms of memory) would first scan for the number of lines, then pick some random numbers and then scan again for those lines. –  Lekensteyn Jul 7 at 17:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.