Python實現(xiàn)的生成自我描述腳本分享(很有意思的程序)
自我描述的語句指這樣一種語句:它的內(nèi)容就是對它本身的描述。(廢話……)比如下面這句句子:
這是一段自我描述的語句,除了標點符號外,它共包含125個字符,其中33個“個”,29個“2”,5個“3”,3個“符”,3個“5”,2個“一”,2個“它”,2個“包”,2個“的”,2個“標”,2個“了”,2個“我”,2個“外”,2個“含”,2個“中”,2個“是”,2個“1”,2個“段”,2個“點”,2個“描”,2個“9”,2個“字”,2個“這”,2個“句”,2個“除”,2個“自”,2個“語”,2個“共”,2個“述”,2個“號”,2個“其”。
這句話是我用一段 Python 腳本生成的,生成原理大致如下:
1、給出一個模板,讓句子的各個內(nèi)容知道自己該出現(xiàn)在哪個部位;
2、根據(jù)當前信息,生成句子;
3、將當前句子作為輸入,再次執(zhí)行第 2 步的操作;
4、直到句子各部分內(nèi)容的信息都正確。
簡單來說,就是一個不斷迭代修正的過程。
其中需要注意的是,每次迭代時應(yīng)該盡量只改動一個地方,以免兩處同時變化相互影響,造成死循環(huán);另外,如果句子中有多處地方需要修正,盡量隨機選取一處進行修正,而不要按一定順序進行修正,同樣是為了減少陷入死循環(huán)的風險。
不過,即使如此,某些情況下還是有可能陷入死循環(huán),比如如果某一步得到了下面這樣的句子:
這句很 2 的話包含 3 個“2”。
上面這句話明顯是錯誤的,因為其中只有兩個“2”。那么,我們把那個“3”改為“2”,是不是就對了呢?很容易發(fā)現(xiàn),如果我們做了這樣的改動之后,句子將變成:
這句很 2 的話包含 2 個“2”。
這時,句子中又包含三個“2”了。像這樣的句子就似乎無法簡單地改為正確的自我描述語句,因為無論如何改都會陷入死循環(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"這是一段自我描述的語句,除了標點符號外,它共包含{length}個字符,其中{detail}。"
sd = SelfDesc(template)
sd.generate()
print u"%s" % sd
if __name__ == "__main__":
main()
相關(guān)文章
Python中.join()和os.path.join()兩個函數(shù)的用法詳解
join()是連接字符串數(shù)組而os.path.join()是將多個路徑組合后返回。接下來通過本文重點給大家介紹Python中.join()和os.path.join()兩個函數(shù)的用法,感興趣的朋友一起看看吧2018-06-06
python 實現(xiàn)數(shù)字字符串左側(cè)補零的方法
今天小編就為大家分享一篇python 實現(xiàn)數(shù)字字符串左側(cè)補零的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
實例探究Python以并發(fā)方式編寫高性能端口掃描器的方法
端口掃描器就是向一批端口上發(fā)送請求來檢測端口是否打開的程序,這里我們以實例探究Python以并發(fā)方式編寫高性能端口掃描器的方法2016-06-06
利用Tensorboard繪制網(wǎng)絡(luò)識別準確率和loss曲線實例
今天小編就為大家分享一篇利用Tensorboard繪制網(wǎng)絡(luò)識別準確率和loss曲線實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python結(jié)合API實現(xiàn)即時天氣信息
這篇文章主要介紹了python結(jié)合API實現(xiàn)即時天氣信息的代碼,非常的實用,有需要的小伙伴可以參考下。2016-01-01

