Kouhei Sutou
null+****@clear*****
Thu Jun 9 15:13:29 JST 2016
Kouhei Sutou 2016-06-09 15:13:29 +0900 (Thu, 09 Jun 2016) New Revision: ede2b6f1f0eb48b808c0dfa21e211adee15880f8 https://github.com/pgroonga/pgroonga/commit/ede2b6f1f0eb48b808c0dfa21e211adee15880f8 Message: Add example to use completion Added files: examples/completion/README.md examples/completion/complete.sh examples/completion/create-insert.rb examples/completion/setup.sh Added: examples/completion/README.md (+10 -0) 100644 =================================================================== --- /dev/null +++ examples/completion/README.md 2016-06-09 15:13:29 +0900 (28fb9fe) @@ -0,0 +1,10 @@ +# Completion example + +## How to try + +Run the following command to setup data: + +```text +% ./setup.sh | psql DB +``` + Added: examples/completion/complete.sh (+17 -0) 100755 =================================================================== --- /dev/null +++ examples/completion/complete.sh 2016-06-09 15:13:29 +0900 (2aa4dc4) @@ -0,0 +1,17 @@ +#!/bin/sh + +if [ $# != 1 ]; then + echo "Usage: $0 INPUT | psql DB" + echo " e.g.: $0 nih | psql DB" + exit 1 +fi + +input=$1 + +cat <<SQL +SET enable_seqscan = no; +SELECT term, readings, english + FROM dictionary + WHERE term &^ '$1' OR + readings &^~> '$1'; +SQL Added: examples/completion/create-insert.rb (+74 -0) 100755 =================================================================== --- /dev/null +++ examples/completion/create-insert.rb 2016-06-09 15:13:29 +0900 (2247cfb) @@ -0,0 +1,74 @@ +#!/usr/bin/env ruby + +require "English" +require "nkf" +require "json" + +def escape(value) + case value + when String + escaped_value = value.gsub(/'/, "''") + "'#{escaped_value}'" + when Array + escaped_value = value.collect {|element| escape(element)} + "ARRAY[#{escaped_value.join(', ')}]" + end +end + +current_term = nil +current_readings = [] +current_english = nil + +first_value = true + +flush = lambda do + return if current_term.nil? + + if first_value + first_value = false + puts + else + puts "," + end + print <<-VALUE.chomp +(#{escape(current_term)}, + #{escape(current_readings)}, + #{escape(current_english)}) + VALUE + current_term = nil +end + +_first_line = gets # Ignore header + +print "INSERT INTO dictionary VALUES" +loop do + raw_line = gets + break if raw_line.nil? + + line = raw_line.encode("UTF-8", "EUC-JP") + term, english = line.strip.split("/", 2) + term = term.strip + if /\s*\[(.+)\]\z/ =~ term + term = $PREMATCH + reading = $1 + reading = NKF.nkf("-Ww --katakana", reading) + else + reading = NKF.nkf("-Ww --katakana", term) + end + + case current_term + when nil + current_term = term + current_readings = [reading] + current_english = english + when term + current_readings << reading + unless english == current_english + current_english << english + end + else + flush.call + end +end +flush.call +puts ";" Added: examples/completion/setup.sh (+35 -0) 100755 =================================================================== --- /dev/null +++ examples/completion/setup.sh 2016-06-09 15:13:29 +0900 (78b5d4c) @@ -0,0 +1,35 @@ +#!/bin/sh + +set -u +set -e + +base_dir="$(dirname $0)" + +edict_gz="${base_dir}/edict.gz" + +if [ ! -f "${edict_gz}" ]; then + wget -O "${edict_gz}" http://ftp.monash.edu.au/pub/nihongo/edict.gz +fi + +cat <<SQL +CREATE EXTENSION IF NOT EXISTS pgroonga; + +DROP TABLE IF EXISTS dictionary; +CREATE TABLE dictionary ( + term text, + readings text[], + english text +); + +SQL + +zcat "${edict_gz}" | ./create-insert.rb + +cat <<SQL + +CREATE INDEX pgroonga_index + ON dictionary + USING pgroonga (term pgroonga.text_term_search_ops_v2, + readings pgroonga.text_array_term_search_ops_v2, + english pgroonga.text_full_text_search_ops_v2); +SQL -------------- next part -------------- HTML����������������������������...Download