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