metadata.py
6.66 KB
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
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
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
152
153
154
155
156
157
158
159
160
import argparse
import os
def main():
args = parse_arguments()
if not args.input:
print('Error: Corpora root directory must be selected!')
statistics = {'documents': 0,
'paragraphs': 0,
'sentences': 0,
'segments': 0,
'by_year': {},
'by_type': {},
'by_content_type': {},
'by_licence': {}}
count_corpora_stats(args.input, statistics)
write_corpora_stats(statistics, args.output)
def parse_arguments():
parser = argparse.ArgumentParser(description='Generate statistics for conllup corpora.')
parser.add_argument('-o', '--output', help='output tsv file with collected statistics')
required_arguments = parser.add_argument_group('required arguments')
required_arguments.add_argument('-i', '--input', help='corpora root directory', required=True)
return parser.parse_args()
def count_corpora_stats(root_directory, stats):
for root, dirs, files in os.walk(root_directory):
for filename in files:
if filename.endswith('.conllup') or filename.endswith('.conllu'):
print('Counting statistics for %s.' % filename)
count_document_statistics(os.path.join(root, filename), stats)
def count_document_statistics(filepath, stats):
year = None
entype = None
content_type = None
licence = None
with open(filepath, 'r') as conllup_file:
for line in conllup_file:
line = line.strip()
if is_segment(line):
stats['segments'] += 1
stats['by_year'][year]['segments'] += 1
stats['by_content_type'][content_type]['segments'] += 1
stats['by_licence'][licence]['segments'] += 1
if entype:
stats['by_type'][entype]['segments'] += 1
elif is_metadata(line):
name, value = get_metadata(line)
if name == 'PublicationDate':
entype = None
year = get_year(value)
if year not in stats['by_year']:
stats['by_year'][year] = {'documents': 0, 'paragraphs': 0, 'sentences': 0, 'segments': 0}
stats['documents'] += 1
stats['by_year'][year]['documents'] += 1
elif name == 'Domain':
entype = value
if entype not in stats['by_type']:
stats['by_type'][entype] = {'documents': 0, 'paragraphs': 0, 'sentences': 0, 'segments': 0}
stats['by_type'][entype]['documents'] += 1
elif name == 'DocumentType':
content_type = value
if content_type not in stats['by_content_type']:
stats['by_content_type'][content_type] = {'documents': 0, 'paragraphs': 0,
'sentences': 0, 'segments': 0}
stats['by_content_type'][content_type]['documents'] += 1
elif name == 'Licence':
licence = value
if licence not in stats['by_licence']:
stats['by_licence'][licence] = {'documents': 0, 'paragraphs': 0,
'sentences': 0, 'segments': 0}
stats['by_licence'][licence]['documents'] += 1
elif name == 'newpar id':
stats['paragraphs'] += 1
stats['by_year'][year]['paragraphs'] += 1
stats['by_content_type'][content_type]['paragraphs'] += 1
stats['by_licence'][licence]['paragraphs'] += 1
if entype:
stats['by_type'][entype]['paragraphs'] += 1
elif name == 'sent_id':
stats['sentences'] += 1
stats['by_year'][year]['sentences'] += 1
stats['by_content_type'][content_type]['sentences'] += 1
stats['by_licence'][licence]['sentences'] += 1
if entype:
stats['by_type'][entype]['sentences'] += 1
def is_segment(line):
if line and line[0].isdigit():
return True
return False
def is_metadata(line):
if line.startswith('#'):
return True
return False
def get_metadata(line):
name_value_pair = line.split('=', 1)
name = name_value_pair[0].lstrip('#').strip()
value = name_value_pair[1].strip()
return name, value
def get_year(date):
return date.split('-')[0]
def write_corpora_stats(stats, outpath):
if outpath:
with open(outpath, 'w') as outfile:
outfile.write(stats_to_string(stats))
else:
print(stats_to_string(stats))
def stats_to_string(stats):
lines = ['\tDocuments\tParagraphs\tSentences\tSegments',
'Total\t%d\t%d\t%d\t%d' % (stats['documents'], stats['paragraphs'], stats['sentences'], stats['segments']),
'\nBy year:']
for year, stats_by_year in sorted(stats['by_year'].items(), key=lambda kv: kv[0], reverse=True):
lines.append('%s\t%d\t%d\t%d\t%d' % (year, stats_by_year['documents'], stats_by_year['paragraphs'],
stats_by_year['sentences'], stats_by_year['segments']))
lines.append('\nBy type:')
for entype, stats_by_type in sorted(stats['by_type'].items(), key=lambda kv: kv[1]['documents'], reverse=True):
lines.append('%s\t%d\t%d\t%d\t%d' % (entype, stats_by_type['documents'], stats_by_type['paragraphs'],
stats_by_type['sentences'], stats_by_type['segments']))
lines.append('\nBy content type:')
for content_type, stats_by_content_type in sorted(stats['by_content_type'].items(),
key=lambda kv: kv[1]['documents'], reverse=True):
lines.append('%s\t%d\t%d\t%d\t%d' % (content_type, stats_by_content_type['documents'],
stats_by_content_type['paragraphs'], stats_by_content_type['sentences'],
stats_by_content_type['segments']))
lines.append('\nBy licence:')
for licence, stats_by_licence in sorted(stats['by_licence'].items(), key=lambda kv: kv[1]['documents'],
reverse=True):
lines.append('%s\t%d\t%d\t%d\t%d' % (licence, stats_by_licence['documents'], stats_by_licence['paragraphs'],
stats_by_licence['sentences'], stats_by_licence['segments']))
return '\n'.join(lines)
if __name__ == '__main__':
main()