Random word generator- Python

前端 未结 5 1729
长情又很酷
长情又很酷 2020-12-23 20:58

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

相关标签:
5条回答
  • 2020-12-23 21:01

    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.

    0 讨论(0)
  • 2020-12-23 21:07

    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()
    
    0 讨论(0)
  • 2020-12-23 21:08

    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'
    
    0 讨论(0)
  • 2020-12-23 21:12

    get the words online

    >>> words = requests.get("http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain").content.splitlines()
    >>> x = 0
    >>> for w in words:
    ...  print(str(x) + str(w).replace("'b",""))
    ...  x += 1
    

    Output

    25477b'zooplankton'
    25478b'Zorn'
    25479b'Zoroaster'
    25480b'Zoroastrian'
    25481b'zounds'
    25482b"z's"
    25483b'zucchini'
    25484b'Zulu'
    25485b'Zurich'
    25486b'zygote'
    

    Store the names in local pc

    with open("dictionary.txt",'w') as file:
        for w in words:
            file.write(str(x) + str(w).replace("'b",""))
    
    0 讨论(0)
  • 2020-12-23 21:16

    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()
    
    0 讨论(0)
提交回复
热议问题