Last active
August 29, 2015 14:16
-
-
Save endaaman/46da5d1a553c90326676 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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