Intelligent Poetry
Readme:
Creat poems.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import re 2 import random 3 from collections import Counter 4 5 def Creat_Poem(number): 6 w=random.sample(poem,number) 7 for i in range(number//4): 8 print(w[i],end='') 9 print(',',end='') 10 for i in range(number//4,number//2): 11 print(w[i],end='') 12 print('。') 13 for i in range(number//2,number//4*3): 14 print(w[i],end='') 15 print(',',end='') 16 for i in range(number//4*3,number): 17 print(w[i],end='') 18 print('。') 19 return 20 21 dic=[] 22 data=open('tangshi.txt','r') 23 for i in data.readlines(): 24 dic.append(i.strip().replace(':','').replace(',','').replace('。','').replace('?','').replace('!','').replace('[','').replace(']','')) 25 word=''.join(dic) 26 word_str=re.sub(r"(?<=\w)","",word) 27 word_list= list(word_str) 28 a= [i for i in word_list if not str(i).isdigit()] 29 s =Counter(a).most_common(300) 30 poem=[] 31 for i in s: 32 poem.append(i[0]) 33 number=int(input('Merciful Sir,\n\tPlease input the Number of Words:\n\t')) 34 print() 35 Creat_Poem(number)