Generate random word python

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’]

Project description

Build
PyPI version
PyPI - Python Version
PyPI - Status
Downloads
License: MIT

This is a simple python package to generate random English words.
If you need help after reading the below, please find me on Twitter at @vaibhavsingh97.

If you love the package, please :star2: the repo.

Installation

You should be able to install using easy_install or pip in the usual ways:

$ easy_install random-word
$ pip install random-word

Or clone this repository and run:

$ python3 setup.py install

Or place the random-word folder that you downloaded somewhere where your scripts can access it.

Basic Usage

👋 This package will now, by default, fetch the random word from local database

from random_word import RandomWords
r = RandomWords()

# Return a single random word
r.get_random_word()

Different services are available as a part of the random word package, which fetches random words from various API providers. Please check the Services section for more details.

Services

  • Wordnik
  • API Ninjas

Development

Assuming that you have Python and pipenv installed, set up your environment and install the required dependencies like this instead of the pip install random-word defined above:

$ git clone https://github.com/vaibhavsingh97/random-word.git
$ cd random-word
$ make init

Add API Key in random_word directory defining API Key in config.yml. If you don’t have an API key, then request your API key [here][wornikWebsiteLink]

API_KEY = "<API KEY>"

To check your desired changes, you can install your package locally.

$ pip install -e .

Issues

You can report the bugs at the issue tracker

License

Built with ♥ by Vaibhav Singh(@vaibhavsingh97) under MIT License

You can find a copy of the License at https://vaibhavsingh97.mit-license.org/

Download files

Download the file for your platform. If you’re not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

Random Word Generator

License: MIT
PyPI
Open Source? Yes!
Python3
made-with-python
install
code quality
Code Score
DOI
Downloads
Downloads

How to install this library?

pip3 install Random-Word-Generator

OR

pip install Random-Word-Generator

Which other python packages are needed to be installed?

  • No need of any external packages
  • Only Python version >= 3 is required

What this library does?

It helps us to generate random words i.e random noise in text data which is helpful in many text augmentation based tasks, NER, etc.

Which methods are available currently in this library?

Method Args Description
.generate() None This will return a randomly generated word
.getList(num_of_words) num_of_words This will return list of random words

Setting to look out before generating random words

Basic

from RandomWordGenerator import RandomWord

# Creating a random word object
rw = RandomWord(max_word_size=10,
                constant_word_size=True,
                include_digits=False,
                special_chars=r"@_!#$%^&*()<>?/|}{~:",
                include_special_chars=False)
Args Data Type Default Description
max_word_size int 10 Represents maximum length of randomly generated word
constant_word_size bool True Represents word length of
randomly generated word
include_digits bool False Represents whether or not to include digits in generated words
special_chars regex/string r»@_!#$%^&*()<>?/\
|}{~:»
Represents a regex string of all specials character you want to include in generated words
include_special_chars bool False Represents inclusion of special characters in generated words

How to get started with this library?

  1. Simple random word generation with constant word size

    from RandomWordGenerator import RandomWord
    
    rw = RandomWord(max_word_size=5)
    
    print(rw.generate())
    
    Output will be some random word like
    > hdsjq
    
  2. Simple random word generation with variable word size

    from RandomWordGenerator import RandomWord
    
    rw = RandomWord(max_word_size=5,
                    constant_word_size=False)
    
    print(rw.generate())
    
    Output will be some random word like
    > gw
    
  3. Random word generation with constant word size and including special character included

    from RandomWordGenerator import RandomWord
    
    rw = RandomWord(max_word_size=5,
                    constant_word_size=True,
                    special_chars=r"@#$%.*",
                    include_special_chars=True)
    
    print(rw.generate())
    
    Output will be some random word like
    > gsd$
    
  4. If we want randomly generated words in list we just have to input the argument with number of words we want

    from RandomWordGenerator import RandomWord
    
    rw = RandomWord(max_word_size=5,
                    constant_word_size=False)
    
    print(rw.getList(num_of_random_words=3))
    
    Output will be some random word like
    > ['adjse', 'qytqw', ' klsdf', 'ywete', 'klljs']
    
    

Application

  • In cases where we need to add random noise in text
  • Text Data Augmentation based tasks
  • Can be used to generate random tokens for some particular application like authorization code
  • In Automatic Password Suggestion system

Author

I will be happy to connect with you guys!!

Linkedin

Twitter

Citation

@software{abhishek_c_salian_2020_4384164,
  author       = {Abhishek C. Salian},
  title        = {AbhishekSalian/Random-Word-Generator v1.0.0},
  month        = dec,
  year         = 2020,
  publisher    = {Zenodo},
  version      = {v1.0.0},
  doi          = {10.5281/zenodo.4384164},
  url          = {https://doi.org/10.5281/zenodo.4384164}
}

Any suggestions are most welcome.


This post goes over how to generate a random word or letter in Python.

Install

Install random-word and PyYaml:

pip3 install random-word pyyaml

PyYaml is required or else you’ll get the error:

ModuleNotFoundError: No module named 'yaml'

Usage

Generate a random word:

from random_word import RandomWords

random_words = RandomWords()
print(random_words.get_random_word())

See the package documentation for more information.

Demo

Replit:

Random Letter

Get a random letter from the alphabet:

from string import ascii_lowercase
from random import randrange

print(ascii_lowercase[randrange(len(ascii_lowercase))])

Demo

Replit:



Please support this site and join our Discord!


Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    File handling in Python is really simple and easy to implement. In order to pull a random word or string from a text file, we will first open the file in read mode and then use the methods in Python’s random module to pick a random word. 

    There are various ways to perform this operation:

    This is the text file we will read from:

    Method 1: Using random.choice()

    Steps:

    1. Using with function, open the file in read mode. The with function takes care of closing the file automatically.
    2. Read all the text from the file and store in a string
    3. Split the string into words separated by space.
    4. Use random.choice() to pick a word or string.

    Python

    import random

    with open("MyFile.txt", "r") as file:

        allText = file.read()

        words = list(map(str, allText.split()))

        print(random.choice(words))

    Note: The split() function, by default, splits by white space. If you want any other delimiter like newline character you can specify that as an argument.

    Output:

    Output for two sample runs

    The above can be achieved with just a single line of code like this : 

    Python

    import random

    print(random.choice(open("myFile.txt","r").readline().split()))

    Method 2: Using random.randint() 

    Steps:

    1. Open the file in read mode using with function
    2. Store all data from the file in a string and split the string into words.
    3. Count the total number of words.
    4. Use random.randint() to generate a random number between 0 and the word_count.
    5. Print the word at that position.

    Python

    import random

    with open("myFile.txt", "r") as file:

        data = file.read()

        words = data.split()

        word_pos = random.randint(0, len(words)-1)

        print("Position:", word_pos)

        print("Word at position:", words[word_pos])

    Output:

    Output for two sample runs

    Like Article

    Save Article

    Понравилась статья? Поделить с друзьями:
  • Generate random return excel
  • Generate random number excel
  • Generate qr codes from excel
  • Generate qr code in excel
  • Generate name from word