forked from Bilingual-Annotation-Task-Force/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpos_transitions.py
More file actions
executable file
·121 lines (100 loc) · 3.55 KB
/
Copy pathpos_transitions.py
File metadata and controls
executable file
·121 lines (100 loc) · 3.55 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
#!/usr/bin/env python3
# pos_transitions.py
# Using Python 3.4.3
#
# PURPOSE: Calculate transitions between pos-tags to describe code-switching behavior in
# pos-tagged corpora.
import sys
import argparse
TOKCOL = 0
LANGCOL = 1
POSCOL = 2
DELIMITER = "\t"
HEADER = False
VERBOSE = False
INFILE = 0
OUTFILE = 0
def main(argc, argv):
tags = []
tokens = []
lang_tags = []
pos_tags = []
# Read all tags
for line in INFILE:
token = line.split(DELIMITER)[TOKCOL]
tokens.append(token.strip())
lang_tag = line.split(DELIMITER)[LANGCOL]
lang_tags.append(lang_tag.strip())
pos_tag = line.split(DELIMITER)[POSCOL]
pos_tags.append(pos_tag.strip())
# Skip first line if header specified
if HEADER:
tokens = tokens[1:]
lang_tags = lang_tags[1:]
pos_tags = pos_tags[1:]
# Combine in one list
tags = list(zip(tokens, lang_tags, pos_tags))
# Print working set of language tags if needed
if VERBOSE:
print("Length of tokens: {}".format(len(tokens)))
print("Set of language tags: {}".format(set(lang_tags)))
print("Set of POS tags: {}".format(set(pos_tags)))
line_bigrams = zip(tags, tags[1:])
OUTFILE.write("Lang1\tLang2\tPOS1\tPOS2\tTOK1\tTOK2\n")
# Compute transitions and find tokens
for line1, line2 in line_bigrams:
tok1, tok2 = line1[0], line2[0]
lang1, lang2 = line1[1], line2[1]
pos1, pos2 = line1[2], line2[2]
OUTFILE.write("\t".join((lang1, lang2, pos1, pos2, tok1, tok2)))
OUTFILE.write("\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=("Compute transitions of POS tags and languages"
" over all tokens"))
# Optional arguments
parser.add_argument(
"-d", "--delimiter",
type=str,
default="\t",
help="delimiter for input file (Default: tab)")
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="verbose flag")
parser.add_argument(
"-c", "--columns",
metavar=("col1", "col2", "col3"),
nargs=3,
default=[0, 1, 2],
help=("Token, POS-tag, and language columns in input file "
"(Default: 0-2)"))
parser.add_argument(
"--header",
action="store_true",
help="header flag (Default: False)")
# Positional arguments
parser.add_argument(
"infile",
nargs="?",
type=argparse.FileType("r"),
default=sys.stdin,
help="corpus file (Default: stdin)")
parser.add_argument(
"outfile",
nargs="?",
type=argparse.FileType("w"),
default=sys.stdout,
help="transition file (Default: stdout)")
args = parser.parse_args()
if args.verbose:
VERBOSE = True
if args.header:
HEADER = True
DELIMITER = args.delimiter
INFILE = args.infile
TOKCOL = int(args.columns[0])
LANGCOL = int(args.columns[1])
POSCOL = int(args.columns[2])
OUTFILE = args.outfile
main(len(sys.argv), sys.argv)