-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinker.py
More file actions
65 lines (51 loc) · 1.7 KB
/
linker.py
File metadata and controls
65 lines (51 loc) · 1.7 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
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests>=2.31.0",
# ]
# ///
import argparse
import logging
import sys
from pathlib import Path
import requests
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
def process_file(source_file: Path, target_file: Path, backend_url: str):
logger.info(f"Reading {source_file}")
with open(source_file, "r", encoding="utf-8") as f:
file_contents = f.read()
payload = {"format": "conllu", "source": file_contents}
logger.info(f"Calling {backend_url}")
response = requests.post(backend_url, json=payload)
response.raise_for_status()
logger.info(f"Writing {target_file}")
target_file.parent.mkdir(parents=True, exist_ok=True)
with open(target_file, "w", encoding="utf-8") as f:
f.write(response.json()["target"])
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--source", type=Path, required=True)
parser.add_argument("--target", type=Path, required=True)
parser.add_argument(
"--backend-url",
type=str,
required=True,
help="URL of the prelinker backend endpoint from an instance of https://github.com/LiITA-LOD/text-linker",
)
args = parser.parse_args()
try:
process_file(args.source, args.target, args.backend_url)
except KeyboardInterrupt:
logger.info("Processing interrupted by user")
sys.exit(1)
except Exception as e:
logger.error(f"Unexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()