Skip to content

Instantly share code, notes, and snippets.

@mootoh
Created September 16, 2014 03:40
Show Gist options
  • Select an option

  • Save mootoh/58a9f5dd9f9324ac6932 to your computer and use it in GitHub Desktop.

Select an option

Save mootoh/58a9f5dd9f9324ac6932 to your computer and use it in GitHub Desktop.
Collect nouns from HTTP POST data and get back as JSON array
require 'natto'
require 'webrick'
require 'json'
def to_nouns(text)
nm = Natto::MeCab.new
results = []
nm.parse(text) do |n|
type = n.feature.split(/,/).first
next unless type == '名詞'
results.push n.surface
end
results
end
server = WEBrick::HTTPServer.new :Port => 1978
server.mount_proc '/' do |req, res|
nouns = to_nouns req.body
res.body = nouns.to_json
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment