Python實(shí)現(xiàn)的生成自我描述腳本分享(很有意思的程序)
自我描述的語句指這樣一種語句:它的內(nèi)容就是對它本身的描述。(廢話……)比如下面這句句子:
這是一段自我描述的語句,除了標(biāo)點(diǎn)符號外,它共包含125個字符,其中33個“個”,29個“2”,5個“3”,3個“符”,3個“5”,2個“一”,2個“它”,2個“包”,2個“的”,2個“標(biāo)”,2個“了”,2個“我”,2個“外”,2個“含”,2個“中”,2個“是”,2個“1”,2個“段”,2個“點(diǎn)”,2個“描”,2個“9”,2個“字”,2個“這”,2個“句”,2個“除”,2個“自”,2個“語”,2個“共”,2個“述”,2個“號”,2個“其”。
這句話是我用一段 Python 腳本生成的,生成原理大致如下:
1、給出一個模板,讓句子的各個內(nèi)容知道自己該出現(xiàn)在哪個部位;
2、根據(jù)當(dāng)前信息,生成句子;
3、將當(dāng)前句子作為輸入,再次執(zhí)行第 2 步的操作;
4、直到句子各部分內(nèi)容的信息都正確。
簡單來說,就是一個不斷迭代修正的過程。
其中需要注意的是,每次迭代時應(yīng)該盡量只改動一個地方,以免兩處同時變化相互影響,造成死循環(huán);另外,如果句子中有多處地方需要修正,盡量隨機(jī)選取一處進(jìn)行修正,而不要按一定順序進(jìn)行修正,同樣是為了減少陷入死循環(huán)的風(fēng)險。
不過,即使如此,某些情況下還是有可能陷入死循環(huán),比如如果某一步得到了下面這樣的句子:
這句很 2 的話包含 3 個“2”。
上面這句話明顯是錯誤的,因?yàn)槠渲兄挥袃蓚€“2”。那么,我們把那個“3”改為“2”,是不是就對了呢?很容易發(fā)現(xiàn),如果我們做了這樣的改動之后,句子將變成:
這句很 2 的話包含 2 個“2”。
這時,句子中又包含三個“2”了。像這樣的句子就似乎無法簡單地改為正確的自我描述語句,因?yàn)闊o論如何改都會陷入死循環(huán)。
最后,我用來生成最上面的那句自我描述語句的 Python 腳本如下:
# -*- coding: utf-8 -*- import random class SelfDesc(object): ignore_chars = u",?!啊? def __init__(self, template): self.template = template self.length = 0 self.detail = "" self.content = "" self.chars = "" self.char_count = {} self.makeContent() self.char_count = self.getCharCount() self.getCharCount() self.makeContent() def __str__(self): return self.content def makeContent(self): self.makeDetail() self.content = self.template.replace(u"{length}", u"%d" % self.length) .replace(u"{detail}", self.detail) self.getChars() def getChars(self): chars = self.content for c in self.ignore_chars: chars = chars.replace(c, "") self.chars = chars return chars def getLength(self): self.length = len(self.chars) def getCharCount(self): d = {} for c in self.chars: if c in self.ignore_chars: continue d.setdefault(c, 0) d[c] += 1 return d def makeDetail(self): d = self.char_count items = d.items() items.sort(key=lambda x: -x[1]) s = [] for c, n in items: s.append(u"%d個“%s”" % (n, c)) self.detail = u",".join(s) def correct(self): print "-" * 50 char_count = self.getCharCount() items = char_count.items() random.shuffle(items) for c, n in items: if n <= 1 and c in self.char_count: del self.char_count[c] continue if self.char_count.get(c) == n: continue else: self.char_count[c] = n return True else: len = self.length self.getLength() if len != self.length: return True return False def generate(self): icount = 0 while self.correct(): icount += 1 self.makeContent() print u"#%d %s" % (icount, self) def main(): template = u"這是一段自我描述的語句,除了標(biāo)點(diǎn)符號外,它共包含{length}個字符,其中{detail}。" sd = SelfDesc(template) sd.generate() print u"%s" % sd if __name__ == "__main__": main()
相關(guān)文章
Numpy中的數(shù)組搜索中np.where方法詳細(xì)介紹
這篇文章主要介紹了Numpy中的數(shù)組搜索中np.where方法詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于Python?OpenCV和?dlib實(shí)現(xiàn)眨眼檢測
這篇文章主要介紹了基于Python?OPenCV及dlib實(shí)現(xiàn)檢測視頻流中的眨眼次數(shù)。文中的代碼對我們的學(xué)習(xí)和工作有一定價值,感興趣的同學(xué)可以參考一下2021-12-12如何使用python實(shí)現(xiàn)模擬鼠標(biāo)點(diǎn)擊
這篇文章主要介紹了如何使用python實(shí)現(xiàn)模擬鼠標(biāo)點(diǎn)擊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01淺析Python中壓縮zipfile與解壓縮tarfile模塊的使用
Python?提供了兩個標(biāo)準(zhǔn)庫模塊來處理文件的壓縮和解壓縮操作:zipfile和tarfile,本文將分享?這兩個模塊的使用方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10VSCode中autopep8無法運(yùn)行問題解決方案(提示Error: Command failed,usage)
這篇文章主要介紹了VSCode中autopep8無法運(yùn)行問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03