├── config.ru ├── start_up.rb ├── Gemfile ├── Jemfile ├── Procfile ├── .rvmrc ├── script ├── bundle └── jruby ├── server.rb ├── Gemfile.lock ├── Jemfile.lock ├── README.md └── pom.xml /config.ru: -------------------------------------------------------------------------------- 1 | require 'start_up' 2 | run Sinatra::Application 3 | -------------------------------------------------------------------------------- /start_up.rb: -------------------------------------------------------------------------------- 1 | # -*- encoding : utf-8 -*- 2 | 3 | # required gem files 4 | require 'sinatra' 5 | 6 | # actual source files 7 | require 'server' 8 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # -*- encoding : utf-8 -*- 2 | source 'http://rubygems.org' 3 | 4 | gem 'trinidad' 5 | gem 'sinatra' 6 | gem 'json' 7 | gem 'jieba-jruby' 8 | -------------------------------------------------------------------------------- /Jemfile: -------------------------------------------------------------------------------- 1 | # -*- encoding : utf-8 -*- 2 | source 'http://rubygems.org' 3 | 4 | gem 'trinidad' 5 | gem 'sinatra' 6 | gem 'json' 7 | gem 'jieba-jruby' 8 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: sh script/jruby -S trinidad -p $PORT 2 | console: sh script/jruby script/rails console 3 | rake: sh script/jruby -S rake 4 | jruby: sh script/jruby 5 | -------------------------------------------------------------------------------- /.rvmrc: -------------------------------------------------------------------------------- 1 | # create the gemset if it does not exist 2 | rvm_gemset_create_on_use_flag=1 3 | # switch to default ruby version when exiting directory 4 | rvm_project_rvmrc_default=1 5 | rvm jruby 6 | -------------------------------------------------------------------------------- /script/bundle: -------------------------------------------------------------------------------- 1 | ENV['GEM_HOME'] = File.join(Dir.pwd,'vendor/bundle') 2 | ENV['GEM_PATH'] = File.join(Dir.pwd,'vendor/bundle') 3 | ENV['BUNDLE_GEMFILE'] = File.join(Dir.pwd,'Jemfile') 4 | 5 | require 'rubygems' 6 | require 'bundler' 7 | require 'bundler/cli' 8 | 9 | Bundler::CLI.start 10 | -------------------------------------------------------------------------------- /server.rb: -------------------------------------------------------------------------------- 1 | # -*- encoding : utf-8 -*- 2 | 3 | require 'jieba' 4 | require 'json' 5 | 6 | post "/?" do 7 | #return 403 unless params[:key] == ENV['KEY'] 8 | text = params[:text] 9 | segmented_text = text.to_tags.to_s 10 | segmented_text.scan(/\[([^\]]*)\]/).flatten.to_json 11 | end -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | jieba-jruby (0.0.1) 5 | jruby-rack (1.1.16) 6 | json (1.8.1-java) 7 | rack (1.5.2) 8 | rack-protection (1.5.3) 9 | rack 10 | sinatra (1.4.5) 11 | rack (~> 1.4) 12 | rack-protection (~> 1.4) 13 | tilt (~> 1.3, >= 1.3.4) 14 | tilt (1.4.1) 15 | trinidad (1.4.6) 16 | jruby-rack (~> 1.1.13) 17 | trinidad_jars (>= 1.3.0, < 1.5.0) 18 | trinidad_jars (1.4.0) 19 | 20 | PLATFORMS 21 | java 22 | 23 | DEPENDENCIES 24 | jieba-jruby 25 | json 26 | sinatra 27 | trinidad 28 | -------------------------------------------------------------------------------- /Jemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | jieba-jruby (0.0.1) 5 | jruby-rack (1.1.16) 6 | json (1.8.1-java) 7 | rack (1.5.2) 8 | rack-protection (1.5.3) 9 | rack 10 | sinatra (1.4.5) 11 | rack (~> 1.4) 12 | rack-protection (~> 1.4) 13 | tilt (~> 1.3, >= 1.3.4) 14 | tilt (1.4.1) 15 | trinidad (1.4.6) 16 | jruby-rack (~> 1.1.13) 17 | trinidad_jars (>= 1.3.0, < 1.5.0) 18 | trinidad_jars (1.4.0) 19 | 20 | PLATFORMS 21 | java 22 | 23 | DEPENDENCIES 24 | jieba-jruby 25 | json 26 | sinatra 27 | trinidad 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JRuby Sinatra app (deployable to Heroku) for jieba-jruby README 2 | ======= 3 | 4 | [jieba-jruby](https://github.com/mimosa/jieba-jruby) is a Chinese NLP tool. 5 | 6 | Instructions 7 | ----------- 8 | 9 | **Check this code out from github.** 10 | `$ git clone git://github.com/diasks2/jieba-heroku.git` 11 | 12 | **Go into the project directory and run:** 13 | `$ jruby -S gem install bundler heroku` 14 | 15 | **Generate your Gemfile.lock and load dependencies:** 16 | `$ jruby -S bundle install` 17 | 18 | **Duplicate the Gemfile to Jemfile so that heroku knows its a JRuby app.** 19 | `$ cp Gemfile Jemfile` 20 | `$ cp Gemfile.lock Jemfile.lock` 21 | 22 | **Initialise GIT** 23 | `$ git init` 24 | `$ git add .` 25 | `$ git commit -m "Initial commit"` 26 | 27 | **Initialise and push to Heroku (with cedar stack for java) this will give you the application URL (can be renamed by logging in to heroku.com)** 28 | `$ heroku create --stack cedar --buildpack http://github.com/heroku/heroku-buildpack-java.git` 29 | `$ git push heroku master` 30 | 31 | **Set your KEY environmental variable on Heroku** 32 | `$ heroku config:set KEY=yourKey` 33 | 34 | Other useful commands 35 | ----------- 36 | 37 | Start local server: `$ rackup` 38 | 39 | If consuming the response in another ruby or rails app you may need to do something like the following: 40 | ```ruby 41 | response = CurbFu.post('http://your-app.herokuapp.com', { :text => your_text, :key => 'yourKey' }) 42 | response.body.force_encoding("utf-8").scan(/\"([^\""]*)\"/).flatten 43 | ``` 44 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 4.0.0 6 | br.com.tomasmuller 7 | jruby-heroku 8 | 1.0 9 | sinatra-jruby-heroku 10 | jar 11 | 12 | 13 | UTF-8 14 | 1.7.12 15 | 16 | 17 | 18 | 19 | org.jruby 20 | jruby-complete 21 | ${jruby.version} 22 | 23 | 24 | org.jruby.plugins 25 | jruby-rake-plugin 26 | ${jruby.version} 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.jruby.plugins 34 | jruby-rake-plugin 35 | ${jruby.version} 36 | 37 | 38 | install-bundler 39 | process-resources 40 | 41 | jruby 42 | 43 | 44 | -S gem install bundler --no-ri --no-rdoc --install-dir vendor/bundle 45 | 46 | 47 | 48 | bundle-install 49 | process-resources 50 | 51 | jruby 52 | 53 | 54 | script/bundle install --without development:test 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-dependency-plugin 63 | 2.3 64 | 65 | 66 | package 67 | 68 | copy 69 | 70 | 71 | 72 | 73 | org.jruby 74 | jruby-complete 75 | ${jruby.version} 76 | jruby-complete.jar 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /script/jruby: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Copyright 2001-2006 The Apache Software Foundation. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # ---------------------------------------------------------------------------- 17 | 18 | # Copyright (c) 2001-2002 The Apache Software Foundation. All rights 19 | # reserved. 20 | 21 | APPDIR=`pwd` 22 | BASEDIR=`dirname $0`/.. 23 | BASEDIR=`(cd "$BASEDIR"; pwd)` 24 | 25 | # OS specific support. $var _must_ be set to either true or false. 26 | cygwin=false; 27 | darwin=false; 28 | case "`uname`" in 29 | CYGWIN*) cygwin=true ;; 30 | Darwin*) darwin=true 31 | if [ -z "$JAVA_VERSION" ] ; then 32 | JAVA_VERSION="CurrentJDK" 33 | else 34 | echo "Using Java version: $JAVA_VERSION" 35 | fi 36 | if [ -z "$JAVA_HOME" ] ; then 37 | JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home 38 | fi 39 | ;; 40 | esac 41 | 42 | if [ "$darwin" = "false" ] && [ $cygwin = "false" ]; then 43 | APPDIR="$(dirname $(dirname $(readlink -fn "$0")))" 44 | fi 45 | 46 | if [ -z "$JAVA_HOME" ] ; then 47 | if [ -r /etc/gentoo-release ] ; then 48 | JAVA_HOME=`java-config --jre-home` 49 | fi 50 | fi 51 | 52 | # For Cygwin, ensure paths are in UNIX format before anything is touched 53 | if $cygwin ; then 54 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 55 | [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 56 | fi 57 | 58 | # If a specific java binary isn't specified search for the standard 'java' binary 59 | if [ -z "$JAVACMD" ] ; then 60 | if [ -n "$JAVA_HOME" ] ; then 61 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 62 | # IBM's JDK on AIX uses strange locations for the executables 63 | JAVACMD="$JAVA_HOME/jre/sh/java" 64 | else 65 | JAVACMD="$JAVA_HOME/bin/java" 66 | fi 67 | else 68 | JAVACMD=`which java` 69 | fi 70 | fi 71 | 72 | if [ ! -x "$JAVACMD" ] ; then 73 | echo "Error: JAVA_HOME is not defined correctly." 74 | echo " We cannot execute $JAVACMD" 75 | exit 1 76 | fi 77 | 78 | REPO="$BASEDIR"/target/dependency 79 | 80 | CLASSPATH=$CLASSPATH_PREFIX:"$BASEDIR"/etc:"$REPO"/jruby-complete.jar 81 | EXTRA_JVM_ARGUMENTS="-Djruby.native.enabled=false -Djruby.cext.enabled=false -Djruby.compat.version=1.9 -Xmx256m -Xss2048k" 82 | 83 | echo "Classpath is: $CLASSPATH" 84 | 85 | # For Cygwin, switch paths to Windows format before running java 86 | if $cygwin; then 87 | [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 88 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 89 | [ -n "$HOME" ] && HOME=`cygpath --path --windows "$HOME"` 90 | [ -n "$BASEDIR" ] && BASEDIR=`cygpath --path --windows "$BASEDIR"` 91 | [ -n "$REPO" ] && REPO=`cygpath --path --windows "$REPO"` 92 | fi 93 | 94 | export GEM_HOME="$APPDIR"/vendor/bundle 95 | export GEM_PATH=$GEM_HOME 96 | export PATH="$GEM_PATH"/bin:$PATH 97 | export BUNDLE_GEMFILE="$APPDIR"/Jemfile 98 | 99 | exec "$JAVACMD" $JAVA_OPTS \ 100 | $EXTRA_JVM_ARGUMENTS \ 101 | -classpath "$CLASSPATH" \ 102 | -Dbasedir="$BASEDIR" \ 103 | -Xmx256m \ 104 | -Xss2048k \ 105 | org.jruby.Main \ 106 | "$@" 107 | --------------------------------------------------------------------------------