cab2xml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 746106cafaf3c1f9241df12fe24e73bb4583d8b6
4
+ data.tar.gz: b4dfe536cb85cf531092fe9053f2b563c1fa4114
5
+ SHA512:
6
+ metadata.gz: 23f643b1a499bc6b3b49c2fec603825e2eee6ebf09da77c2310d6c3c5cf1c607bce616166212337172c1f3ff4c9617a3fbafd3afc4c5d802952ca64bfd6db8a0
7
+ data.tar.gz: 2c989bd371b647056b79f1a25338097fdab688f47a9a582cc4fdc8e975c21e8bbcaaf5460c32c4f857fa53c66fe6a84852b55a637dc96052ca0f2988dbf598b5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cab2xml.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mizuho IMADA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Cab2xml
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cab2xml`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cab2xml'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cab2xml
22
+
23
+ ## Usage
24
+
25
+ $ cab2xml your.cabocha > your.xml
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/cab2xml/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/cab2xml ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cab2xml'
4
+ converter = Cab2xml::Converter.new
5
+ puts converter.parse(ARGF)
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cab2xml"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/cab2xml.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cab2xml/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cab2xml"
8
+ spec.version = Cab2xml::VERSION
9
+ spec.authors = ["Mizuho IMADA"]
10
+ spec.email = ["imadamizuho@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
14
+ end
15
+
16
+ spec.summary = %q{Cabocha to XML.}
17
+ spec.description = %q{Cabocha to XML.}
18
+ spec.homepage = ""
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ # spec.bindir = "exe"
23
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.bindir = "lib"
25
+ spec.executables = ["cab2xml"]
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "nokogiri"
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.8"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
@@ -0,0 +1,134 @@
1
+ require "nokogiri"
2
+ require "csv"
3
+
4
+ module Cab2xml
5
+ class Converter
6
+ attr_accessor :attr_mode, :token_format
7
+ def initialize
8
+ @attr_mode = :attr
9
+ @token_format = :mecab_unidic
10
+ end
11
+ def parse(file)
12
+ @xml, @sen, @senid = nil
13
+ file.set_encoding 'UTF-8'
14
+ file.each_line do |line|
15
+ line.chomp!
16
+ case line
17
+ when ''
18
+ # ignore
19
+ when /^##/
20
+ # comment line
21
+ when /^#!/
22
+ parse_extended_tag line
23
+ else
24
+ parse_cabocha_tag line
25
+ end
26
+ end
27
+ return @xml
28
+ end
29
+ def create_xml(mode)
30
+ # mode = {:corpora|:document}
31
+ @xml = Nokogiri::XML("<#{mode}/>")
32
+ @xml.encoding = 'UTF-8'
33
+ @corpora = @xml.root if mode == :corpora
34
+ @doc = @xml.root if mode == :document
35
+ end
36
+ def add_node(parent, format, data)
37
+ parent << (format % data)
38
+ @last = parent.children.last
39
+ return @last
40
+ end
41
+ def check_namespace(key)
42
+ return unless key =~ /:/
43
+ namespace, key = key.split(':', 2)
44
+ @namespaces ||= {}
45
+ return if @namespaces[namespace]
46
+ @namespaces[namespace] = true
47
+ @doc.add_namespace namespace, 'http://www.ninjal.ac.jp/corpus_center/bccwj/' + namespace
48
+ end
49
+ def parse_extended_tag(line)
50
+ null, label, *data = CSV.parse_line(line, :col_sep => "\s")
51
+ data.map!{|item| item.encode(:xml => :text)}
52
+ case label
53
+ when 'DOCID'
54
+ create_xml(:corpora) unless @xml
55
+ format = '<DOCID id=%d>%s</DOCID>'
56
+ @docid = add_node(@corpora, format, data)
57
+ when 'SENTENCETAGID'
58
+ format = '<SENTENCETAGID id=%d>%s</SENTENCETAGID>'
59
+ @sentencetagid = add_node(@corpora, format, data)
60
+ when 'DOC'
61
+ format = '<document id="%d"/>'
62
+ @doc = add_node(@corpora, format, data)
63
+ @senid = 0
64
+ when 'ATTR'
65
+ case @attr_mode
66
+ when :node
67
+ format = '<ATTR Key="%s" Value="%s"/>'
68
+ @attr = add_node(@last, format, data)
69
+ when :attr
70
+ key, value = data
71
+ check_namespace key
72
+ @last[key] = value
73
+ end
74
+ when 'SEGMENT'
75
+ format = '<SEGMENT TagName="%s" StartGPos="%s" EndGPos="%s" Comments="%s"/>'
76
+ @seg = add_node(@doc, format, data)
77
+ when 'SEGMENT_S'
78
+ format = '<SEGMENT_S TagName="%s" StartLPos="%s" EndLPos="%s" Comments="%s"/>'
79
+ @seg = add_node(@sen, format, data)
80
+ when 'LINK'
81
+ format = '<LINK TagName="%s" FromSegNo="%s" EndSegNo="%s" Comments="%s"/>'
82
+ @link = add_node(@doc, format, data)
83
+ when 'LINK_S'
84
+ format = '<LINK_S TagName="%s" FromSegSNo="%s" EndSegSNo="%s" Comments="%s"/>'
85
+ @link = add_node(@sen, format, data)
86
+ when 'GROUP'
87
+ format = '<GROUP TagName="%s" SegNo="%s" Comments="%s"/>'
88
+ data = [data[0], data[1..-2].join(','), data[-1]]
89
+ @group = add_node(@doc, format, data)
90
+ when 'GROUP_S'
91
+ format = '<GROUP_S TagName="%s" SegSNo="%s" Comments="%s"/>'
92
+ data = [data[0], data[1..-2].join(','), data[-1]]
93
+ @group = add_node(@sen, format, data)
94
+ end
95
+ end
96
+ def parse_cabocha_tag(line)
97
+ case line
98
+ when /^\*/
99
+ create_xml(:document) unless @xml
100
+ unless @sen
101
+ @sen = add_node(@doc, '<sentence id="%d"/>', @senid ||= 0)
102
+ @senid += 1
103
+ @tokid = 0
104
+ end
105
+ null, id, dep, headfunc, score = line.split(' ')
106
+ link, rel = dep[0..-2], dep[-1]
107
+ head, func = headfunc.split('/')
108
+ data = [id, link, rel, head, func, score]
109
+ format = '<chunk id="%d" link="%d" rel="%s" head="%d" func="%d" score="%s"/>'
110
+ @chunk = add_node(@sen, format, data)
111
+ when 'EOS'
112
+ @sen = nil
113
+ else
114
+ case token_format
115
+ when :chasen
116
+ data = line.split(/\s/)
117
+ data = [@tokid, *data[1..5], data[0]]
118
+ format = '<tok id="%d" read="%s" base="%s" pos="%s" cype="%s" cform="%s">%s</tok>'
119
+ @tok = add_node(@chunk, format, data)
120
+ @tokid += 1
121
+ when :mecab_unidic
122
+ text, data = line.split(/\s/, 2)
123
+ data = data.split(',').map{|item| item == '*' ? nil : item }
124
+ pos = data[0, 4].compact.join('-')
125
+ ctype, cform, lemmaForm, lemma = data[4, 4]
126
+ data = [@tokid, pos, ctype, cform, lemmaForm, lemma, text]
127
+ format = '<tok id="%d" pos="%s" cype="%s" cform="%s" lemmaForm="%s" lemma="%s">%s</tok>'
128
+ @tok = add_node(@chunk, format, data)
129
+ @tokid += 1
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,3 @@
1
+ module Cab2xml
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cab2xml.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "cab2xml/version"
2
+ require "cab2xml/converter"
3
+
4
+ module Cab2xml
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cab2xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mizuho IMADA
8
+ autorequire:
9
+ bindir: lib
10
+ cert_chain: []
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Cabocha to XML.
56
+ email:
57
+ - imadamizuho@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/cab2xml
68
+ - bin/console
69
+ - bin/setup
70
+ - cab2xml.gemspec
71
+ - lib/cab2xml.rb
72
+ - lib/cab2xml/converter.rb
73
+ - lib/cab2xml/version.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Cabocha to XML.
99
+ test_files: []