当前位置:Gxlcms > Python > Python实现的生成自我描述脚本分享(很有意思的程序)

Python实现的生成自我描述脚本分享(很有意思的程序)

时间:2021-07-01 10:21:17 帮助过:60人阅读

自我描述的语句指这样一种语句:它的内容就是对它本身的描述。(废话……)比如下面这句句子:
代码如下:


这是一段自我描述的语句,除了标点符号外,它共包含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、给出一个模板,让句子的各个内容知道自己该出现在哪个部位;
2、根据当前信息,生成句子;
3、将当前句子作为输入,再次执行第 2 步的操作;
4、直到句子各部分内容的信息都正确。

简单来说,就是一个不断迭代修正的过程。

其中需要注意的是,每次迭代时应该尽量只改动一个地方,以免两处同时变化相互影响,造成死循环;另外,如果句子中有多处地方需要修正,尽量随机选取一处进行修正,而不要按一定顺序进行修正,同样是为了减少陷入死循环的风险。

不过,即使如此,某些情况下还是有可能陷入死循环,比如如果某一步得到了下面这样的句子:

代码如下:


这句很 2 的话包含 3 个“2”。

上面这句话明显是错误的,因为其中只有两个“2”。那么,我们把那个“3”改为“2”,是不是就对了呢?很容易发现,如果我们做了这样的改动之后,句子将变成:

代码如下:


这句很 2 的话包含 2 个“2”。

这时,句子中又包含三个“2”了。像这样的句子就似乎无法简单地改为正确的自我描述语句,因为无论如何改都会陷入死循环。

最后,我用来生成最上面的那句自我描述语句的 Python 脚本如下:

  1. # -*- coding: utf-8 -*-
  2. import random
  3. class SelfDesc(object):
  4. ignore_chars = u",。“”"
  5. def __init__(self, template):
  6. self.template = template
  7. self.length = 0
  8. self.detail = ""
  9. self.content = ""
  10. self.chars = ""
  11. self.char_count = {}
  12. self.makeContent()
  13. self.char_count = self.getCharCount()
  14. self.getCharCount()
  15. self.makeContent()
  16. def __str__(self):
  17. return self.content
  18. def makeContent(self):
  19. self.makeDetail()
  20. self.content = self.template.replace(u"{length}", u"%d" % self.length)
  21. .replace(u"{detail}", self.detail)
  22. self.getChars()
  23. def getChars(self):
  24. chars = self.content
  25. for c in self.ignore_chars:
  26. chars = chars.replace(c, "")
  27. self.chars = chars
  28. return chars
  29. def getLength(self):
  30. self.length = len(self.chars)
  31. def getCharCount(self):
  32. d = {}
  33. for c in self.chars:
  34. if c in self.ignore_chars:
  35. continue
  36. d.setdefault(c, 0)
  37. d[c] += 1
  38. return d
  39. def makeDetail(self):
  40. d = self.char_count
  41. items = d.items()
  42. items.sort(key=lambda x: -x[1])
  43. s = []
  44. for c, n in items:
  45. s.append(u"%d个“%s”" % (n, c))
  46. self.detail = u",".join(s)
  47. def correct(self):
  48. print "-" * 50
  49. char_count = self.getCharCount()
  50. items = char_count.items()
  51. random.shuffle(items)
  52. for c, n in items:
  53. if n <= 1 and c in self.char_count:
  54. del self.char_count[c]
  55. continue
  56. if self.char_count.get(c) == n:
  57. continue
  58. else:
  59. self.char_count[c] = n
  60. return True
  61. else:
  62. len = self.length
  63. self.getLength()
  64. if len != self.length:
  65. return True
  66. return False
  67. def generate(self):
  68. icount = 0
  69. while self.correct():
  70. icount += 1
  71. self.makeContent()
  72. print u"#%d %s" % (icount, self)
  73. def main():
  74. template = u"这是一段自我描述的语句,除了标点符号外,它共包含{length}个字符,其中{detail}。"
  75. sd = SelfDesc(template)
  76. sd.generate()
  77. print u"%s" % sd
  78. if __name__ == "__main__":
  79. main()

人气教程排行