Hasilkan kata dan kalimat acak dengan mudah di Python
Repositori GitHub | Pypi | Dokumentasi
WonderWords adalah paket Python yang berguna untuk menghasilkan kata -kata acak dan kalimat acak terstruktur. Ini juga dilengkapi dengan antarmuka baris perintah warna -warni untuk menghasilkan kata -kata acak dengan cepat. Versi terbaru tersedia di GitHub sementara versi stabil tersedia di PYPI.
Inilah yang mampu Wonderwords:
Untuk menginstal versi WonderWords terbaru, gunakan manajer paket favorit Anda untuk indeks paket Python untuk menginstal paket wonderwords . Misalnya dengan PIP:
pip install wonderwordsUntuk meningkatkan WonderWords dengan Pip Use:
pip install --upgrade wonderwordsUntuk memverifikasi bahwa instalasi berhasil, impor Wonderwords di Python:
import wonderwords Jika Anda mendapatkan kesalahan ModuleNotFound , pastikan bahwa Anda telah menginstal Wonderwords dari langkah di atas. Untuk masalah lebih lanjut, buka masalah baru dari halaman GitHub.
Bagian ini akan secara singkat menjelaskan penggunaan Wonderwords. Karena Wonderwords memiliki antarmuka baris perintah dan modul Python, Anda akan menemukan dua subbagian.
Kelas pembuatan kata acak dasar adalah kelas RandomWord . Anda dapat menghasilkan kata dengan metode 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" ]) Anda juga bisa mendapatkan daftar semua kata yang cocok dengan beberapa kriteria menggunakan metode 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 ) Anda juga dapat menghasilkan daftar kata acak dengan metode random_words . Ini sangat mirip dengan metode filter, kecuali Anda menentukan jumlah kata yang akan dikembalikan, dan kata -kata tersebut dipilih secara acak. Jika tidak ada kata yang cukup untuk memenuhi jumlahnya, pengecualian NoWordsToChooseFrom dinaikkan:
# 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 ) Menghasilkan kalimat acak mudah menggunakan kelas 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 () Kata -kata diatur dalam kategori, seperti "kata benda", "kata kerja", dan "kata sifat". Bagaimana jika Anda memiliki kategori kata sendiri? Anda dapat menentukan kategori kustom Anda saat instantiasi kelas 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.*" )Akhirnya, dimulai dengan versi 2.3, WonderWords memiliki dukungan eksplisit untuk memfilter kata -kata kotor dari daftar kata. Saat ini, ini belum sempurna:
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 ))Penggunaan yang lebih maju (dan tutorial!) Ditemukan dalam dokumentasi, seperti menambahkan kategori kata khusus. Dokumentasi lengkap dengan semua informasi dapat ditemukan di: https://wonderwords.readthedocs.io
Catatan : Sebelum menggunakan antarmuka baris perintah (CLI), pastikan Anda menginstal semua dependensi yang diperlukan untuk CLI menggunakan pip install wonderwords[cli] . Wonderwords biasanya tidak memerlukan dependensi, tetapi menggunakan kaya untuk output berwarna di baris perintah.
Wonderwords menyediakan antarmuka baris perintah, juga, yang dapat digunakan dengan perintah wonderwords . Penggunaan:
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
Perintah dasarnya adalah:
-w : menghasilkan kata acak-f : yang berfungsi seperti fungsi filter untuk mengembalikan semua kata yang sesuai dengan kriteria tertentu-l LIST : Dapatkan daftar kata acak LIST-s {bb,ss,bba,s} : menghasilkan kalimat acak:bb : kalimat tulang telanjangss : kalimat sederhana (kalimat tulang telanjang dengan objek langsung)bba : Kalimat tulang telanjang dengan kata sifats : menghasilkan kalimat sederhana dengan kata sifat Selama tahap awal, WonderWords tidak memiliki sistem versi yang ditetapkan dan oleh karena itu, versi sebelum v2.0.0-alpha berantakan. Dimulai dengan Versi 2 Alpha, WonderWords menggunakan versi Sematic .
Wonderwords adalah open source dan didistribusikan di bawah lisensi MIT. Lihat lisensi untuk detail lebih lanjut.
Semua kontribusi dipersilakan, dan kami berharap Wonderwords akan terus tumbuh. Mulailah dengan membaca CONTRIBUTING.md untuk menyumbangkan pedoman dan cara memulai.
Wonderwords telah dimungkinkan berkat karya -karya berikut:
profanitylist.txt dari RobertJgabriel/Google-Profanity-words di bawah lisensi apache-2.0