程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use Python to randomly empty text for filling in blanks

編輯:Python
python [py file name ] [ difficulty ] [ Handle text object file names ]
::python Randomly hollowed out text .py 1 input.md

Handle text object file names fileName, The default is input.md

difficulty difficulty, Value 0、1、2、3( The default is 0), Corresponding to the excavation proportion inter Of 25%、50%、75%、100%

Separator seq, Divide text by separator , The following code takes the common full angle symbol as an example

Exclusions exc, Ignore rows that match the regular expression

Reserved items res, It means that when the text is hollowed out , Keep the beginning of the line that the regular expression matches prefix

import sys
import random
import re
fileName='input.md'
if(len(sys.argv)>1):
difficulty=sys.argv[1]
if(len(sys.argv)==3):
fileName=sys.argv[2]
else:
difficulty=0
#difficulty Means difficulty , Represents the proportion of excavation ,0:25%,1:50%,2:75%,3:100%
content=open(fileName,'r',encoding='utf-8')
with open('output.md',"w",encoding='utf-8') as f:
for line in content:
sep='[,.;!?、]'
exc='[#]'
res='[0-9+-][.]'
lineArr=re.split(sep,line)
inter=(int(difficulty)+1)*0.25
for i in range(0,len(lineArr)):
if(lineArr[i]=='\n'):
continue
if(re.match(exc,line) is not None):
continue
elif(random.random()<=inter):
if(len(lineArr[i])>2 and re.match(res,lineArr[i][0:3])):
prefix=lineArr[i][0:3]
else:
prefix=''
lineArr[i]=prefix+'('+''.rjust(len(lineArr[i])).replace(' ',' ')+')'
line=','.join(lineArr)
f.write(line)
f.flush() # Write to hard disk 
f.close() # Close file , And refresh 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved