在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