-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add correct_typos.py as a replacement for preprocess --corr=...
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Read a corrections file (path as first argument to the script) | ||
and then read line by line from standard input, and substitute each | ||
word with the correct one from the corrections file.""" | ||
from sys import stdin, argv | ||
|
||
if len(argv) <= 1: | ||
exit("usage: python {argv[0]} <correction_file>") | ||
|
||
|
||
def read_corrections_file(path): | ||
lookups = {} | ||
with open(path, "r") as f: | ||
lines = f.readlines() | ||
for line in lines: | ||
line = line.strip() | ||
try: | ||
wrong, right = line.split("\t") | ||
except ValueError: | ||
pass | ||
else: | ||
lookups[wrong] = right | ||
return lookups | ||
|
||
|
||
def main(): | ||
correction_file = argv[1] | ||
corrections = read_corrections_file(correction_file) | ||
|
||
for line in stdin.readlines(): | ||
line = line.strip() | ||
if not line: | ||
continue | ||
print(corrections.get(line, line)) | ||
|
||
|
||
if __name__ == "__main__": | ||
raise SystemExit(main()) |