Skip to content

Instantly share code, notes, and snippets.

@nutszebra
Created November 20, 2015 14:40
Show Gist options
  • Save nutszebra/436c7c748a5cd493c22a to your computer and use it in GitHub Desktop.
Save nutszebra/436c7c748a5cd493c22a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Link: http://www.cl.ecei.tohoku.ac.jp/nlp100/
"""
Question 09:
09. Typoglycemia
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,
それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.
ただし,長さが4以下の単語は並び替えないこととする.
適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")
を与え,その実行結果を確認せよ.
"""
"""***********************************************************
100_questions_NLP_005よりparseSentenceの関数を定義する
***********************************************************"""
import re
def parseSentence(sentence):
return re.findall(r"[\w,']+|,|\.|!|:", sentence)
"""*********************************************************
link: https://gist.github.com/nutszebra/5e29c345b700498bcc5b
*********************************************************"""
import numpy as np
sentence = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind."
answer = []
length = 4
def shuffle(word):
answer = ""
for i in np.random.permutation(len(word)):
answer = answer + word[i]
return answer
def removeSpaceBeforeDot(sentence):
return re.sub(r"\s+\.$",".",sentence)
for word in parseSentence(sentence):
if len(word) <= length:
answer.append(word)
else:
answer.append(word[0] + shuffle(word[1:-1]) + word[-1])
print("original: " + sentence)
print("shuffled: " + removeSpaceBeforeDot(" ".join(answer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment