python 随机生成简单语法结构的中文句子

python编程练习 专栏收录该内容
1 篇文章 0 订阅

简单的中文语句生成器

python 随机生成简单语法结构的中文句子

题目如下:Writing a programming which could generate random Chinese sentences based
on one grammar.
Your input grammar is:
simple_grammar = “”"
sentence => noun_phrase verb_phrase noun_phrase => Article Adj* noun
Adj* => null | Adj Adj*
verb_phrase => verb noun_phrase
Article => 一个 | 这个
noun =>女人|篮球|桌子|小猫
verb => 看着 | 听着 | 看见
Adj =>蓝色的 |好看的 |小小的 |年轻的 “”"
Your task is define a function called generate, if we call generate(‘sentence’),
you could see some sentences like:

generate(“sentence”)
Output: 这个蓝色的女人看着一个小猫

generate(“sentence”)
Output: 这个好看的小猫坐在一个女人

解题思路

类 ClassGenerator
类型 :基本内容列表 一维#
Adj = [“蓝色的”,“好看的”,“小小的”,“年轻的”]
verb = [“看着”,“听着”,“看见”,“听见”]
noun = [“女人”,“小猫”,“篮球”,“桌子”]
Article = [“一个”,“这个”]

       组合内容列表
            noun_phrase = Article[i]+Adj*+noun[j]
            verb_phrase = verb[i]+noun_phrase
            Adj* = [ ]或者adj[i]或者adj[i]+adj[j]...

#output结果:
sentence = noun_phrase+verb_phrase

#####方法:
sentGene():

            调用 noun_phrase_Gene    调用verb_phrase_Gene
              sentence = noun_phrase+verb_phrase

        noun_phrase();
              random i in range(2)  取Article[i]
              调用 AdjGet 取 Adj*
              random j in range(4)  get noun[j]
           return noun_phrase = Article[i]+Adj*+noun[j]

        verb_phrase_Gene():
              random i in range(4)  取verb[i]
              random j in range(4)  get noun[j]
            return verb_phrase = verb[i]+noun_phrase

         AdjGet():
              random k in range(4)  确定 adj个数
              random k个序号in range(4) get k个adj

              return Adj* = [ ]或者adj[i]或者adj[i]+adj[j]...

代码

import  numpy as np
import random
class ClassGenerator:
    Adj = ["蓝色的","好看的","小小的","年轻的"]
    verb = ["看着","听着","看见","听见"]
    noun = ["女人","小猫","篮球","桌子"]
    Article = ["一个","这个"]

    def AdjGet(self):
        Adj_num = int(np.random.uniform(0, 4))  #形容词数量
        assert Adj_num<=4
        print("the num of adj is",Adj_num)
        adj_group_number =[random.randint(0,3) for i in range(Adj_num)]                            #list(np.random.uniform(0,4,Adj_num))  #形容词序号列表    np的随机函数返回的是ndarry多维数组
        #print(adj_group_number.shape)
        adj_group = []
        for i in range(Adj_num):
            k =adj_group_number[i]
            adj_group = "".join(adj_group)+self.Adj[k]
        return "".join(adj_group)               #list转字符串

    def verb_phrase_Gene(self):
        verb_num = int(np.random.uniform(0, 4))
        noun_num = int(np.random.uniform(0, 4))
        return self.verb[verb_num]+self.noun[noun_num]

    def noun_phrase_gene(self):
        Article_num = int(np.random.uniform(0, 2))                 #np.random.uniform取值左右都闭,且返回值是float64类型!
        Adj_Group = self.AdjGet()
        noun_num = int(np.random.uniform(0, 3))
        return self.Article[Article_num]+Adj_Group+self.noun[noun_num]


    def sentGene(self):
        noun_phr = self.noun_phrase_gene()
        verb_phr = self.verb_phrase_Gene()
        return noun_phr+verb_phr

while(1):
    key = input("生成语句?Y|N")
    if key == 'Y':
        print(">>generate(\"sentence\")")
        generate = ClassGenerator()
        sentence = generate.sentGene()
        print(sentence)
    if key =='N':
        break

![Alt](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hdmF0YXIuY3Nkbi5uZXQvNy83L0IvMV9yYWxmX2h4MTYzY29tLm在这里插入图片描述在这里插入图片描述

笔记

random — 生成伪随机数

https://docs.python.org/zh-cn/3/library/random.html?highlight=np%20random%20uniform#random.getrandbits

“”.join(adj_group) #list转字符串

  • 1
    点赞
  • 0
    评论
  • 5
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值