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()
Answer from Kyle Kelley on Stack OverflowReading 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()
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'
import string
def make_new_words(start_word):
"""create new words from given start word and returns new words"""
new_words = []
for i, letter in enumerate(start_word):
word_as_list = list(start_word)
for char in string.ascii_lowercase:
word_as_list[i] = char
new_words.append("".join(word_as_list))
return new_words
lowercase is just a string containing the lowercase letters...
We want to change each letter of the original word (here w) so we
iterate on the letters of w, but we'll mostly need the index of the letter, so we do our for loop on enumerate(w).
First of all, in python strings are immutable so we build a list x from w... lists are mutable
Now a second, inner loop on the lowercase letters: we change the current element of the x list accordingly (having changed x, we need to reset it before the next inner loop) and finally we print it.
Because we want to print a string rather than the characters in a list, we use the join method of the null string '' that glue together the elements of x using, of course, the null string.
I have not reported the output but it's exactly what you've asked for, just try...
from string import lowercase
w = 'car'
for i, _ in enumerate(w):
x = list(w)
for s in lowercase:
x[i] = s
print ''.join(x)
xml - How can I create a Word document using Python? - Stack Overflow
Generate random word of specific length
Simple word generator written in Python
maybe i'm the stupid one here, but how do i run the thing?
More on reddit.compython - Generating random words - Stack Overflow
Videos
I work in a legal department that’s been having a lot of issues with how our contracts are being generation out of SFDC by our sales team. Working with the SFDC (Salesforce) developer team is a pain and takes months for any solutions to be implemented.
Is it possible for me to use a Python script that takes an active word document, strips information found in certain locations of the doc, and then generates a new (and correct) word document from a presaved template. In essence, the wrong contract template is being generated from sales and I want a Python script to automate the manual adjustments we’ve been having to make. Thanks so much in advance!
A couple ways you can create Word documents using Python:
- Use COM automation to create a document using the MS Word object model (using
pywin32). http://python.net/crew/pirx/spam7/ - Automate OpenOffice using Python: http://wiki.services.openoffice.org/wiki/Python
- If rtf format is OK, use the PyRTF library: http://pyrtf.sourceforge.net/
EDIT:
Since COM is out of the question, I suggest the following (inspired by @kcrumley's answer):
Using the UNO library to automate Open Office from python, open the HTML file in OOWriter, then save as .doc.
EDIT2:
There is now a pure Python python-docx project that looks nice (I have not used it).
I tried python-docx with succes, it enables you to make and edit docx within Python
I want to generate a random word of a specific length. I found a python module called random-word and this is my code.
from random_word import RandomWords
r = RandomWords()
# Return a single random word
r.get_random_word()
result = None
while result is None:
try:
word1 = r.get_random_word()
print (word1)
if len(word1) == 5:
result = True
except:
print ('There was a problem')
# other code that uses result but is not involved in getting it
print (word1)
print (len(word1))It works perfectly fine, but it's very slow. It takes about 10 to 15 seconds to run until it finds a word of that length. Does anybody have a better way of doing this? Another way I thought was to maybe have a dictionary file and iterate through that. The reason I don't like that approach is I don't really like the idea of having to have a file on the machine. Also if it iterates through the file it won't be random.
Any suggestions appreciated.
Thanks
https://github.com/Lebed-kun/simple-conlang-generator/blob/patch-1/conlang_generator.py
This word generator can create any amount of words with user-defined phonemic patterns considering user-defined forbidden phonemic sequences
Further changes:
-
Add user-defined changes of resulted words using phonemic notation (X > Y / Z , where X is a source phoneme(s), Y - a resulting phoneme(s), Z - is a condition for phonemic shift (optional)) -
Use randomly generated or user-defined frequencies of phonemes for word generating (because phonemes aren't equiprobable in any spoken language)
-
Add randomly generated sound changes and phonotactics
-
Add user-defined and randomly generated (derivational) morphology
-
Add semantics
UPD: https://github.com/Lebed-kun/simple-conlang-generator/blob/master/conlang_generator.py
Now you can set sound changes (in phonological rule notation) and apply them to words. Also phonemes, forbidden clusters and sound changes are saved in a single file
random.choice(words) * 5 executes random.choice only once and then multiplies the result by five, causing the same string to be repeated.
>>> import random
>>> words = ['hello', 'apple', 'something', 'yeah', 'nope', 'lalala']
>>> print ''.join(random.choice(words) for _ in range(5))
applesomethinghellohellolalala
If you don't want the words from your original list to be repeated, then you could use sample.
import random as rn
words = ['hello', 'apple', 'something', 'yeah', 'nope', 'lalala']
word = ''.join(rn.sample(words, 5))
Result:
>>> word
'yeahhellosomethingapplenope'