forked from ppke-nlpg/gut-besser-chunker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoteN.py
executable file
·37 lines (32 loc) · 1.22 KB
/
voteN.py
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
#!/usr/bin/python3
# -*- coding: utf-8, vim: expandtab:ts=4 -*-
from collections import Counter
import sys
import os.path
if len(sys.argv) == 1:
print('USAGE: {0} [file,]+ file'.format(sys.argv[0]), file=sys.stderr)
print("Every file must have the labels as last field!", file=sys.stderr)
sys.exit(1)
fh = []
for f in sys.argv[1:]:
if os.path.exists(f):
fh.append(open(f, encoding='UTF-8'))
no_of_voters = len(fh)
for i, lines in enumerate(zip(*fh)):
if len(lines[0].strip()) == 0:
print()
else:
out_line = lines[0].strip() # This is needed for output
gold = out_line.split()[-2]
out_lines = []
vote = Counter()
for ind in range(no_of_voters):
label = lines[ind].strip().split()[-1]
out_lines.append(label)
vote[label] += 1 # Vote for the label...
chunk_max, maximum = max(sorted(vote.items()), key=lambda x: x[1]) # And the winner is...
count_of_max = Counter(vote.values())[maximum]
votes = ' '.join(out_lines[1:])
if count_of_max > 1:
print('Tie({0})'.format(count_of_max), out_line, votes, gold, chunk_max, '!', file=sys.stderr)
print(out_line, votes, gold, chunk_max)