# -*- 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()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有