#!/usr/bin/python #coding: utf-8 # # Brainfuck Interpreter # Copyright 2011 Sebastian Kaspari (Original Version) # This code was modified by script named "make_byscript_mecab.py". # # Usage: ./cons_jp.py [FILE] import sys import re import MeCab def execute(filename): words_raw = open(filename).read() tagg = MeCab.Tagger() parse = tagg.parseToNode(words_raw) words = [] while parse: feature = unicode(parse.feature, "utf-8") surface = unicode(parse.surface, "utf-8") if feature.startswith(u"名詞") or feature.startswith(u"感動詞"): words.append(surface) parse = parse.next evaluate(words) def evaluate(code): code = cleanup(code) bracemap = buildbracemap(code) cells, codeptr, cellptr = [0], 0, 0 while codeptr < len(code): command = code[codeptr] if command == ">": cellptr += 1 if cellptr == len(cells): cells.append(0) if command == "<": cellptr = 0 if cellptr <= 0 else cellptr - 1 if command == "+": cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0 if command == "-": cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255 if command == "[" and cells[cellptr] == 0: codeptr = bracemap[codeptr] if command == "]" and cells[cellptr] != 0: codeptr = bracemap[codeptr] if command == ".": sys.stdout.write(chr(cells[cellptr])) if command == ",": cells[cellptr] = ord(getch.getch()) codeptr += 1 def cleanup(code): bfDict = { u'日本国憲法':'+', u'昭和':'+', u'十一月':'+', u'子孫':'+', u'協和':'+', u'わが国':'+', u'全土':'+', u'恵沢':'+', u'確保':'[', u'政府':'>', u'惨禍':'+', u'ここ':'+', u'宣言':'+', u'確定':'+', u'そもそも':'[', u'厳粛':'>', u'権威':'+', u'由来':'+', u'福利':'>', u'享受':'+', u'法令':'+', u'排除':'+', u'恒久':'>', u'念願':'+', u'人間':'+', u'自覚':'+', u'公正':'>', u'信義':'+', u'信頼':'<', u'安全':'<', u'専制':'<', u'隷従':'<', u'圧迫':'-', u'偏狭':']', u'地上':'>', u'永遠':'+', u'除去':'>', u'世界':'+', u'恐怖':'>', u'欠乏':'-', u'免':'>', u'確認':'>', u'専念':'+', u'無視':'[', u'道徳':'<', u'対等':']', u'各国':'<', u'責務':'-', u'全力':']', u'達成':'>', u'誓':'>', u'統合':'.', u'総意':'>', u'皇位':'-', u'世襲':'-', u'継承':'-', u'公示':'.', u'任免':'+', u'全権':'+', u'批准':'+', u'書':'+', u'文書':'+', u'接受':'+', u'賜':'+', u'正義':'.', u'基調':'.', u'希求':'+', u'発動':'+', u'威嚇':'+', u'国際紛争':'.', u'解決':'>', u'手段':'>', u'陸海空':'.', u'軍':'<', u'戦力':'-', u'交戦':'.', u'要件':'<', u'享有':'.', u'不断':'+', u'濫用':'+', u'個人':'+', u'幸福':'.', u'追求':'-', u'最大':'-', u'経済':'-', u'華族':'-', u'貴族':'-', u'制度':'-', u'栄誉':'.', u'勲章':'-', u'伴':'-', u'一代':'-', u'固有':'-', u'全体':'-', u'成年':'-', u'普通選挙':'-', u'公的':'-', u'私的':'.', u'救済':'>', u'廃止':'>', u'平穏':'+', u'待遇':'.', u'不法行為':'>', u'公共団体':'+', u'賠償':'+', u'奴隷':'.', } ans = [bfDict[word] for word in code if (word in bfDict)] tmp = "" for word in ans: tmp = tmp+word return tmp return ans def buildbracemap(code): temp_bracestack, bracemap = [], {} for position, command in enumerate(code): if command == "[": temp_bracestack.append(position) if command == "]": start = temp_bracestack.pop() bracemap[start] = position bracemap[position] = start return bracemap # From http://code.activestate.com/recipes/134892/ # "getch()-like unbuffered character reading from stdin # on both Windows and Unix (Python recipe)" class _Getch: '''Gets a single character from standard input. Does not echo to the screen.''' def __init__(self): try: self.impl = _GetchWindows() except ImportError: self.impl = _GetchUnix() def __call__(self): return self.impl() class _GetchUnix: def __init__(self): import tty, sys def __call__(self): import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch class _GetchWindows: def __init__(self): import msvcrt def __call__(self): import msvcrt return msvcrt.getch() getch = _Getch() #getch() ends here def main(): if len(sys.argv) == 2: execute(sys.argv[1]) else: print "Usage:", sys.argv[0], "filename" if __name__ == "__main__": main()