æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.10ï¼»CHAPTER7 å¦ç¿ã®ã¹ã¹ã¡ï¼½â¢
è¨äºã®æ¦è¦
ãæããããã°ã©ã ãã®ï¼»CHAPTER7 å¦ç¿ã®ã¹ã¹ã¡ï¼½ãåèã«ãå½¢æ ç´ è§£æãå©ç¨ãã¦å¿çæã®èªå½ãå¢ãããã£ããããããä½æãã¾ãã
ç®æ¬¡
- è¨äºã®æ¦è¦
- ç®æ¬¡
- éçºç°å¢
- ããã£ã®è¾æ¸ã®ç¨®é¡ãå¢ãã
- åèæ¸ç±
- åèè¨äº
- é¢é£è¨äº
éçºç°å¢
- OSX 10.11.2 El Capitan
- ããã¹ãã¨ãã£ã¿ï¼ MacVim
- ã¿ã¼ããã«ã¨ãã¥ã¬ã¼ã¿ï¼ Macã¿ã¼ããã«
- ã·ã§ã«ï¼ zsh
- ããã±ã¼ã¸ããã¼ã¸ã£ï¼ Homebrew
- ãã©ã¦ã¶ï¼ Firefox - Ruby 2.0.0p645
- ãã¼ã¸ã§ã³ããã¼ã¸ã£ï¼ rbenv
- Webãã¬ã¼ã ã¯ã¼ã¯ï¼ Sinatra - MeCab 0.996
- æ¥æ¬èªè¾æ¸ï¼ IPAè¾æ¸
åèè¨äº
Rubyã®éçºç°å¢æ§ç¯ã¯ããã¡ãã®è¨äºãåèã«ããã¦ããã ãã¾ããã
ããã£ã®è¾æ¸ã®ç¨®é¡ãå¢ãã
å ¥åããä¼è©±ã®å 容ãã©ã³ãã è¾æ¸ã«è¿½å ãã¤ã¤ãå½¢æ ç´ è§£æãå©ç¨ãã¦åè©ãæ½åºããåè©ããã¼ã¯ã¼ãã«ããå¿çããã¿ã¼ã³è¾æ¸ã¸ãåè©ãé¤å¤ããæã®éª¨çµã¿ããã³ãã¬ã¼ãè¾æ¸ã¸è¿½å ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ãã£ã¬ã¯ããªã®æ§æ
~/programinlove
|- proto.rb // CUIãã£ãããããã®ã¡ã¤ã³ãã¡ã¤ã«
|- unmo.rb // ãã£ããããããªãã¸ã§ã¯ãã®ã¢ãã«
|- responder.rb // å¿çãªãã¸ã§ã¯ãã®ã¢ãã«
|- dictionary.rb // è¾æ¸ãèªã¿è¾¼ã¿ç®¡çããã¯ã©ã¹
|- mecab_natto.rb // å½¢æ
ç´ è§£æå¨MeCabã使ãããã®ã¢ã¸ã¥ã¼ã«
|- morph.rb // MeCabNattoã¢ã¸ã¥ã¼ã«ãunmo.rbã§ä½¿ãããã®ã¢ã¸ã¥ã¼ã«
|- log.txt // ã¦ã¼ã¶ã®å
¥åå±¥æ´ãä¿åãããã°ãã¡ã¤ã«
|- /dics // è¾æ¸ãã¡ã¤ã«ãæ ¼ç´ãããã£ã¬ã¯ããª
| |- random.txt // ã©ã³ãã ãªå¿çãè¿ãããã®è¾æ¸
| |- pattern.txt // ãã¿ã¼ã³ã«åã£ãå¿çãè¿ãããã®è¾æ¸
| |- template.txt // ãã³ãã¬ã¼ãã使ã£ãå¿çãè¿ãããã®è¾æ¸
|- noby.rb // Webã¢ããªã®ã¡ã¤ã³ãã¡ã¤ã«
|- /views // ãã¥ã¼ã®ãã³ãã¬ã¼ããé
ç½®ãããã£ã¬ã¯ããª
| |- index.erb // ãã£ããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã表示ãããã¥ã¼
|- /public // éçãã¡ã¤ã«ãé
ç½®ãããã£ã¬ã¯ããª
|- nobycanvas.js // ãã£ã©ã¯ã¿ã¼ã®ç»åãã¢ãã¡ã¼ã·ã§ã³ãããJavascript
|- styles.css // ãã£ããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãã¬ã¤ã¢ã¦ãããcss
|- /img // ç»åãã¡ã¤ã«ãæ ¼ç´ãããã£ã¬ã¯ããª
|- /normal // 平常æã®ç»åãæ ¼ç´ãããã£ã¬ã¯ããª
|- /blink // ç¬ãã®ç»åãæ ¼ç´ãããã£ã¬ã¯ããª
|- /lookaround // å¨å²ãè¦åãç»åãæ ¼ç´ãããã£ã¬ã¯ããª
|- /talk // åãç»åãæ ¼ç´ãããã£ã¬ã¯ããª
~ 以ä¸ã«è¡¨æ
ãã¨ã®ç»åãæ ¼ç´ãããã£ã¬ã¯ããªãé
ç½®
ã½ã¼ã¹ã³ã¼ã
morph.rb
ããmecab_natto.rb
ãå¼ã³åºããå½¢æ
ç´ è§£æã®çµæãé
åã«æ ¼ç´ãã¾ãã
ãµã³ãã«ããã°ã©ã ã¨ç°ãªãå½¢æ
ç´ è§£æã«MeCabã使ç¨ãã¦ãããããinit_analyzer
ã¡ã½ããã¨analyze
ã¡ã½ããã®ä¸èº«ãMeCabã®åºåã«åããã¦å¤æ´ãã¦ãã¾ãã
Morph
ã¢ã¸ã¥ã¼ã«ã®åæåã¯ãnoby.rb
ã®ã³ã³ãã£ã®ã¥ã¬ã¼ã·ã§ã³ã§Morph::init_analyzer
ã¡ã½ãããå¼ã³åºãã¦è¡ãªãã¾ãã
ãã®ä»ã®Rubyãã¡ã¤ã«ã¯ããµã³ãã«ããã°ã©ã ã¨åæ§ã®è¨è¿°ã§ãç¹ã«å¤æ´ã¯ããã¾ããã
morph.rb
require_relative 'mecab_natto' module Morph def init_analyzer MeCabNatto::setarg('-F%m\s%F-[0,1,2]') end def analyze(text) # å½¢æ ç´ è§£æã®åºåã表層形ã¨åè©ãè¦ç´ ã«ããé åã«ã㦠# 解æçµæã®é åã«æ ¼ç´ãã analysis_result = [] MeCabNatto::analyze(text).each do |part| analysis_result.push(part.feature.split(/ /)) if !part.is_eos? end return analysis_result end def keyword?(part) return /åè©-(ä¸è¬|åºæåè©|ãµå¤æ¥ç¶|形容åè©èªå¹¹)/ =~ part end module_function :init_analyzer, :analyze, :keyword? end
noby.rb
require 'sinatra' require 'sinatra/reloader' require_relative 'unmo' # ä¼è©±ãã°ãæ ¼ç´ããé å log_area = [] # èµ·åæã«ãªãã·ã§ã³ãè¨å®ãã # ç¶æ ãä¿æãããªãã¸ã§ã¯ãã®çæã«ä½¿ã configure do # ããã£çæ set :noby, Unmo.new('noby') # å½¢æ ç´ è§£æã®ããã«Morphã¢ã¸ã¥ã¼ã«ãåæå set :morph, Morph::init_analyzer end # ãã«ãã¼ã¡ã½ãããå®ç¾©ãã # ã«ã¼ãã£ã³ã°ã¡ã½ããã®ä¸ã§ä½¿ã helpers do def noby # ããã£ã¸ã¢ã¯ã»ã¹ noby = settings.noby end def prompt(resp_opt) # å¿çã表示ããéã®ããã³ãããä½æ resp_opt ? "#{noby.name}ï¼#{noby.responder_name}" : "#{noby.name}" end def change_looks # ææ å¤ã§è¡¨æ ãå¤åããã case noby.mood when -5..5 then 'talk' when -10..-5 then 'angry_talk' when -15..-10 then 'more_angry_talk' when 5..10 then 'happy_talk' when 10..15 then 'more_happy_talk' end end def save_log(log) # ãã¡ã¤ã«ã«ãã°ãæ¸ãè¾¼ã noby.save open('log.txt', 'a') do |f| f.puts(log) end end end # URL'/'ã«ã¢ã¯ã»ã¹ get '/' do # æ¥ææ å ±ããã°ã«æ ¼ç´ log = "\nUnmo System : #{noby.name} Log -- #{Time.now}" save_log(log) # ä¼è©±ãã°ãåæåãã¦nobyfomã表示 log_area = [] erb :index end # URL'/'ã«POSTã¡ã½ããã§ã¢ã¯ã»ã¹ post '/' do # ã¦ã¼ã¶ã®å ¥åãåå¾ talk_text = params['inputarea'] # Responderã表示ãããã§ãã¯ããã¯ã¹ã®ç¶æ ãåå¾ # ãã§ãã¯ããã¦ãå ´åã¯ç¶æ ãç¶æ resp_opt = params['respoption'] @check = "checked" if resp_opt # ã¦ã¼ã¶ã®å ¥åãããã°å¿çãã¦ä¼è©±ãã°ã«è¡¨ç¤º unless talk_text.empty? @responder_resp = noby.dialogue(talk_text) log_area << "> #{talk_text}<br>" log_area << "#{prompt(resp_opt)}> #{@responder_resp}<br>" @noby_state = change_looks save_log(talk_text) end @talk_log = log_area.join erb :index end
ãã®ã³ã¼ãã®ã³ãããã«ã¯ã[chapter7-4]ã®ã¿ã°ãä»ãã¦ã¾ãã
å®è¡çµæ
ã¿ã¼ããã«ã§noby.rb
ãåããããã©ã¦ã¶ã«http://localhost:4567
ãå
¥åãã¾ãã
~/programinlove
$ruby noby.rb
ä¼è©±ã«å«ã¾ãã¦ããåè©ããä¼è©±ã®å 容ã¨å ±ã«ãã¿ã¼ã³è¾æ¸ã«ä¿åãããä¼è©±ã®å 容ããåè©ãé¤å¤ããæã®éª¨çµã¿ããã³ãã¬ã¼ãè¾æ¸ã«ä¿åããã¾ãã
0##ããã 0##ãããããããããã®ãã¡ 0##ãã 0##ãããããããããã®ãã¡ 0##ç§ 0##ç§ã¯ããã°ã©ã ã®å¥³ã®åã§ã 0##ããã°ã©ã 0##ç§ã¯ããã°ã©ã ã®å¥³ã®åã§ã 0##女ã®å 0##ç§ã¯ããã°ã©ã ã®å¥³ã®åã§ã
3 %noun%ã%noun%ã%noun%ã®ãã¡ 3 %noun%ã¯%noun%ã®%noun%ã§ã
åèæ¸ç±
åèè¨äº
Sinatraã«ã¤ãã¦ã¯ããã¡ãã®è¨äºãåèã«ããã¦ããã ãã¾ããã
- Rubyの入門や書き捨てアプリを作る場合は sinatraがオススメ! - むかぁ~ どっと こむ
- SinatraとjQueryでおよそ100行で作るAjax掲示板アプリケーション - gaaamiiのブログ
- Sinatra: README (Japanese)
é¢é£è¨äº
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.0ï¼»ã¯ããã«ï¼½
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.1ï¼»CHAPTER3 ã»ãã¨ã«ç¡è½ï¼½
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.2ï¼»CHAPTER4 ããããã®GUIï¼½â
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.3ï¼»CHAPTER4 ããããã®GUIï¼½â¡
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.4ï¼»CHAPTER4 ããããã®GUIï¼½â¢
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.5ï¼»CHAPTER5 è¾æ¸ãçæã«ï¼½
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.6ï¼»CHAPTER6 ææ
ã³ã³ããã¼ã«ã®éè¡å¸«ï¼½â
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.7ï¼»CHAPTER6 ææ
ã³ã³ããã¼ã«ã®éè¡å¸«ï¼½â¡
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.8ï¼»CHAPTER7 å¦ç¿ã®ã¹ã¹ã¡ï¼½â
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.9ï¼»CHAPTER7 å¦ç¿ã®ã¹ã¹ã¡ï¼½â¡
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.11ï¼»CHAPTER8 æç« ãä½ãåºãï¼½
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.12ï¼»CHAPTER9 ããã£ããããã¯ã¼ã¯ã«ã¤ãªããï¼½
- æããããã°ã©ã ãSinatraã§Webã¢ããªã«ããPart.13ï¼»ãããã«ï¼½