-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.rb
93 lines (78 loc) · 1.69 KB
/
parser.rb
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
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
require 'csv'
require 'progress'
def split_languages(raw)
langs = []
lang_raw = ''
lang = nil
raw.split("\n").each do |line|
match = /^==([^=]*?)==$/.match(line.strip)
if not match.nil?
if not lang.nil?
langs.push([lang,lang_raw])
end
lang_raw = ''
lang = match[1]
else
lang_raw += line+"\n"
end
end
if not lang.nil?
langs.push([lang,lang_raw])
end
return langs
end
def parse(page)
#puts page
title_match = /<title>(.*?)<\/title>/m.match(page)
match = /<text xml:space="preserve">(.*?)<\/text>/m.match(page)
return false if not match
title = title_match[1]
if title.start_with?("Wiktionary:", "Talk:", "User talk:", "User:", 'Appendix:', 'Help talk:','Citations:')
return false
end
raw = match[1]
#puts raw
#split by languages
langs = split_languages(raw)
#p langs
#puts title
templates = nil
c=0
langs.each do |lang,lang_raw|
if lang == 'English'
c+=1
templates = lang_raw.scan(/{{(?:en-plural noun|en-noun|en-proper noun|en-prop|en-proper-noun).*?}}/)
if c>1
puts 'More than one ==English== definitions', title
#puts page
end
end
end
if not templates.nil?
return [title, templates]
end
return false
end
f = File.open(ARGV[0])
o = CSV.open('word_templates.csv','w')
page = ''
Progress.start(f.size)
f.each do |line|
Progress.set(f.pos)
if line.strip == '<page>'
page = ''
elsif line.strip == '</page>'
result = parse(page)
next if not result
title, templates = result
#p result
if not templates.empty?
o << [title] + templates
end
else
page += line
end
end
Progress.stop
f.close
o.close