Class: ODDB::AnalysisParse::Parser

Inherits:
Object
  • Object
show all
Defined in:
ext/analysisparse/src/parser.rb

Constant Summary collapse

FOOTNOTE_PTRN =
/^\s*_*\s*(\d|\*)\s*_*\s*[^\d\*\.]+/mu
FOOTNOTE_TYPE =
:footnote
LINE_PTRN =
/^\s*([CNS]|N,\s*ex|TP)?\s*\d{4}\.\d{2,}\s*[\d\*]/u
STOPCHARS =
";.\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



15
16
17
# File 'ext/analysisparse/src/parser.rb', line 15

def initialize
	@footnotes = {}
end

Instance Attribute Details

#footnotesObject (readonly)

Returns the value of attribute footnotes.



13
14
15
# File 'ext/analysisparse/src/parser.rb', line 13

def footnotes
  @footnotes
end

#list_titleObject

Returns the value of attribute list_title.



14
15
16
# File 'ext/analysisparse/src/parser.rb', line 14

def list_title
  @list_title
end

#permissionObject

Returns the value of attribute permission.



14
15
16
# File 'ext/analysisparse/src/parser.rb', line 14

def permission
  @permission
end

#taxpoint_typeObject

Returns the value of attribute taxpoint_type.



14
15
16
# File 'ext/analysisparse/src/parser.rb', line 14

def taxpoint_type
  @taxpoint_type
end

Instance Method Details

#footnote_line(footnotes, src, start, stop) ⇒ Object



18
19
20
21
22
23
24
25
# File 'ext/analysisparse/src/parser.rb', line 18

def footnote_line(footnotes, src, start, stop)
	line = src[start..stop].strip
	fn = line.slice!(/^\d+|\*/u)
	#line.gsub!(/_/,'')
	line.strip!
	line.gsub!(/\s+/, ' ')
	footnotes.store(fn, line)
end

#footnote_typeObject



26
27
28
# File 'ext/analysisparse/src/parser.rb', line 26

def footnote_type
	self.class::FOOTNOTE_TYPE
end

#parse_footnotes(src) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'ext/analysisparse/src/parser.rb', line 29

def parse_footnotes(src)
	src.gsub!(/_*/u, '')
	src.gsub!(/~R/u, '\'')
	footnotes = {}
	stop = 0
	start = 0
	while(nextstart = src.index(FOOTNOTE_PTRN, start + 5))
		stop = nextstart - 1
		footnote_line(footnotes, src, start, stop)
		start = nextstart
	end
	footnote_line(footnotes, src, stop, -1)
	@footnotes.update(footnotes)
end

#parse_line(src) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/analysisparse/src/parser.rb', line 43

def parse_line(src)
	data = {
		:list_title			=> @list_title,
		:permission			=> @permission,
		:taxpoint_type	=> @taxpoint_type,
	}
	src << "\n"
	ast = self.class::PARSER.parse(src)
rescue Exception	=>	e
	ptrn = /(\d{4})\.(\d{2})\s*(\d{1,2})\s*(.*)/u
	data.update({
		:error			=>	e,
		:line						=>	src,
	})
	if(match = ptrn.match(src))
		data.update({
			:code					=> [match[1], match[2]].join('.'), 
			:group				=> match[1],
			:position			=> match[2],
			:taxpoints		=> match[3],
			:description	=> match[4],
		})
	end
	data
else
	desc = ''
	position = ast.position.value
	group = ast.group.value
	if(position.size > 2)
		data.store(footnote_type, position.slice!(2..-1))
	end
	data.update({
		:code					=> [group, position].join('.'),
		:group				=> group,
		:position			=> position,
		:taxpoints		=> ast.taxpoints.value.to_i,	
		:description	=> desc,
	})
	extract_text(ast.description, desc)
	if(lba = child_if_exists(ast, 'labarea'))
		data.store(:lab_areas, lba.value.strip.split(''))
	end
	if(agroup = child_if_exists(ast, 'anonymousgroup'))
		data.store(:anonymousgroup, agroup.value)
	end
	if(apos = child_if_exists(ast, 'anonymouspos'))
		data.store(:anonymouspos, apos.value)
	end
	if(lim = child_if_exists(ast, 'limitation'))
		limitation = extract_text(lim.description)
		if(ml = child_if_exists(ast, 'morelines'))
			extract_text(ml, limitation)
		end
		data.store(:limitation, limitation)
	elsif(more = child_if_exists(ast, 'morelines'))
		extract_text(more, desc)
		if(lim = child_if_exists(ast, 'limitation2'))
			limitation = extract_text(lim.description)
			data.store(:limitation, limitation)
		end
	elsif(lim2 = child_if_exists(ast, 'limitation2'))
		limitation = extract_text(lim2.description)
		data.store(:limitation, limitation)
	end
	if((number = child_if_exists(ast, 'taxnumber')) \
			&& (note = child_if_exists(ast, 'taxnote')))
		taxnote = extract_text(note.description)
		taxnumber = number.value[/\d+/u]
		data.store(:taxnumber, taxnumber)
		data.store(:taxnote, taxnote)
	end
	if(revision = child_if_exists(ast, 'revision'))
		data.store(:analysis_revision, revision.value)
	end
	[:finding, footnote_type].each { |key|
		if(node = child_if_exists(ast, key.to_s))
			data.store(key, node.value)
		end
	}
	if(child_if_exists(ast, 'anonymous'))
		data.store(:anonymous, true)
	end
	data
end

#parse_page(page, pagenum) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'ext/analysisparse/src/parser.rb', line 127

def parse_page(page, pagenum)
	stop = 0
	new_data = []
	footnotes = {}
	line_ptrn = self.class::LINE_PTRN
	if(start = page.index(line_ptrn))
		while(nextstart = page.index(line_ptrn, start + $~.to_s.length))
			stop = nextstart - 1
			line = page[start..stop]
			new_data.push(parse_line(line))
			start = nextstart
		end
		pagenum_pos = page.rindex(pagenum.to_s).to_i - 1
		if(stop = page.index(FOOTNOTE_PTRN, start + $~.to_s.length))
			parse_footnotes(page[stop..pagenum_pos])
		else
			stop = pagenum_pos
		end
		stop -= 1
		line = page[start..stop]
		new_data.push(parse_line(line))
		update_footnotes(new_data, @footnotes)
	end
	new_data
end

#update_footnotes(new_data, footnotes) ⇒ Object



152
153
154
155
156
157
158
159
# File 'ext/analysisparse/src/parser.rb', line 152

def update_footnotes(new_data, footnotes)
	new_data.each { |data|
		if(fn = footnotes[data[:footnote]])
			data.store(:footnote, fn)
		end
	}
	new_data
end