Sha256: f1a151cab780d5fd2880ad979d18d3052b4c8c3bfbe5262011ffd43b90644b72

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# -*- coding: utf-8 -*-

require 'sqlite3'
require 'open-uri'

module Polaris

  module Nlp
    module_function

    @home = Dir.home

    # init method is called in first setup.
    def init

      unless Dir.exists?("#{@home}/.polaris")
        Dir.mkdir("#{@home}/.polaris")
      end

      puts "Download data..."
      ja_text = open('http://www.lr.pi.titech.ac.jp/%7Etakamura/pubs/pn_ja.dic').read.encode('utf-8','sjis')
      en_text = open('http://www.lr.pi.titech.ac.jp/%7Etakamura/pubs/pn_en.dic').read.encode('utf-8','sjis')
      puts "Done"

      if File.exists?("#{@home}/.polaris/features.sqlite3")
        FileUtils.rm("#{@home}/.polaris/features.sqlite3")
      end

      db = SQLite3::Database.new("#{@home}/.polaris/features.sqlite3")
      db.execute("CREATE TABLE models (word TEXT ,value REAL);")

      puts "Seed Japanese data..."
      ja_text.split("\n").each do |line|
        item = line.split(':')
        word = item[0]
        value = item[3]
        insert_value(db,word,value)
      end
      puts "Done"

      puts "Seed English data..."
      en_text.split("\n").each do |line|
        item = line.split(':')
        word = item[0]
        value = item[2]
        insert_value(db,word,value)
      end
      puts "Done"

    end

    def insert_value(db,word,value)
      return db.execute("INSERT INTO models (word, value) VALUES(?, ?);", word, value)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polaris-nlp-0.2.0 lib/polaris/nlp/init.rb