I want to select n pairs of word positions (unique) in a file and swap them. My code is as following, but it never gives me n pairs I am looking for.
My logic is first to sample 2*n elements and create n pairs. Then iterate over the file contents (stored in list) and locate the correct positions and make the swap. It is not working. I have difficulty in understanding where it fails.
with open(rname) as rd:
lines = rd.readlines()
for i in range(len(lines)):
lines[i] = lines[i].strip()
sz = sum(len(x.split()) for x in lines)
chosen = random.sample(range(sz),6000)
result = pairwise(chosen)
for (a,b) in result:
flag1 = 0
flag2 = 0
l = 0
ind1 = 0
m = 0
ind2 = 0
j = 0
for y in range(len(lines)):
if ( (j+len(lines[y].split()) >= a) and (flag1 == 0)):
l = y
ind1 = a - j-1
flag1 = 1
if ( (j+len(lines[y].split()) >= b) and (flag2 == 0)):
m = y
ind2 = b - j-1
flag2 = 1
if ( (flag1 == 1) and (flag2 == 1)):
words1 = lines[l].split()
words2 = lines[m].split()
words1[ind1], words2[ind2] = 'swapped'+words2[ind2], 'swapped'+words1[ind1]
lines[l] = ' '.join(words1)
lines[m] = ' '.join(words2)
break
j += len(lines[y].split())
name ='n_file.txt'
with open(wname,'w') as wd:
for line in lines:
print(line, file=wd)
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return zip(a, b)
For example, consider a file with content
i am here, where are you it is none of your business
Let us say i choose 2 positions (2,8) (position is defined in terms of the word positions in the file and start with 0 for the first word in the file). My output will look like
i am swappednone where are you it is swappedhere, of your business
Aucun commentaire:
Enregistrer un commentaire