Welcome to mirror list, hosted at ThFree Co, Russian Federation.

check_consistency.rb « category_consistency « ruby « tools - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6558cdd61e9a28c13c8d364f637128564914e39f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
#!/usr/bin/env ruby

require_relative './omim_parsers'

ROOT = File.expand_path(File.dirname(__FILE__))
OMIM_ROOT = File.join(ROOT, '..', '..', '..')
CPP_CATEGORIES_FILENAME = File.join(OMIM_ROOT, 'search', 'displayed_categories.cpp')
CATEGORIES_FILENAME = File.join(OMIM_ROOT, 'data', 'categories.txt')
STRINGS_FILENAME = File.join(OMIM_ROOT, 'strings.txt')
CATEGORIES_MATCHER = /m_keys = \{(.*)\};/m

def load_categories_from_cpp(filename)
  raw_categories = File.read(CPP_CATEGORIES_FILENAME)
  match = CATEGORIES_MATCHER.match(raw_categories)
  if match
    cpp_categories = match[1].split(/,\s+/)
    # Delete quotes
    cpp_categories.map { |cat| cat.gsub!(/^"|"$/, '') }
    cpp_categories
  end
end

def compare_categories(string_cats, search_cats)
  inconsistent_strings = {}

  string_cats.each do |category_name, category|
    if !search_cats.include? category_name
      puts "Category '#{category_name}' not found in categories.txt"
      next
    end
    category.each do |lang, translation|
      if search_cats[category_name].include? lang
        if !search_cats[category_name][lang].include? translation
          not_found_cats_list = search_cats[category_name][lang]
          (inconsistent_strings[category_name] ||= {})[lang] = [translation, not_found_cats_list]
        end
      end
    end
  end

  inconsistent_strings.each do |name, languages|
    puts "\nInconsistent category \"#{name}\""
    languages.each do |lang, values|
      string_value, category_value = values
      puts "\t#{lang} : \"#{string_value}\" is not matched by #{category_value}"
    end
  end
  inconsistent_strings.empty?
end

def check_search_categories_consistent
  cpp_categories = load_categories_from_cpp(CPP_CATEGORIES_FILENAME)
  categories_txt_parser = OmimParsers::CategoriesParser.new cpp_categories
  strings_txt_parser = OmimParsers::StringsParser.new cpp_categories

  search_categories = categories_txt_parser.parse_file(CATEGORIES_FILENAME)
  string_categories = strings_txt_parser.parse_file(STRINGS_FILENAME)

  compare_categories(string_categories, search_categories) ? 0 : 1
end


if __FILE__ == $0
  exit check_search_categories_consistent()
end