Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save endaaman/46da5d1a553c90326676 to your computer and use it in GitHub Desktop.
Save endaaman/46da5d1a553c90326676 to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
import MeCab
import random
def make_strange(message, options={}):
tagger = MeCab.Tagger('mecabrc')
text = message.encode('utf-8')
node = tagger.parseToNode(text)
words = []
nouns = []
index = 0
noun_index = 0
while node:
pos = node.feature.split(',')[0]
word = node.surface
noun = pos == '名詞'
if noun:
words.append([word, noun_index])
nouns.append(word)
noun_index = noun_index + 1
else:
words.append([word, -1])
index = index + 1
node = node.next
random.shuffle(nouns)
new_words = []
for i, pair in enumerate(words):
if pair[1] < 0:
word = pair[0]
else:
word = nouns[pair[1]]
new_words.append(word)
return ''.join(new_words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment