- Saved searches
- Use saved searches to filter your results more quickly
- License
- AbhishekSalian/Random-Word-Generator
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Generator random words python
- # Table of Contents
- # Generate random words in Python
- # Splitting by newlines or splitting by spaces
- # Picking a random word from the list
- # Picking N random words from the list
- # Generate random words from a remote database (HTTP request)
- # Picking a random word from the list
- # Picking N random words from the list
- # Additional Resources
- Random Word Generator
- Which other python packages are needed to be installed?
- What this library does?
- Which methods are available currently in this library?
- Setting to look out before generating random words
- Basic
- Application
- Author
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
This library helps you to create random words i.e noise in text data. Helpful in many tasks like the generation of random authorization token generation of constant or variable length, text data augmentation, etc.
License
AbhishekSalian/Random-Word-Generator
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
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?
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
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"@_!#$%^&*()<>?/\|>
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?
from RandomWordGenerator import RandomWord rw = RandomWord(max_word_size=5) print(rw.generate())
Output will be some random word like > hdsjq
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
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$
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
I will be happy to connect with you guys!!
@software, title = , month = dec, year = 2020, publisher = , version = , doi = , url = >
Any suggestions are most welcome.
About
This library helps you to create random words i.e noise in text data. Helpful in many tasks like the generation of random authorization token generation of constant or variable length, text data augmentation, etc.
Generator random words python
Last updated: Feb 22, 2023
Reading time · 4 min
# Table of Contents
# Generate random words in Python
To generate a random word from the file system:
- Open a file in reading mode.
- Use the str.splitlines() or str.split() method to split the contents of the file into a list.
- Use the random.choice() method to pick as many random words from the list as necessary.
Copied!import random def get_list_of_words(path): with open(path, 'r', encoding='utf-8') as f: return f.read().splitlines() words = get_list_of_words('/usr/share/dict/words') print(words) random_word = random.choice(words) print(random_word) # 👉️ sales
If you'd rather generate random words from a remote database (HTTP request), click on the following subheading:
The code sample generates random words from a file on the local file system.
If you are on Linux or macOS, you can use the /usr/share/dict/words/ file.
If you are on Windows, you can use this MIT word list.
Open the link, right-click on the page and click "Save as", then save the .txt file right next to your Python script.
We used the with statement to open the file in reading mode.
The statement automatically takes care of closing the file for us.
Copied!import random import requests def get_list_of_words(path): with open(path, 'r', encoding='utf-8') as f: return f.read().splitlines() words = get_list_of_words('/usr/share/dict/words') print(words) random_word = random.choice(words) print(random_word) # 👉️ sales
Make sure to update the path if you aren't on macOS or Linux.
We used the file.read() method to read the contents of the file into a string.
# Splitting by newlines or splitting by spaces
The str.splitlines method splits the string on newline characters and returns a list containing the lines in the string.
Copied!multiline_str = """\ bobby hadz com""" lines = multiline_str.splitlines() print(lines) # 👉️ ['bobby', 'hadz', 'com']
If the words in your file are separated by spaces, use the str.split() method instead.
Copied!string = "bobby hadz . com" lines = string.split() print(lines) # 👉️ ['bobby', 'hadz', '.', 'com']
The str.split() method splits the string into a list of substrings using a delimiter.
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.
# Picking a random word from the list
If you need to pick a random word from the list, use the random.choice() method.
Copied!import random def get_list_of_words(path): with open(path, 'r', encoding='utf-8') as f: return f.read().splitlines() words = get_list_of_words('/usr/share/dict/words') print(words) random_word = random.choice(words) print(random_word) # 👉️ unbreakable
The random.choice method takes a sequence and returns a random element from the non-empty sequence.
# Picking N random words from the list
If you need to pick N random words from the list, use a list comprehension.
Copied!import random def get_list_of_words(path): with open(path, 'r', encoding='utf-8') as f: return f.read().splitlines() words = get_list_of_words('/usr/share/dict/words') print(words) n_random_words = [ random.choice(words) for _ in range(3) ] # 👇️ ['computers', 'praiseworthiness', 'shareholders'] print(n_random_words)
We used a list comprehension to iterate over a range object.
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
The range class is commonly used for looping a specific number of times.
On each iteration, we call the random.choice() method to pick a random word and return the result.
# Generate random words from a remote database (HTTP request)
To generate random words from a remote database:
- Make an HTTP request to a database that stores a word list.
- Use the random.choice() method to pick a random word from the list.
- Optionally, use a list comprehension to pick N random words from the list.
Copied!import random import requests def get_list_of_words(): response = requests.get( 'https://www.mit.edu/~ecprice/wordlist.10000', timeout=10 ) string_of_words = response.content.decode('utf-8') list_of_words = string_of_words.splitlines() return list_of_words words = get_list_of_words() print(words) random_word = random.choice(words) print(random_word) # 👉️ zoo
If you don't have the requests module installed, install it by running the following command.
Copied!# 👇️ in a virtual environment or using Python 2 pip install requests # 👇️ for python 3 (could also be pip3.10 depending on your version) pip3 install requests
You can open the MIT word list in your browser to view the contents.
The list contains 10,000 words with each word on a separate line.
We used the bytes.decode() method to convert the bytes object to a string.
The bytes.decode method returns a string decoded from the given bytes. The default encoding is utf-8 .
The words are on separate lines, so we used the str.splitlines() method to split the string into a list of words.
If your database responds with a long string containing space-separated words, use the str.split() method instead.
Copied!string = "bobby hadz . com" lines = string.split() print(lines) # 👉️ ['bobby', 'hadz', '.', 'com']
# Picking a random word from the list
If you need to pick a random word from the list, use the random.choice() method.
Copied!import random import requests def get_list_of_words(): response = requests.get( 'https://www.mit.edu/~ecprice/wordlist.10000', timeout=10 ) string_of_words = response.content.decode('utf-8') list_of_words = string_of_words.splitlines() return list_of_words words = get_list_of_words() print(words) random_word = random.choice(words) print(random_word) # 👉️ global
# Picking N random words from the list
If you need to pick N random words from the list, use a list comprehension.
Copied!import random import requests def get_list_of_words(): response = requests.get( 'https://www.mit.edu/~ecprice/wordlist.10000', timeout=10 ) string_of_words = response.content.decode('utf-8') list_of_words = string_of_words.splitlines() return list_of_words words = get_list_of_words() print(words) n_random_words = [ random.choice(words) for _ in range(3) ] # 👇️ ['clerk', 'trust', 'tr'] print(n_random_words)
The list comprehension iterates over a range object of length N and calls the random.choice() method to pick a random word on each iteration.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Random Word Generator
Which other python packages are needed to be installed?
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, constant_word_size=True, include_digits=False, special_chars=r"@_!#$%^&*()<>?/\|>
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?
from RandomWordGenerator import RandomWord rw = RandomWord(max_word_size=5) print(rw.generate())
Output will be some random word like > hdsjq
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
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$
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
- Name Entity Relation extraction based tasks
- Text Data Augmentation based tasks
Author
I will be happy to connect with you guys!!
Any suggestions are most welcome.