37
38
39
40
41
42
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
|
# File 'lib/udise_school_report_reader/enrollment_data_reader.rb', line 37
def read
grade_rows = []
bg_rows = []
category_rows = {}
ALL_CATEGORIES.each do |category|
category_rows[category[:key]] = []
end
CSV.foreach(@csv_path, headers: true) do |row|
if row['page'] == '2' && (row['rect_x'].to_f - 27.0).abs < 5.0
ALL_CATEGORIES.each do |category|
if row['text'].downcase == category[:label].downcase
@category_y_coords[category[:key]] = row['rect_y'].to_f
end
end
end
end
CSV.foreach(@csv_path, headers: true) do |row|
if row['page'] == '2'
if row['text'] == "Total" && row['rect_y'].to_f == 778.0
@x_cutoff = row['rect_x'].to_f
end
if ['Pre-Pr', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'].include?(row['text'])
if row['text_y'].to_f == 780.0
grade_rows << row
end
elsif ['B', 'G'].include?(row['text'])
if row['text_y'].to_f == 768.0
bg_rows << row
end
elsif row['text'] =~ /^\d+$/
y_coord = row['rect_y'].to_f
ALL_CATEGORIES.each do |category|
if @category_y_coords[category[:key]] && (y_coord - @category_y_coords[category[:key]]).abs < 5.0
category_rows[category[:key]] << row
end
end
end
end
end
return nil if grade_rows.empty?
[grade_rows, bg_rows].each do |rows|
rows.sort_by! { |row| row['text_x'].to_f }
rows.reject! { |row| row['text_x'].to_f >= @x_cutoff }
end
category_rows.values.each do |rows|
rows.sort_by! { |row| row['text_x'].to_f }
rows.reject! { |row| row['text_x'].to_f >= @x_cutoff }
end
bg_pairs = bg_rows.each_slice(2).map do |b, g|
x_mid = (b['text_x'].to_f + g['text_x'].to_f) / 2
[x_mid, [b, g]]
end.to_h
result = {
grade_rows: grade_rows,
bg_pairs: bg_pairs
}
ALL_CATEGORIES.each do |category|
result["#{category[:key]}_numbers".to_sym] = match_numbers_to_pairs(category_rows[category[:key]], bg_pairs)
end
result
end
|