在Python中轻松生成随机单词和句子
GitHub存储库| PYPI |文档
WonderWords是一个用于生成随机单词和结构化随机句子的Python软件包。它还带有一个彩色命令行界面,用于快速生成随机单词。最新版本可在GitHub上找到,而稳定版本则可以在PYPI上获得。
这是Wonderwords的能力:
要安装Wonderwords的最新版本,请使用您喜欢的软件包管理器进行Python软件包索引来安装wonderwords软件包。例如,使用PIP:
pip install wonderwords用pip升级神经词:
pip install --upgrade wonderwords为了验证安装工作是否有效,请在Python中导入Wonderwords:
import wonderwords如果您遇到ModuleNotFound错误,请确保已从上面的步骤中安装了Wonderwords。有关更多问题,请从GitHub页面上打开新问题。
本节将简要描述Wonderwords的用法。由于WonderWords具有命令行接口和Python模块,因此您将找到两个小节。
基本随机单词生成类是RandomWord类。您可以用word方法生成单词:
from wonderwords import RandomWord
r = RandomWord ()
# generate a random word
r . word ()
# random word that starts with a and ends with en
r . word ( starts_with = "a" , ends_with = "en" )
# generate a random noun or adjective, by default all parts of speech are included
r . word ( include_parts_of_speech = [ "nouns" , "adjectives" ])
# generate a random word between the length of 3 and 8 characters
r . word ( word_min_length = 3 , word_max_length = 8 )
# generate a random word with a custom Python regular expression
r . word ( regex = ".*a" )
# some of the words in the default word lists have spaces, such as 'contact lens'
# this option disables them
r . word ( exclude_with_spaces = True )
# you can combine multiple filtering options
r . word ( starts_with = "ru" , word_max_length = 10 , include_parts_of_speech = [ "verbs" ])您还可以使用filter方法获取与某些条件匹配的所有单词的列表:
# get a list of ALL words that start with "am"
r . filter ( starts_with = "am" )
# you can use all the options found in the word method:
r . filter ( ends_with = "k" , include_parts_of_speech = [ "verbs" ], word_min_length = 4 )您还可以使用random_words方法生成随机单词列表。这很像过滤器方法,除了您指定要返回的单词数量,并且单词是随机选择的。如果没有足够的单词来满足金额, NoWordsToChooseFrom提出一个例外:
# get a list of 3 random nouns
r . random_words ( 3 , include_parts_of_speech = [ "nouns" ])
# you can use all the options found in the word method
r . random_words ( 5 , starts_with = "o" , word_min_length = 10 )
# if the amount of words you want to get is larger than the amount of words
# there are, a NoWordsToChooseFrom exception is raised:
r . random_words ( 100 , starts_with = "n" , word_min_length = 16 )
# there are less than 100 words that are at least 16 letters long and start with
# n, so an exception is raised
# you can silence the NoWordsToChooseFrom exception and return all words even
# if there are less, by setting return_less_if_necessary to True
r . random_words ( 100 , starts_with = "n" , word_min_length = 16 , return_less_if_necessary = True )使用随机RandomSentence生成随机句子很容易:
from wonderwords import RandomSentence
s = RandomSentence ()
# Get a random bare-bone sentence
s . bare_bone_sentence ()
# Get a random bare-bone sentence with a direct object
s . simple_sentence ()
# Get a random bare-bone sentence with an adjective
s . bare_bone_with_adjective ()
# Get a random sentence with a subject, predicate, direct object and adjective
s . sentence ()单词是在类别中组织的,例如“名词”,“动词”和“形容词”。如果您有自己的单词类别怎么办?实例化RandomWord类时,您可以指定自定义类别:
from wonderwords import RandomWord
cars = [ "Chevrolet" , "Subaru" , "Tesla" ]
airplanes = [ "Boeing" , "Airbus" , "Cessna" ]
w = RandomWord ( cars = cars , airplanes = airplanes )
# Will return a random car or airplane
w . word ()
# Will return a random car
w . word ( include_categories = [ "cars" ])
# You can also mix and match custom categories with defaults
from wonderwords import Defaults
proper_nouns = [ "Austin" , "Seattle" , "New York" ]
w2 = RandomWord ( proper_nouns = proper_nouns , common_nouns = Defaults . NOUNS )
# Will return either Seattle or seat
w . word ( regex = "[Ss]eat.*" )最后,从版本2.3开始,Wonderwords具有明确的支持,以从单词列表中滤除亵渎。目前,这是基本的:
from wonderwords import is_profanity , filter_profanity
# Test against words that could possibly be offensive. Good of user-facing apps.
is_profanity ( "apple" ) # False
# Can be done with a list
words = [ ... ]
# The function returns a generator, so we convert it to a list
words_clean = list ( filter_profanity ( words ))在文档中找到了更高级的用法(和教程!),例如添加自定义单词类别。所有信息的完整文档可以在以下网址找到:https://wonderwords.readthedocs.io
注意:使用命令行接口(CLI)之前,请确保使用pip install wonderwords[cli]安装CLI的所有必需依赖项。 WonderWords通常不需要依赖项,而是在命令行中使用丰富的色彩输出。
WonderWords也提供了一个命令行接口,可以与wonderwords命令一起使用。用法:
usage: wonderwords [-h] [-w] [-f] [-l LIST] [-s {bb,ss,bba,s}] [-v] [-S STARTS_WITH] [-e ENDS_WITH]
[-p {noun,verb,adjective,nouns,verbs,adjectives} [{noun,verb,adjective,nouns,verbs,adjectives} ...]]
[-m WORD_MIN_LENGTH] [-M WORD_MAX_LENGTH] [-r REGEX] [-x] [-d DELIMITER] [-E]
Generate random words and sentences from the command line. Here is a full list of available commands. To learn more
about each command, go to the documentation at https://wonderwords.readthedocs.io
options:
-h, --help show this help message and exit
-w, --word, --random-word
generate a random word
-f, --filter get a list of all known words matching the criteria specified
-l LIST, --list LIST return a list of a certain length of random words
-s {bb,ss,bba,s}, --sentence {bb,ss,bba,s}
return a sentence based on the structure chosen
-v, --version print the version number and exit
-S STARTS_WITH, --starts-with STARTS_WITH
strings the random word(s) should start with
-e ENDS_WITH, --ends-with ENDS_WITH
strings the random word(s) should end with
-p {noun,verb,adjective,nouns,verbs,adjectives} [{noun,verb,adjective,nouns,verbs,adjectives} ...], --parts-of-speech {noun,verb,adjective,nouns,verbs,adjectives} [{noun,verb,adjective,nouns,verbs,adjectives} ...]
only include certain parts of speech (by default all parts of speech are included)
-m WORD_MIN_LENGTH, --word-min-length WORD_MIN_LENGTH
minimum length of the word(s)
-M WORD_MAX_LENGTH, --word-max-length WORD_MAX_LENGTH
maximum length of the word(s)
-r REGEX, --regex REGEX, --re REGEX, --regular-expression REGEX
a python-style regular expression for the word(s) to match
-x, --exclude-with-spaces
exclude open compounds, such as 'contact lens'
-d DELIMITER, --delimiter DELIMITER
specify the delimiter to put between a list of words, default is ', '
-E, --suppress-error-on-less
suppress errors when less words are returned in a list then wanted
基本命令是:
-w :生成一个随机单词-f :哪个工作非常类似于filter功能,以返回与某个条件匹配的所有单词-l LIST :获取列表LIST随机单词-s {bb,ss,bba,s} :生成一个随机句子:bb :裸骨句ss :简单的句子(直接对象的裸骨句)bba :形容词的裸骨句子s :用形容词生成一个简单的句子在早期阶段,WonderWords没有设定的版本控制系统,因此, v2.0.0-alpha之前的版本混乱。从版本2 alpha开始,WonderWords使用语义版本操作。
WonderWords是开源的,并根据MIT许可分发。有关更多详细信息,请参见许可证。
欢迎所有贡献,我们希望Wonderwords将继续增长。首先阅读贡献贡献指南以及如何入门的CONTRIBUTING.md 。
由于以下工作,Wonderwords已成为可能:
profanitylist.txt