-
Notifications
You must be signed in to change notification settings - Fork 94
Use argparse in plain_bpmf_compiler #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,50 +1,62 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
| from .compiler_utils import HEADER, convert_vks_rows_to_sorted_kvs_rows | ||
| import argparse | ||
| import re | ||
| import sys | ||
|
|
||
| __author__ = "Lukhnos Liu and The McBopomofo Authors" | ||
| __copyright__ = "Copyright 2012 and onwards The McBopomofo Authors" | ||
| __license__ = "MIT" | ||
|
|
||
| skip = re.compile(r".\s+_punctuation.*_>") | ||
| # Punctuation fix for Plain Bopomofo mode | ||
| SKIP_RE = re.compile(r".\s+_punctuation.*_>") | ||
| PUNCTUATION_FIX = [".", '_punctuation_"', "0.0"] | ||
|
|
||
| insert = [".", "_punctuation_\"", "0.0"] | ||
|
|
||
|
|
||
| def main(): | ||
| if len(sys.argv) < 4: | ||
| sys.exit('Usage: cook-plain-bpmf.py bpmf-base punctuation-list output') | ||
|
|
||
| bpmf_base = open(sys.argv[1], "r") | ||
| punctuation_list = open(sys.argv[2], "r") | ||
| def cook(bpmf_base_path, punctuations_path, output_path): | ||
| output = [] | ||
|
|
||
| while True: | ||
| line = bpmf_base.readline() | ||
| if not line: break | ||
| kv = line.split(" ") | ||
| output.append((kv[0], kv[1], "0.0")) | ||
|
|
||
| while True: | ||
| line = punctuation_list.readline() | ||
| if not line: | ||
| break | ||
| if skip.search(line): | ||
| continue | ||
| row = line.rstrip().split(" ") | ||
| assert len(row) == 3 | ||
| output.append(row) | ||
|
|
||
| output.append(insert) | ||
| with open(bpmf_base_path, "r") as bpmf_base: | ||
| for line in bpmf_base: | ||
| line = line.strip() | ||
| if not line: | ||
| continue | ||
| elements = line.split(" ") | ||
| assert len(elements) >= 2 | ||
| output.append((elements[0], elements[1], "0.0")) | ||
|
|
||
| with open(punctuations_path, "r") as punctuation_list: | ||
|
lukhnos marked this conversation as resolved.
|
||
| for line in punctuation_list: | ||
| if SKIP_RE.search(line): | ||
| continue | ||
| row = line.rstrip().split(" ") | ||
| assert len(row) == 3 | ||
| output.append(row) | ||
|
|
||
| output.append(PUNCTUATION_FIX) | ||
|
|
||
| output = convert_vks_rows_to_sorted_kvs_rows(output) | ||
| with open(sys.argv[3], "w") as fout: | ||
| fout.write(HEADER) | ||
| with open(output_path, "w") as output_file: | ||
|
lukhnos marked this conversation as resolved.
|
||
| output_file.write(HEADER) | ||
| for row in output: | ||
| fout.write("%s %s %s\n" % tuple(row)) | ||
| output_file.write("%s %s %s\n" % tuple(row)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="compile plain Bopomofo phrases database" | ||
| ) | ||
| parser.add_argument("--bpmf_base", required=True) | ||
| parser.add_argument("--punctuations", required=True) | ||
| parser.add_argument("--output", required=True) | ||
|
|
||
| args = parser.parse_args() | ||
| cook( | ||
| bpmf_base_path=args.bpmf_base, | ||
| punctuations_path=args.punctuations, | ||
| output_path=args.output, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.