[PYTHON] 100 Amateur Language Processing Knock: 09

It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).

Chapter 1: Preparatory movement

09.Typoglycemia

Create a program that randomly rearranges the order of the other letters, leaving the first and last letters of each word for the word string separated by spaces. However, words with a length of 4 or less are not rearranged. Give an appropriate English sentence (for example, "I couldn't believe that I could actually understand what I was reading: the phenomenal power of the human mind.") And check the execution result.

The finished code:

main.py


# coding: utf-8
import random


def Typoglycemia(target):
	'''Typoglycemia
For a space-separated word sequence, leave the first and last letters of each word,
Randomly rearrange the order of other characters.
However, words with a length of 4 or less are not rearranged.

argument:
	target --Target character string
Return value:
Converted string
	'''
	result = []
	for word in target.split(' '):
		if len(word) <= 4:
			result.append(word)
		else:
			chr_list = list(word[1:-1])
			random.shuffle(chr_list)
			result.append(word[0] + ''.join(chr_list) + word[-1])

	return ' '.join(result)

#Input of target character string
target = input('Please enter a string--> ')

#Typoglycemia
result = Typoglycemia(target)
print('Conversion result:' + result)

Execution result:

Terminal


Please enter a string--> I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .
Conversion result:I c'udnlot bievele that I colud aclautly utaedsrnnd what I was rainedg : the penomaenhl power of the human mind .

Random number generation logic

I used random.shuffle () for random sorting. With this function, you can also specify the random number generation function to be used for sorting, but since it is omitted this time, random.random () /random.html#random.random) is used. The generation logic in this case is [Mersenne Twister](https://ja.wikipedia.org/wiki/Mersenne Twister). I'm happy that excellent random numbers are generated by default.

There is also a warning in Explanation of random module, but since the random numbers generated by this logic can be predicted, It is dangerous to use it as it is for the purpose of encryption. Please be careful.

Typoglycemia

The phenomenon of typoglycemia, which is wrong but somehow readable, is quite interesting. However, the problem is that if you are not good at English, you can only read it even if the spelling is correct ...

That's why I also tried it in Japanese. In the case of Japanese, it is okay to rearrange even 4 characters, so this is a version in which "length 4 or less" is changed to "length 3 or less".

The finished code:

main2.py


# coding: utf-8
import random


def Typoglycemia(target):
	'''Typoglycemia [Japanese version]
For a space-separated word sequence, leave the first and last letters of each word,
Randomly rearrange the order of other characters.
However, words with a length of 3 or less are not rearranged.

argument:
	target --Target character string
Return value:
Converted string
	'''
	result = []
	for word in target.split(' '):
		if len(word) <= 3:
			result.append(word)
		else:
			chr_list = list(word[1:-1])
			random.shuffle(chr_list)
			result.append(word[0] + ''.join(chr_list) + word[-1])

	return ' '.join(result)

#Input of target character string
target = input('Please enter a string--> ')

#Typoglycemia
result = Typoglycemia(target)
print('Conversion result:' + result)

Execution result:

Terminal


Please enter a string-->I started the Saikin Paison Benkyo, but it's quite interesting, isn't it?
Conversion result:Watashihasakiin Paison's Benkyo and other mutations are quite interesting, aren't they?

In the case of Japanese, you have to put a space in hiragana, but somehow it became understandable.

This is Jinpoume's Nokku. I'm glad you can do it if you don't like it ^^

Recommended Posts

100 Amateur Language Processing Knock: 17
100 Amateur Language Processing Knock: 07
100 Amateur Language Processing Knock: 09
100 Amateur Language Processing Knock: 47
100 Amateur Language Processing Knock: 97
100 Amateur Language Processing Knock: 67
100 Amateur Language Processing Knock: Summary
100 Language Processing Knock (2020): 38
100 language processing knock 00 ~ 02
100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 language processing knock 2020 [00 ~ 39 answer]
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 language processing knock 2020 [00-79 answer]
100 language processing knock 2020 [00 ~ 69 answer]
100 amateur language processing knocks: 59
100 amateur language processing knocks: 62
100 amateur language processing knocks: 60
100 Language Processing Knock 2020 Chapter 1
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 06
100 amateur language processing knocks: 84
100 language processing knock 2020 [00 ~ 49 answer]
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 Language Processing Knock-52: Stemming
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
100 amateur language processing knocks: 04
100 Language Processing Knock Chapter 1
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 42
100 amateur language processing knocks: 19
100 amateur language processing knocks: 98
100 amateur language processing knocks: 83
100 amateur language processing knocks: 95
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 79
100 amateur language processing knocks: 23
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 68
100 amateur language processing knocks: 11