Merge: Added contributing guidelines and link from readme
[nit.git] / contrib / benitlux / src / correct.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Correct errors in beer names before using data from the DB
16 #
17 # Reads corrections from the file at `DB::corrections_path` which must be formatted like so:
18 #
19 # ~~~raw
20 # Wrong name -> Correct name
21 # Name with typo -> Clean name
22 # ~~~
23 module correct
24
25 import benitlux_db
26
27 redef class BenitluxDB
28 # Path to file with the corrections
29 private var corrections_path = "benitlux_corrections.txt"
30
31 # Corrections of beer name: wrong name to corrected name
32 private var corrections: Map[String, String] is lazy do
33 var map = new HashMap[String, String]
34
35 # Read from file
36 if corrections_path.file_exists then
37 var lines = corrections_path.to_path.read_lines
38 for line in lines do
39 var parts = line.split("->")
40 assert parts.length == 2 else print_error "Error: wrong format in '{corrections_path}'"
41 map[parts[0].trim] = parts[1].trim
42 end
43 end
44
45 return map
46 end
47
48 redef fun beers
49 do
50 var beers = super
51 if beers == null then return null
52
53 # Skip corrected beers
54 for beer in beers.reverse_iterator do
55 if corrections.keys.has(beer.name) then
56 beers.remove beer
57 end
58 end
59
60 return beers
61 end
62
63 redef fun days(beer)
64 do
65 var days = super(beer)
66 if beer == null or days == null then return days
67
68 # Merge days of `corrections` to `beer`
69 for from, to in corrections do
70 if to == beer.name then
71 var missing_days = super(new Beer(0, from, ""))
72 if missing_days != null then days.add_all missing_days
73 end
74 end
75
76 return days
77 end
78 end