copath_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in copath_parser.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Utkarsh Kukreti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # CopathParser
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'copath_parser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install copath_parser
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ # If you want to make this the default task
8
+ task :default => :spec
9
+
10
+ task :pry do
11
+ system "pry -Ilib -r./lib/copath_parser -e 'include CopathParser'"
12
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.expand_path('../lib', __FILE__))
4
+
5
+ require 'copath_parser'
6
+
7
+ CopathParser::CLI.new(ARGV)
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'copath_parser/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "copath_parser"
8
+ gem.version = CopathParser::VERSION
9
+ gem.authors = ["Utkarsh Kukreti"]
10
+ gem.email = ["utkarshkukreti@gmail.com"]
11
+ gem.description = %q{Copath Parser}
12
+ gem.summary = %q{Copath Parser}
13
+ gem.homepage = "https://github.com/utkarshkukreti/copath_parser"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'parslet'
21
+ gem.add_dependency 'docopt'
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'pry'
24
+ end
@@ -0,0 +1,9 @@
1
+ require "pry" rescue nil
2
+
3
+ require "csv"
4
+ require "docopt"
5
+ require "parslet"
6
+
7
+ require "copath_parser/cli"
8
+ require "copath_parser/parsers/prostrate"
9
+ require "copath_parser/version"
@@ -0,0 +1,41 @@
1
+ module CopathParser
2
+ class CLI
3
+ Doc = <<DOC
4
+ Copath Parser
5
+
6
+ Usage:
7
+ copath_parser <type> <input-file> <output-file>
8
+ copath_parser -v | --version
9
+
10
+ Options:
11
+ <type> Type of data, e.g. "prostrate"
12
+ DOC
13
+
14
+ def initialize(argv)
15
+ require "pp"
16
+ begin
17
+ options = Docopt::docopt(Doc)
18
+ puts "You passed in:"; pp options
19
+ if options["-v"] || options["--version"]
20
+ puts "Copath Parser #{CopathParser::VERSION}"
21
+ exit 0
22
+ end
23
+
24
+ type = options["<type>"]
25
+ case type
26
+ when /prostrate/i
27
+ parser = Parsers::Prostrate
28
+ else
29
+ raise "Invalid type."
30
+ end
31
+
32
+ puts "Parsing..."
33
+ csv = parser.parse_to_csv(File.read(options["<input-file>"]))
34
+ puts "Parsed!"
35
+ puts "Writing csv to #{options["<output-file>"]}..."
36
+ File.write(options["<output-file>"], csv)
37
+ puts "Done!"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,69 @@
1
+ module CopathParser
2
+ module Parsers
3
+ class Prostrate < Parslet::Parser
4
+ def self.parse_records(records)
5
+ ret = []
6
+ records.split(/\n\s*\n/).each do |record|
7
+ parsed = new.parse record
8
+ date = parsed[0][:date]
9
+ case_number = parsed[0][:case_number]
10
+ fmp = parsed[0][:fmp]
11
+ i = 1
12
+ parsed[1..-1].each do |addition|
13
+ addition = addition[:addition]
14
+ primary = addition[:first_number]
15
+ secondary = addition[:second_number]
16
+ sum = addition[:sum]
17
+ block = i
18
+ ret << [date, case_number, block, fmp, primary, secondary, sum].map(&:to_s).map(&:strip)
19
+ i += 1
20
+ end
21
+ end
22
+ ret
23
+ end
24
+
25
+ def self.parse_to_csv(records)
26
+ CSV.generate do |csv|
27
+ parse_records(records).each do |record|
28
+ csv << record
29
+ end
30
+ end
31
+ end
32
+
33
+ root(:record)
34
+
35
+ # Helpers
36
+ rule(:sp) { match('\s') }
37
+ rule(:sp?) { sp.repeat }
38
+ rule(:word) { (sp.absent? >> any).repeat(1) >> sp? }
39
+ rule(:number) { match['0-9'].repeat(1) >> sp? }
40
+ rule(:eq) { str('=') >> sp? }
41
+ rule(:plus) { str('+') >> sp? }
42
+ rule(:f) { number.as(:first_number) }
43
+ rule(:s) { number.as(:second_number) }
44
+ rule(:sum) { number.as(:sum) }
45
+ rule(:minus) { str('-') >> sp? }
46
+ rule(:ob) { str('[') >> sp? }
47
+ rule(:cb) { str(']') >> sp? }
48
+ rule(:op) { str('(') >> sp? }
49
+ rule(:cp) { str(')') >> sp? }
50
+
51
+ rule(:addition) do
52
+ f >> plus >> s >> eq >> sum |
53
+ sum >> eq >> f >> plus >> s |
54
+ f >> plus >> s >> minus >> sum |
55
+ sum >> ob >> f >> plus >> s >> cb |
56
+ f >> plus >> s >> (sum.absent? >> any).repeat >> sum |
57
+ sum >> op >> f >> plus >> s >> cp
58
+ end
59
+
60
+ rule(:record) do
61
+ word.as(:date) >>
62
+ word.as(:case_number) >>
63
+ word.as(:fmp) >>
64
+ ((addition.absent? >> any).repeat >> addition.as(:addition)).repeat >>
65
+ any.repeat
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module CopathParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ 01/01/01 S01-12345 20/111-22-1001 A. PROSTATE AND SEMINAL VESICLES, PROSTATECTOMY:
2
+ TOTAL GLEASON SCORE: GLEASON 5+4=9
3
+
4
+ 01/02/02 S02-1234 20/111-22-1002 PROSTATE AND SEMINAL VESICLES, PROSTATECTOMY:
5
+ GLEASON SCORE: 3 + 3 = 6 WITH TERTIARY PATTERN OF 5.
6
+
7
+ 05/02/03 S03-1234 31/111-22-1003 PROSTATECTOMY: ADENOCARCINOMA, GLEASON GRADE 4+3=7.
8
+ GLEASON SCORE: 7=4+3.
9
+
10
+ 07/17/04 S04-123 30/111-22-1004 C) RADICAL PROSTATECTOMY: ADENOCARCINOMA.
11
+ GLEASON SCORE: 3+4-7.
12
+
13
+ 01/28/05 S05-1234 20/111-22-1005 PROSTATECTOMY:
14
+ PROSTATIC ADENOCARCINOMA, GLEASON SCORE 7 [3+4].
15
+
16
+ 07/18/06 S06-10686 20/111-22-1006 A. PROSTATE AND SEMINAL VESICLES, PROSTATECTOMY:
17
+ TOTAL GLEASON SCORE: GLEASON 4+3 WITH TERTIARY GLEASON PATTERN 5
18
+
19
+ 02/22/07 S07-2749 20/111-22-1007 A. PROSTATE AND SEMINAL VESICLES, PROSTATECTOMY:
20
+ HISTOLOGIC PATTERN: GLEASON SCORE 6 (3+3)
21
+
@@ -0,0 +1,8 @@
1
+ 01/01/01,S01-12345,1,20/111-22-1001,5,4,9
2
+ 01/02/02,S02-1234,1,20/111-22-1002,3,3,6
3
+ 05/02/03,S03-1234,1,31/111-22-1003,4,3,7
4
+ 05/02/03,S03-1234,2,31/111-22-1003,4,3,7
5
+ 07/17/04,S04-123,1,30/111-22-1004,3,4,7
6
+ 01/28/05,S05-1234,1,20/111-22-1005,3,4,7
7
+ 07/18/06,S06-10686,1,20/111-22-1006,4,3,5
8
+ 02/22/07,S07-2749,1,20/111-22-1007,3,3,6
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe CopathParser::Parsers::Prostrate do
4
+ it "should parse input-1.txt" do
5
+ input = File.read("spec/data/prostrate-input-1.txt")
6
+ output = File.read("spec/data/prostrate-output-1.csv")
7
+
8
+ csv = CopathParser::Parsers::Prostrate.parse_to_csv(input)
9
+ csv.should eq(output)
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'copath_parser'
3
+
4
+ RSpec.configure do |config|
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: copath_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Utkarsh Kukreti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: parslet
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: docopt
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Copath Parser
79
+ email:
80
+ - utkarshkukreti@gmail.com
81
+ executables:
82
+ - copath-parser
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/copath-parser
92
+ - copath_parser.gemspec
93
+ - lib/copath_parser.rb
94
+ - lib/copath_parser/cli.rb
95
+ - lib/copath_parser/parsers/prostrate.rb
96
+ - lib/copath_parser/version.rb
97
+ - spec/data/prostrate-input-1.txt
98
+ - spec/data/prostrate-output-1.csv
99
+ - spec/parsers/prostrate_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/utkarshkukreti/copath_parser
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Copath Parser
125
+ test_files:
126
+ - spec/data/prostrate-input-1.txt
127
+ - spec/data/prostrate-output-1.csv
128
+ - spec/parsers/prostrate_spec.rb
129
+ - spec/spec_helper.rb
130
+ has_rdoc: