英语中随机单词和句子的python包

wonderwords的Python项目详细描述


妙语

在python中轻松生成随机单词和句子

PyPI - DownloadsLibraries.io SourceRankPyPI - License

快速链接:


目录

  • 安装
  • 使用
    • Wonderwords Python API
    • Wonderwords命令行界面
  • 版本控制
  • 许可证
  • 贡献

安装

要安装最新版本的Wonderwords,请使用您最喜欢的软件包管理器 为Python包索引安装wonderwords包。例如 使用pip:

pip install wonderwords

要使用pip升级Wonderwords,请使用:

^{pr2}$

要验证安装是否有效,请在python中导入Wonderwords:

importwonderwords

如果出现ModuleNotFound错误,请确保已安装 上面这一步的妙语。关于进一步的问题, open a new issue from the GitHub page。在

使用

本节将简要介绍Wonderwords的用法。既然wonderwords 一个命令行界面和python模块,您将发现两个子部分。在

Wonderwords Python API

基本随机单词生成类是RandomWord类。你可以 使用word方法生成单词:

fromwonderwordsimportRandomWordr=RandomWord()# generate a random wordr.word()# random word that starts with a and ends with enr.word(starts_with="a",ends_with="en")# generate a random noun or adjective, by default all parts of speech are includedr.word(include_parts_of_speech=["nouns","adjectives"])# generate a random word between the length of 3 and 8 charactersr.word(word_min_length=3,word_max_length=8)# you can combine multiple filtering optionsr.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方法生成一个单词的随机列表。 这很像filter方法,只是指定了单词的数量 返回,单词是随机选择的。如果没有足够的话 满足该数量时,将引发NoWordsToChooseFrom异常:

# get a list of 3 random nounsr.random_words(3,include_parts_of_speech=["nouns"])# you can use all the options found in the word methodr.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 Truer.random_words(100,starts_with="n",word_min_length=16,return_less_if_necessary=True)

使用RandomSentence类很容易生成随机句子:

fromwonderwordsimportRandomSentences=RandomSentence()# Get a random bare-bone sentences.bare_bone_sentence()# Get a random bare-bone sentence with a direct objects.simple_sentence()# Get a random bare-bone sentence with an adjectives.bare_bone_with_adjective()# Get a random sentence with a subject, predicate, direct object and adjectives.sentence()

包含所有信息的完整文档可在以下网址找到: https://wonderwords.readthedocs.io

Wonderwords命令行界面

Wonderwords也提供了一个命令行界面,可以与 wonderwords命令。用法:

usage: wonderwords [-h] [-w] [-f] [-l LIST] [-s {bb,ss,bba,s}] [-v]
                   [-sw STARTS_WITH] [-ew ENDS_WITH]
                   [-p {nouns,verbs,adjectives} [{nouns,verbs,adjectives} ...]]
                   [-min WORD_MIN_LENGTH] [-max WORD_MAX_LENGTH]
                   [-d DELIMITER]

optional arguments:
  -h, --help            show the help message and exit
  -w, --word, --random-word
                        generate a random word
  -f, --filter          filter a list of words matching the criteria specified
  -l LIST, --list LIST  return a list of words of a certain length
  -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
  -sw STARTS_WITH, --starts-with STARTS_WITH
                        specify what string the random word(s) should start
                        with
  -ew ENDS_WITH, --ends-with ENDS_WITH
                        specify what string the random word(s) should end with
  -p {nouns,verbs,adjectives} [{nouns,verbs,adjectives} ...], --parts-of-speech {nouns,verbs,adjectives} [{nouns,verbs,adjectives} ...]
                        specify to only include certain parts of speech (by
                        default all parts of speech are included)
  -min WORD_MIN_LENGTH, --word-min-length WORD_MIN_LENGTH
                        specify the minimum length of the word(s)
  -max WORD_MAX_LENGTH, --word-max-length WORD_MAX_LENGTH
                        specify the maximum length of the word(s)
  -d DELIMITER, --delimiter DELIMITER
                        Specify the delimiter to put between a list of words,
                        default is ', '

基本命令包括:

  • -w:生成一个随机字
  • -f:其工作原理与filter函数非常相似,可以返回所有匹配的单词 一定的标准
  • -l LIST:获取LIST随机单词的列表
  • -s {bb,ss,bba,s}:生成一个随机句子:
    • bb:裸句
    • ss:简单句(直接宾语裸句)
    • ^带形容词的句子
    • s:用形容词生成一个简单的句子

版本控制

在早期阶段,Wonderwords没有一套版本控制系统 因此,v2.0.0-alpha之前的版本是混乱的。从版本开始 2 alpha,Wonderwords使用sematic versioning。在

许可证

Wonderwords是开放源码的,根据麻省理工学院的许可证发行。参见许可证 更多细节。在

贡献

欢迎所有的贡献,我们希望wonderwords能够继续增长。 从阅读开始贡献.md提供指导和如何获得 起动。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java将Map<String,String>传递给需要Map<String,Object>   java在循环中使用字符串而不是StringBuilder是否会造成内存损失?   jnlp如何更新java控制台JRE?   java更改、修改和重新打包CXFAPI源文件   JavaFXJava应用程序在Fedora上运行一段时间后关闭   使用来自不同类的方法的java   java如何通过ant脚本在linux中使用subst?   java在使用camunda modeler进行base64编码/解码时出错   获取java。netbeans、weblogic和fastswap设置为true时的lang.NoSuchMethodError   java如何提高FinalizerThread在GC中收集对象的优先级   java检测具有相同根的单词   netbeans crud应用程序中的java错误