Skip to content

Instantly share code, notes, and snippets.

@ealmansi
Created September 17, 2014 19:08
Show Gist options
  • Save ealmansi/af8f06ef668d72b2bb7b to your computer and use it in GitHub Desktop.
Save ealmansi/af8f06ef668d72b2bb7b to your computer and use it in GitHub Desktop.
Word scrambler (so called "Cambridge Effect")
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import random
def myshuffle(xs):
for i in range(len(xs)):
ind = random.randint(i, len(xs) - 1)
xs[i], xs[ind] = xs[ind], xs[i]
return xs
def word_shuffle(p):
if (len(p) < 2):
return p
mid = list(p[1:-1])
mid = myshuffle(mid)
mid = ''.join(mid)
return p[0] + mid + p[-1]
def main():
text = "Cognitive science is the interdisciplinary scientific study of the mind and its processes. It examines what cognition is, what it does and how it works."
text = re.sub(r'([^a-zA-ZñÑáéíóúÁÉÍÓÚ0-9])', ' \g<1> ', text)
text = re.sub(r'\s+', ' ', text)
text = text.split(' ')
text = [word_shuffle(p) for p in text]
text = " ".join(text)
text = re.sub(r' ([:;,.])', '\g<1>', text)
text = re.sub(r'([(]) ', '\g<1>', text)
text = re.sub(r' ([)])', '\g<1>', text)
print(text)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment