-
Notifications
You must be signed in to change notification settings - Fork 1
/
lowercase_ner_data.py
96 lines (83 loc) · 3.04 KB
/
lowercase_ner_data.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
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
import json
import argparse
import random
from typing import Iterable, TypedDict, List
Sentence = TypedDict("Sentence",
{
"id": str,
"tokens": List[str],
"pos_tags": List[str],
"ner_tags": List[str],
}
)
def lowercase_all_text(fn: str, both: bool, mix: bool) -> Iterable[Sentence]:
with open(fn) as fh:
for line in fh:
jline = json.loads(line)
lower_jline = {k: v for k, v in jline.items()} # type: Sentence
lower_jline["tokens"] = [token.lower() for token in jline["tokens"]]
lower_jline["id"] = jline["id"] + "_lower"
if both:
yield jline
yield lower_jline
elif mix:
if random.random() <= 0.5:
yield jline
else:
yield lower_jline
else:
yield lower_jline
def lowercase_ne_only(fn: str, both: bool, mix: bool) -> Iterable[Sentence]:
with open(fn) as fh:
for line in fh:
jline = json.loads(line)
tokens = []
for tag, token in zip(jline["ner_tags"], jline["tokens"]):
if tag != "O":
token = token.lower()
tokens.append(token)
lower_jline = {k: v for k, v in jline.items()} # type: Sentence
lower_jline["tokens"] = tokens
lower_jline["id"] = jline["id"] + "_ne_lower"
if both:
yield jline
yield lower_jline
elif mix:
if random.random() <= 0.5:
yield jline
else:
yield lower_jline
else:
yield lower_jline
def lowercase_wrapper(fn_in: str,
ne_only: bool,
both: bool,
mix: bool,
fn_out,
) -> None:
with open(fn_out, "w") as fout:
if ne_only:
for jline in lowercase_ne_only(fn_in, both, mix):
print(json.dumps(jline), file=fout)
else:
for jline in lowercase_all_text(fn_in, both, mix):
print(json.dumps(jline), file=fout)
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--infiles", nargs="+")
parser.add_argument("--outfiles", nargs="+")
parser.add_argument("--ne_only", action="store_true")
parser.add_argument("--both", action="store_true")
parser.add_argument("--mix", action="store_true")
parser.add_argument("--seed", type=int)
return parser.parse_args()
def main() -> None:
args = get_args()
if args.seed:
random.seed(args.seed)
print(args)
assert len(args.infiles) == len(args.outfiles)
for i, o in zip(args.infiles, args.outfiles):
lowercase_wrapper(i, args.ne_only, args.both, args.mix, o)
if __name__ == "__main__":
main()