Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub: #1
- Loading branch information
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| /Gemfile.lock | ||
| /data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # -*- ruby -*- | ||
|
|
||
| source "https://rubygems.org/" | ||
|
|
||
| gem "test-unit", :require => false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require "pathname" | ||
|
|
||
| base_dir_path = Pathname.new(__FILE__).dirname | ||
| lib_dir_path = base_dir_path + "lib" | ||
|
|
||
| $LOAD_PATH.unshift(lib_dir_path.to_s) | ||
|
|
||
| require "wikipedia-search/groonga-converter" | ||
|
|
||
| converter = WikipediaSearch::GroongaConverter.new(ARGF) | ||
| converter.convert($stdout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| require "json" | ||
| require "rexml/streamlistener" | ||
| require "rexml/parsers/baseparser" | ||
| require "rexml/parsers/streamparser" | ||
|
|
||
| module WikipediaSearch | ||
| class GroongaConverter | ||
| def initialize(input) | ||
| @input = input | ||
| end | ||
|
|
||
| def convert(output) | ||
| listener = Listener.new(output) | ||
| parser = REXML::Parsers::StreamParser.new(@input, listener) | ||
| parser.parse | ||
| listener.finish | ||
| end | ||
|
|
||
| class Listener | ||
| include REXML::StreamListener | ||
|
|
||
| def initialize(output) | ||
| @output = output | ||
| @text_stack = [""] | ||
| @first_page = true | ||
| @output.puts("load --table Pages") | ||
| @output.puts("[") | ||
| end | ||
|
|
||
| def finish | ||
| @output.puts unless @first_page | ||
| @output.puts("]") | ||
| end | ||
|
|
||
| def tag_start(name, attributes) | ||
| push_stacks | ||
| case name | ||
| when "page" | ||
| @title = nil | ||
| @id = nil | ||
| @text = nil | ||
| end | ||
| end | ||
|
|
||
| def tag_end(name) | ||
| case name | ||
| when "page" | ||
| if @first_page | ||
| @first_page = false | ||
| else | ||
| @output.puts(",") | ||
| end | ||
| page = { | ||
| "_key" => @id, | ||
| "title" => @title, | ||
| "text" => @text, | ||
| } | ||
| @output.print(JSON.generate(page)) | ||
| when "title" | ||
| @title = @text_stack.last | ||
| when "id" | ||
| @id ||= Integer(@text_stack.last) | ||
| when "text" | ||
| @text = @text_stack.last | ||
| end | ||
| pop_stacks | ||
| end | ||
|
|
||
| def text(data) | ||
| @text_stack.last << data | ||
| end | ||
|
|
||
| def cdata(contnet) | ||
| @text_stack.last << content | ||
| end | ||
|
|
||
| private | ||
| def push_stacks | ||
| @text_stack << "" | ||
| end | ||
|
|
||
| def pop_stacks | ||
| @text_stack.pop | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require "pathname" | ||
|
|
||
| require "bundler/setup" | ||
| require "test-unit" | ||
|
|
||
| base_dir_path = Pathname.new(__FILE__).dirname.parent | ||
| lib_dir_path = base_dir_path + "lib" | ||
| test_dir_path = base_dir_path + "test" | ||
|
|
||
| $LOAD_PATH.unshift(lib_dir_path.to_s) | ||
|
|
||
| exit(Test::Unit::AutoRunner.run(true, test_dir_path.to_s)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| require "stringio" | ||
| require "wikipedia-search/groonga-converter" | ||
|
|
||
| class TestGroongaConverter < Test::Unit::TestCase | ||
| def convert(xml) | ||
| input = StringIO.new(xml) | ||
| output = StringIO.new | ||
| converter = WikipediaSearch::GroongaConverter.new(input) | ||
| converter.convert(output) | ||
| output.string | ||
| end | ||
|
|
||
| def test_empty | ||
| xml = <<-XML | ||
| <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.8/"> | ||
| </mediawiki> | ||
| XML | ||
| assert_equal(<<-GROONGA, convert(xml)) | ||
| load --table Pages | ||
| [ | ||
| ] | ||
| GROONGA | ||
| end | ||
|
|
||
| def test_one | ||
| xml = <<-XML | ||
| <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.8/"> | ||
| <page> | ||
| <title>Title</title> | ||
| <id>1</id> | ||
| <revision> | ||
| <id>1001</id> | ||
| <text>Text1 & Text2</text> | ||
| </revision> | ||
| </page> | ||
| </mediawiki> | ||
| XML | ||
| assert_equal(<<-GROONGA, convert(xml)) | ||
| load --table Pages | ||
| [ | ||
| {"_key":1,"title":"Title","text":"Text1 & Text2"} | ||
| ] | ||
| GROONGA | ||
| end | ||
| end |