-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_citations.py
52 lines (39 loc) · 1.35 KB
/
process_citations.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
import requests
import json
def process_citations(citations):
# Grobid server URL (replace with the actual URL if different)
url = "http://host.docker.internal:8070/api/processCitationList"
# Prepare the data
data = {
"citations": citations
}
# Set the headers
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/x-bibtex"
}
try:
# Send POST request to Grobid
response = requests.post(url, data=data, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
result = response.text
return result
else:
print(f"Error: Received status code {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"Error occurred: {e}")
return None
# Example usage
if __name__ == "__main__":
from pathlib import Path
import sys
citation_txt = Path(sys.argv[1]).read_text().split("\n\n")
result = process_citations(citation_txt)
# result = process_citations("Doe J. et al., A study of citation processing, Journal of Citations, 2023.")
if result:
print(result)
else:
print("Failed to process citations.")