source code

	# -*- coding: utf-8 -*-
	require 'rubygems'
	require 'dbi'
	require 'mecab_util.rb'
	
	# ---------------------------------------
	# --- define
	# ---------------------------------------
	DBNAME = 'DBI:mysql:koruri:localhost'
	DBLOGIN = 'yourid'
	DBPASS = 'yourpass'
	VERB = false
	
	
	# ---------------------------------------
	# --- subroutine
	# ---------------------------------------
	
	def make_template(sentence)
	  sentence.sub!(/^RT/, '')
	  sentence.sub!(/RT$/, '')
	  token = MecabParse.new(sentence)
	  num = token.size
	  line = ""
	  noun = false
	  score = 1
	  count = 0
	  for index in 0...num
	    node = token.get_hinshi(index)

	      if noun == false then
	        line << "(*)"
		count += 1
	      end
	      noun = true
	      if score > 0 then
	        word = token.get_word(index)
	        if word.size < 2 then
	          score = 0
	        else
	          score += (word.size - 2)
	        end
	      end
	    else
	      line << token.get_word(index)
	      noun = false
	    end
	  end
	  score = 0 if num < 3
	  score = 0 if score == 1
	  result = [score, count, line]
	  result
	end
	
	
	def set_template(db, text, nouncount, score, parent)
	  sql = 'SELECT id,count FROM template WHERE template=?'
	  sth = db.execute(sql, text)
	  hit = false
	  sth.each do |row|
	    hit = true
	    sql = 'UPDATE template SET count=? WHERE id=?'
	    db.do(sql, row[1]+1, row[0])
	    sql = 'UPDATE sentence SET template=? WHERE id=?'
	    db.do(sql, row[0], parent)
	  end
	  sth.finish
	
	  if hit == false then
	    sql = 'INSERT INTO template VALUE (NULL,?,?,?,1,0,NULL)'
	    db.do(sql, text, nouncount, score)
	    sql = 'SELECT id FROM template WHERE template=?'
	    sth = db.execute(sql, text)
	    tmpid = -1
	    sth.each do |row|
	      tmpid = row[0]
	    end
	    sth.finish
	    sql = 'UPDATE sentence SET template=? WHERE id=?'
	    db.do(sql, tmpid, parent)
	  end
	end
	
	# ---------------------------------------
	# --- main routine
	# ---------------------------------------
	
	# --- open DB
	db = DBI.connect(DBNAME, DBLOGIN, DBPASS)
	
	
	# --- query
	sql = 'SELECT id,text FROM sentence WHERE template=-1'
	begin
	  sth = db.execute(sql)
	  sth.each do |row|
	    temp = make_template(row[1])
	    if temp[0] > 0 then
	      puts temp[2] if VERB
	      set_template(db, temp[2], temp[1], temp[0], row[0])
	    else
	      sql = 'UPDATE sentence SET template=0 WHERE id=?'
	      db.do(sql, row[0])
	    end
	  end
	  sth.finish
	rescue => e
	  puts e
	  puts "error terminate."
	ensure
	  # --- end
	  db.disconnect
	end

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2017-11-06 (月) 01:22:22 (877d)