46

So i'm basically working on a project where the computer takes a word from a list of words and jumbles it up for the user. there's only one problem: I don't want to keep having to write tons of words in the list, so i'm wondering if there's a way to import a ton of random words so even I don't know what it is, and then I could enjoy the game too? This is the coding of the whole program, it only has 6 words that i put in:

import random

WORDS = ("python", "jumble", "easy", "difficult", "answer",  "xylophone")
word = random.choice(WORDS)
correct = word
jumble = ""
while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
print(
"""
      Welcome to WORD JUMBLE!!!

      Unscramble the leters to make a word.
      (press the enter key at prompt to quit)
      """
      )
print("The jumble is:", jumble)
guess = input("Your guess: ")
while guess != correct and guess != "":
    print("Sorry, that's not it")
    guess = input("Your guess: ")
if guess == correct:
    print("That's it, you guessed it!\n")
print("Thanks for playing")

input("\n\nPress the enter key to exit")
dreftymac
  • 29,742
  • 25
  • 114
  • 177
Infamouslyuseless
  • 842
  • 2
  • 8
  • 15
  • 2
    Use a text file of words? `/usr/share/dict/words` is common *nix platforms, or there's other wordlists you can use... – Jon Clements Sep 16 '13 at 18:19
  • possible duplicate of [Free word list for use programatically?](http://stackoverflow.com/questions/772922/free-word-list-for-use-programatically) – Bakuriu Sep 16 '13 at 18:45
  • **See also:** novelty python demo that does not require loading any external word list [https://github.com/dreftymac/trypublic/blob/master/lab2021/python-wordlist/random_word_demo_py37.py](https://github.com/dreftymac/trypublic/blob/master/lab2021/python-wordlist/random_word_demo_py37.py) dmid://uu669smist1628779 – dreftymac Aug 12 '21 at 15:19

6 Answers6

97

Reading a local word list

If you're doing this repeatedly, I would download it locally and pull from the local file. *nix users can use /usr/share/dict/words.

Example:

word_file = "/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()

Pulling from a remote dictionary

If you want to pull from a remote dictionary, here are a couple of ways. The requests library makes this really easy (you'll have to pip install requests):

import requests

word_site = "https://www.mit.edu/~ecprice/wordlist.10000"

response = requests.get(word_site)
WORDS = response.content.splitlines()

Alternatively, you can use the built in urllib2.

import urllib2

word_site = "https://www.mit.edu/~ecprice/wordlist.10000"

response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
Kyle Kelley
  • 13,315
  • 4
  • 47
  • 77
  • 3
    Thanks a lot man, this really helped, i put your answer as the accepted answer and i voted up :) – Infamouslyuseless Sep 16 '13 at 19:27
  • Note that the link `http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content-type=text%2Fplain` is now dead... – Eric Mar 19 '14 at 13:56
  • @Eric - :( Guess I'll just have to host a word list. – Kyle Kelley Mar 19 '14 at 21:25
  • @Eric, this works great; word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain". It's hosted with ViewVc formatting, and they mentioned the download link is the best way to go; http://permalink.gmane.org/gmane.comp.version-control.cvs.viewcvs.user/5453 . Can you believe this 25k dictionary is 21 years old!!?? I love the web. //oops truncates. will edit post. – AnneTheAgile Oct 27 '14 at 19:42
  • the used link seems deprecated, use https://www.mit.edu/~ecprice/wordlist.10000 – navaneeth kt Oct 27 '20 at 13:38
  • users beware... that MIT wordlist contains some VERY inappropriate words. – Rexovas Feb 02 '22 at 03:37
17

Solution for Python 3

For Python3 the following code grabs the word list from the web and returns a list. Answer based on accepted answer above by Kyle Kelley.

import urllib.request

word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = urllib.request.urlopen(word_url)
long_txt = response.read().decode()
words = long_txt.splitlines()

Output:

>>> words
['a', 'AAA', 'AAAS', 'aardvark', 'Aarhus', 'Aaron', 'ABA', 'Ababa',
 'aback', 'abacus', 'abalone', 'abandon', 'abase', 'abash', 'abate',
 'abbas', 'abbe', 'abbey', 'abbot', 'Abbott', 'abbreviate', ... ]

And to generate (because it was my objective) a list of 1) upper case only words, 2) only "name like" words, and 3) a sort-of-realistic-but-fun sounding random name:

import random
upper_words = [word for word in words if word[0].isupper()]
name_words  = [word for word in upper_words if not word.isupper()]
rand_name   = ' '.join([name_words[random.randint(0, len(name_words))] for i in range(2)])

And some random names:

>>> for n in range(10):
        ' '.join([name_words[random.randint(0,len(name_words))] for i in range(2)])

    'Semiramis Sicilian'
    'Julius Genevieve'
    'Rwanda Cohn'
    'Quito Sutherland'
    'Eocene Wheller'
    'Olav Jove'
    'Weldon Pappas'
    'Vienna Leyden'
    'Io Dave'
    'Schwartz Stromberg'
amoodie
  • 313
  • 2
  • 9
  • 1
    I got `404` using this code till I set `headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}` then modified the code `req = urllib.request.Request(word_url, headers=headers)` and `response = urllib.request.urlopen(req)`. – abulka Feb 22 '21 at 00:05
  • It's way better to just download that words file, about 200Kb than re-downloading and rely on the connection. – m3nda Oct 15 '21 at 02:36
4

There is a package random_word could implement this request very conveniently:

 $ pip install random-word

from random_word import RandomWords
r = RandomWords()

# Return a single random word
r.get_random_word()
# Return list of Random words
r.get_random_words()
# Return Word of the day
r.word_of_the_day()
Freman Zhang
  • 403
  • 4
  • 6
3

There are a number of dictionary files available online - if you're on linux, a lot of (all?) distros come with an /etc/dictionaries-common/words file, which you can easily parse (words = open('/etc/dictionaries-common/words').readlines(), eg) for use.

a p
  • 2,922
  • 2
  • 24
  • 45
  • can you edit my code, to see how it would look like if i wanted all the words from this website? http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content-type=text%2Fplain – Infamouslyuseless Sep 16 '13 at 18:27
1

get the words online

from urllib.request import Request, urlopen
url="https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})

web_byte = urlopen(req).read()

webpage = web_byte.decode('utf-8')
print(webpage)

Randomizing the first 500 words

from urllib.request import Request, urlopen
import random


url="https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})

web_byte = urlopen(req).read()

webpage = web_byte.decode('utf-8')
first500 = webpage[:500].split("\n")
random.shuffle(first500)
print(first500)

Output

['abnegation', 'able', 'aborning', 'Abigail', 'Abidjan', 'ablaze', 'abolish', 'abbe', 'above', 'abort', 'aberrant', 'aboriginal', 'aborigine', 'Aberdeen', 'Abbott', 'Abernathy', 'aback', 'abate', 'abominate', 'AAA', 'abc', 'abed', 'abhorred', 'abolition', 'ablate', 'abbey', 'abbot', 'Abelson', 'ABA', 'Abner', 'abduct', 'aboard', 'Abo', 'abalone', 'a', 'abhorrent', 'Abelian', 'aardvark', 'Aarhus', 'Abe', 'abjure', 'abeyance', 'Abel', 'abetting', 'abash', 'AAAS', 'abdicate', 'abbreviate', 'abnormal', 'abject', 'abacus', 'abide', 'abominable', 'abode', 'abandon', 'abase', 'Ababa', 'abdominal', 'abet', 'abbas', 'aberrate', 'abdomen', 'abetted', 'abound', 'Aaron', 'abhor', 'ablution', 'abeyant', 'about']

PythonProgrammi
  • 20,343
  • 3
  • 36
  • 34
0
import random
import string

letters = string.ascii_letters
x = "".join(random.sample(letters,5))
print(x)

Above, letters will contain all ASCII letter characters (lowercase & uppercase) like so:

>>> print(letters)
>>> 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Using random.sample will provide you with a list of the total number of items (characters in this case) from the given object (a string object here) based on the number you want (random.sample(<string>, <# of random parts>).

>>> random.sample(letters, 5)
>>> ['j', 'e', 'u', 'g', 'k']

Finally,

x = "".join(...) will join together the returned list with no spaces because of "". which can be changed to " ". if you want spaces in between each letter.

PythonReactor
  • 453
  • 3
  • 17
  • 2
    On Stack Overflow, the **how** is important, but a great part of the quality level of the site comes from the fact that people go to great lengths to explain the **why**. While a _code-only_ answer get the person who asked the question past whatever hurdle they might be facing, it doesn't do them or future visitors much good in the long run. See [Is there any benefit in code-only answers?](https://meta.stackexchange.com/a/148274/183937) – Steve Mar 25 '22 at 08:02
  • 1
    Does this really do what OP wants? They seem to be looking for a list of real words, not completely random strings like `xqqpl` or `ajhye`. Please read [answer]. Note also that this question is **8.5 years old** and already has 5 answers. With questions like this it is even _more_ important to explain your solution. Help us understand why this is better than what's already posted. – Chris Mar 25 '22 at 17:19