第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用此 CFG 隨機(jī)生成字符串?

如何使用此 CFG 隨機(jī)生成字符串?

喵喵時(shí)光機(jī) 2022-09-27 16:12:50
我有這個(gè)代碼描述一個(gè)上下文無(wú)關(guān)的語(yǔ)法,我試圖生成匹配它的隨機(jī)字符串;例如,像這樣:“約翰認(rèn)為瑪麗討厭每只綠貓”但我目前的輸出是:[['_S', ['_NP _VP']], ['_NP', ['_Det _Adj _N', '_Det _N', '_Adj _PropN', '_PropN']], ['_VP', ['_Vi', '_Vt _NP', '_Vc _Comp _S']]][['_Det', ['the', 'a', 'some', 'any', 'every']], ['_Adj', ['green', 'young', 'tired', 'confused']], ['_N', ['dog', 'cat']], ['_PropN', ['John', 'Mary']], ['_Vi', ['sleeps', 'walks']], ['_Vt', ['loves', 'hates']], ['_Vc', ['says', 'thinks', 'believes']], ['_Comp', ['that']]]請(qǐng)幫忙!import randompsg_rules_str = "S → NP VP\n" \                "NP → Det Adj N | Det N | Adj PropN | PropN\n" \                "VP → Vi | Vt NP | Vc Comp S"terminals_str = "Det → the | a | some | any | every\n" \                "Adj → green | young | tired | confused\n" \                "N → dog | cat\n" \                "PropN → John | Mary\n" \                "Vi → sleeps | walks\n" \                "Vt → loves | hates\n" \                "Vc → says | thinks | believes\n" \                "Comp → that"psg_rules_list = [a.split("→") for a in psg_rules_str.split("\n")]for p in psg_rules_list:    p[0] = "_" + p[0].strip()    p[1] = p[1].split("|")    p[1] = ["_" + a.strip().replace(" ", " _") for a in p[1]]print(psg_rules_list)# [['_S', ['_NP _VP']], ['_NP', ['_Det _Adj _N', '_Det _N', '_Adj _PropN', '_PropN']], ['_VP', ['_Vi', '_Vt _NP', '_Vc _Comp _S']]]terminals_list = [a.split("→") for a in terminals_str.split("\n")]for t in terminals_list:    t[0] = "_" + t[0].strip()    t[1] = t[1].split("|")    t[1] = [a.strip() for a in t[1]]print(terminals_list)
查看完整描述

1 回答

?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

你幾乎讓程序工作。以下是完成該函數(shù)的方法:reachTerminals


import random


psg_rules_str = "S → NP VP\n" \

                "NP → Det Adj N | Det N | Adj PropN | PropN\n" \

                "VP → Vi | Vt NP | Vc Comp S"


terminals_str = "Det → the | a | some | any | every\n" \

                "Adj → green | young | tired | confused\n" \

                "N → dog | cat\n" \

                "PropN → John | Mary\n" \

                "Vi → sleeps | walks\n" \

                "Vt → loves | hates\n" \

                "Vc → says | thinks | believes\n" \

                "Comp → that"


psg_rules_list = [a.split("→") for a in psg_rules_str.split("\n")]

for p in psg_rules_list:

    p[0] = "_" + p[0].strip()

    p[1] = p[1].split("|")

    p[1] = ["_" + a.strip().replace(" ", " _") for a in p[1]]


terminals_list = [a.split("→") for a in terminals_str.split("\n")]

for t in terminals_list:

    t[0] = "_" + t[0].strip()

    t[1] = t[1].split("|")

    t[1] = [a.strip() for a in t[1]]


def reachTerminals(from_nts, with_rules, with_ts):

    from_nts = str.upper("_" + from_nts.replace("_", "").strip().replace(" ", " _"))

    rule_tags = [a[0] for a in with_rules]

    ts_tags = [a[0] for a in with_ts]

    nts_todo = [a for a in rule_tags if a in from_nts]

    while nts_todo:

        for tag in nts_todo:

            wr_index = rule_tags.index(tag)

            repl_choices = with_rules[wr_index][1]


            choice = random.choice(repl_choices)

            from_nts = from_nts.replace(tag, choice, 1)

        nts_todo = [a for a in rule_tags if a in from_nts]


    ts_todo = [a for a in ts_tags if a in from_nts]

    while ts_todo:

        for tag in ts_todo:

            wr_index = ts_tags.index(tag)

            repl_choices = with_ts[wr_index][1]


            choice = random.choice(repl_choices)

            from_nts = from_nts.replace(tag, choice, 1)

        ts_todo = [a for a in ts_tags if a in from_nts]


    return from_nts



print(reachTerminals(from_nts = "s", with_rules = psg_rules_list, with_ts = terminals_list))

您可以使用的重要工具是 random.option 函數(shù)和 str.replace 函數(shù)的第三個(gè)參數(shù),它只允許您替換子字符串的第一個(gè)匹配項(xiàng)。我還沒(méi)有徹底測(cè)試代碼,但它似乎按預(yù)期工作。輸出示例:


green John loves some confused dog


Mary says that the tired dog says that some green cat hates some cat


every green dog loves young John


John loves the tired cat


查看完整回答
反對(duì) 回復(fù) 2022-09-27
  • 1 回答
  • 0 關(guān)注
  • 78 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)