sdltb_importer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b27d5a6d044ecc1f59c52b849c9f27b9cb93d596
4
+ data.tar.gz: a26923c56044c4ba3dfd67f9ec20d0097cd6bfe3
5
+ SHA512:
6
+ metadata.gz: 41b89552cf7c38a70d9821bf5e10c6b920cf72808e37d2b771c2351cfdb6f9cefe30c76ad669a1884768b5a2b002c467b42e332e520d2f0eaad2bbff0a5352a6
7
+ data.tar.gz: e06e11a07ce21164255a61bd1efcb4a3173d3277c4db2eda8b553cd8e41ae269f0280ba8fd524dd07a8ab64ba4dca60070920ceeeb4ca63df9379cb715c99090
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/sample_files
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sdltb_importer.gemspec
4
+ gemspec
@@ -0,0 +1,71 @@
1
+ # SDLTB Importer
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/sdltb_importer.svg)](https://badge.fury.io/rb/sdltb_importer) [![Build Status](https://travis-ci.org/diasks2/sdltb_importer.png)](https://travis-ci.org/diasks2/sdltb_importer) [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/diasks2/sdltb_importer/blob/master/LICENSE.txt)
4
+
5
+ This gem handles the importing and parsing of .sdltb terminology files.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ **Ruby**
12
+ ```
13
+ gem install sdltb_importer
14
+ ```
15
+
16
+ **Ruby on Rails**
17
+ Add this line to your application’s Gemfile:
18
+ ```ruby
19
+ gem 'sdltb_importer'
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ # Get the high level stats of a .sdltb file
26
+ # Including the encoding is optional. If not included the gem will attempt to detect the encoding.
27
+ file_path = File.expand_path('../sample_files/sample.tbx')
28
+ sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
29
+ sdltb.stats
30
+ # => {:tc_count=>1, :term_count=>3, :language_pairs=>[["en", "fr"], ["en", "es"]]}
31
+
32
+ # Extract the segments of a .sdltb file
33
+ # Result: [term concepts, terms]
34
+ # term concepts = [tu_id, definition]
35
+ # terms = [tu_id, language, part_of_speech, term]
36
+
37
+ sdltb.import
38
+ # => [[["6234-1457917153-1"], "the earth, together with all of its countries, peoples, and natural features.""], [["6234-1457917153-1", "en", "noun", world"], ["6234-1457917153-1", "fr", "noun", "monde"], ["6234-1457917153-1", "es", "noun", "mundo"]]]
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/diasks2/sdltb_importer/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
48
+
49
+ ## License
50
+
51
+ The MIT License (MIT)
52
+
53
+ Copyright (c) 2016 Kevin S. Dias
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining a copy
56
+ of this software and associated documentation files (the "Software"), to deal
57
+ in the Software without restriction, including without limitation the rights
58
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
59
+ copies of the Software, and to permit persons to whom the Software is
60
+ furnished to do so, subject to the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be included in
63
+ all copies or substantial portions of the Software.
64
+
65
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
68
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
69
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
70
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
71
+ THE SOFTWARE.
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,74 @@
1
+ require 'sdltb_importer/version'
2
+ require 'mdb'
3
+ require 'open-uri'
4
+ require 'pretty_strings'
5
+
6
+ module SdltbImporter
7
+ class Sdltb
8
+ LANGUAGE_GROUP_REGEX = /<lG[^>]*>(.*?)<\/lG>/m
9
+ TERM_REGEX = /<t>(.*?)<\/t>/m
10
+ LANGUAGE_REGEX = /(?<=[^cn]lang=\S)\S{2,5}(?=")/
11
+ DEFINITION_REGEX = /<d type="Definition">(.*?)<\/d>/m
12
+
13
+ attr_reader :file_path
14
+ def initialize(file_path:)
15
+ @file_path = file_path
16
+ @doc = {
17
+ source_language: "",
18
+ target_language: "",
19
+ tc: { id: "", counter: 0, vals: [], creation_date: "", definition: "" },
20
+ term: { lang: "", counter: 0, vals: [], role: "" },
21
+ language_pairs: []
22
+ }
23
+ end
24
+
25
+ def stats
26
+ imported_data
27
+ { tc_count: @doc[:tc][:counter], term_count: @doc[:term][:counter], language_pairs: @doc[:language_pairs].uniq }
28
+ end
29
+
30
+ def import
31
+ imported_data
32
+ [@doc[:tc][:vals], @doc[:term][:vals]]
33
+ end
34
+
35
+ private
36
+
37
+ def imported_data
38
+ @imported_data ||= import_data
39
+ end
40
+
41
+ def import_data
42
+ db = Mdb.open(open(file_path).path)
43
+ db[:mtConcepts].each_with_index do |record, index|
44
+ @doc[:tc][:definition] = PrettyStrings::Cleaner.new(record[:text].scan(DEFINITION_REGEX).flatten[0]).pretty.gsub("\\","&#92;").gsub("'",%q(\\\'))
45
+ generate_unique_id
46
+ @doc[:term][:counter] += record[:text].scan(TERM_REGEX).length
47
+ language_groups = record[:text].scan(LANGUAGE_GROUP_REGEX).flatten
48
+ language_groups.each_with_index do |lg, i|
49
+ @doc[:term][:lang] = lg.scan(LANGUAGE_REGEX).flatten[0]
50
+ if i.eql?(0)
51
+ @doc[:source_language] = @doc[:term][:lang]
52
+ else
53
+ @doc[:language_pairs] << [@doc[:source_language], @doc[:term][:lang]]
54
+ end
55
+ lg.scan(TERM_REGEX).flatten.each do |term|
56
+ write_term(term)
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def write_term(text)
63
+ return if text.nil? || text.empty?
64
+ text = PrettyStrings::Cleaner.new(text).pretty.gsub("\\","&#92;").gsub("'",%q(\\\'))
65
+ @doc[:term][:vals] << [@doc[:tc][:id], @doc[:term][:lang], nil, text]
66
+ end
67
+
68
+ def generate_unique_id
69
+ @doc[:tc][:id] = [(1..4).map{rand(10)}.join(''), Time.now.to_i, @doc[:tc][:counter] += 1 ].join("-")
70
+ @doc[:tc][:vals] << [@doc[:tc][:id], @doc[:tc][:definition]]
71
+ @doc[:tc][:definition] = ''
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module SdltbImporter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sdltb_importer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sdltb_importer"
8
+ spec.version = SdltbImporter::VERSION
9
+ spec.authors = ["Kevin S. Dias"]
10
+ spec.email = ["diasks2@gmail.com"]
11
+
12
+ spec.summary = %q{SDLTB file importer}
13
+ spec.description = %q{Import the content of a .sdltb terminology file}
14
+ spec.homepage = "https://github.com/diasks2/sdltb_importer"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_runtime_dependency "pretty_strings", "~> 0.7.0"
25
+ spec.add_runtime_dependency "mdb"
26
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe SdltbImporter do
4
+ it 'has a version number' do
5
+ expect(SdltbImporter::VERSION).not_to be nil
6
+ end
7
+
8
+ # describe '#stats' do
9
+ # it 'reports the stats of a .sdltb file 1' do
10
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_1.sdltb')
11
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
12
+ # expect(sdltb.stats).to eq({:tc_count=>144, :term_count=>300, :language_pairs=>[["DE", "EN"]]})
13
+ # end
14
+
15
+ # it 'reports the stats of a .sdltb file 2' do
16
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_2.sdltb')
17
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
18
+ # expect(sdltb.stats).to eq({:tc_count=>10, :term_count=>24, :language_pairs=>[["DE", "NL"]]})
19
+ # end
20
+
21
+ # it 'reports the stats of a .sdltb file 3' do
22
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_3.sdltb')
23
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
24
+ # expect(sdltb.stats).to eq({:tc_count=>904, :term_count=>9571, :language_pairs=>[["DE", "EN-GB"], ["DE", "PL"], ["DE", "HU"], ["DE", "CS"], ["DE", "SL"], ["DE", "TR"], ["DE", "PT"], ["DE", "IT"], ["DE", "DA"], ["DE", "FR"]]})
25
+ # end
26
+ # end
27
+
28
+ # describe '#import' do
29
+ # it 'imports a .sdltb file 1' do
30
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_1.sdltb')
31
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
32
+ # expect(sdltb.import[0].length).to eq(144)
33
+ # end
34
+
35
+ # it 'imports a .sdltb file 1' do
36
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_1.sdltb')
37
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
38
+ # expect(sdltb.import[1].length).to eq(300)
39
+ # end
40
+
41
+ # it 'imports a .sdltb file 1' do
42
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_1.sdltb')
43
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
44
+ # expect(sdltb.import[0][77][1]).to eq("Eine Vordermarke oder ein Vorderanschlag ist eine mechanische Baugruppe, die einen in die Druckmaschine einlaufenden Papierbogen exakt nach vorne in Laufrichtung ausrichtet. Die vordere Bogenkante fährt dabei gegen einen Anschlag und neigt dazu, unkontrolliert leicht zurückzuprallen. Um diesen Effekt zu kompensieren und den Bogen in eine genaue Ausgangslage zu bringen, wird er durch Saug- oder Blasdüsen an die stillstehende Vordermarke gedrückt. Eine weitere Lösung des Problems besteht darin, eine bewegliche Vordermarke geringfügig gegen den Bogen zu schwenken, nachdem er bereits angeschlagen hat. Aus diesem Grund wird zwischen still stehenden und bewegten Vordermarken unterschieden. Vordermarken entstanden mit der Entwicklung von Zylinderpressen Anfang des 19. Jahrhunderts. Nach der Fogra-Norm wird bei der Ausrichtung des Druckbogens eine Genauigkeit von 0,015 mm gefordert. Das ist notwendig, um den Passer bei folgenden weiteren Durchgängen und der Weiterverarbeitung zu gewährleisten.")
45
+ # end
46
+
47
+ # it 'imports a .sdltb file 2' do
48
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_2.sdltb')
49
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
50
+ # expect(sdltb.import[0].length).to eq(10)
51
+ # end
52
+
53
+ # it 'imports a .sdltb file 2' do
54
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_2.sdltb')
55
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
56
+ # expect(sdltb.import[0][1][0]).to eq(sdltb.import[1][3][0])
57
+ # end
58
+
59
+ # it 'imports a .sdltb file 2' do
60
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_2.sdltb')
61
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
62
+ # expect(sdltb.import[1].length).to eq(24)
63
+ # end
64
+
65
+ # it 'imports a .sdltb file 3' do
66
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_3.sdltb')
67
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
68
+ # expect(sdltb.import[0].length).to eq(904)
69
+ # end
70
+
71
+ # it 'imports a .sdltb file 3' do
72
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_3.sdltb')
73
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
74
+ # expect(sdltb.import[1].length).to eq(9571)
75
+ # end
76
+
77
+ # it 'imports a .sdltb file 3' do
78
+ # file_path = File.expand_path('../sdltb_importer/spec/sample_files/sample_3.sdltb')
79
+ # sdltb = SdltbImporter::Sdltb.new(file_path: file_path)
80
+ # expect(sdltb.import[1][5][1]).to eq("SL")
81
+ # end
82
+ # end
83
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'sdltb_importer'
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sdltb_importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin S. Dias
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pretty_strings
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: mdb
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Import the content of a .sdltb terminology file
84
+ email:
85
+ - diasks2@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - lib/sdltb_importer.rb
97
+ - lib/sdltb_importer/version.rb
98
+ - sdltb_importer.gemspec
99
+ - spec/sdltb_importer_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/diasks2/sdltb_importer
102
+ licenses: []
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: SDLTB file importer
124
+ test_files:
125
+ - spec/sdltb_importer_spec.rb
126
+ - spec/spec_helper.rb