dkl9 commited on 2025-203 00:58:58
Showing 1 changed files, with 12 additions and 2 deletions.
| ... | ... |
@@ -64,8 +64,12 @@ class Trie: |
| 64 | 64 |
self.use(tokens) |
| 65 | 65 |
|
| 66 | 66 |
if __name__ == "__main__": |
| 67 |
- parser = argparse.ArgumentParser(prog="dost", description="Diverse-order sampling by trie") |
|
| 67 |
+ parser = argparse.ArgumentParser( |
|
| 68 |
+ prog="dost", description="Diverse-order sampling by trie", |
|
| 69 |
+ epilog="Tends to run faster on shorter tokens" |
|
| 70 |
+ ) |
|
| 68 | 71 |
parser.add_argument("files", default="-", nargs="*")
|
| 72 |
+ parser.add_argument("-i", "--in-place", action="store_true")
|
|
| 69 | 73 |
parser.add_argument( |
| 70 | 74 |
"-s", "--separators", default=" \t", |
| 71 | 75 |
help="characters that separate tokens in an input item (default space and tab)" |
| ... | ... |
@@ -75,6 +79,8 @@ if __name__ == "__main__": |
| 75 | 79 |
help="structural regexp for a token of an input item, overrides -s" |
| 76 | 80 |
) |
| 77 | 81 |
args = parser.parse_args() |
| 82 |
+ if args.in_place and len(args.files) != 1: |
|
| 83 |
+ sys.exit(1) |
|
| 78 | 84 |
re_flags = re.MULTILINE | re.DOTALL |
| 79 | 85 |
if args.token: |
| 80 | 86 |
token_pattern = args.token |
| ... | ... |
@@ -87,5 +93,9 @@ if __name__ == "__main__": |
| 87 | 93 |
with sys.stdin if filename == "-" else open(filename, "r") as handle: |
| 88 | 94 |
for word in re.finditer(entry_pattern, handle.read()): |
| 89 | 95 |
t.add(re.findall(token_pattern, word.group(0))) |
| 96 |
+ output_handle = sys.stdout |
|
| 97 |
+ if args.in_place and args.files[0] != "-": |
|
| 98 |
+ output_handle = open(args.files[0], "w") |
|
| 90 | 99 |
for word in t.traverse(): |
| 91 |
- print("".join(word))
|
|
| 100 |
+ output_handle.write("".join(word) + "\n")
|
|
| 101 |
+ output_handle.close() |
|
| 92 | 102 |