-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateTTL.py
78 lines (60 loc) · 2.39 KB
/
validateTTL.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from rdflib import Graph, URIRef
import re
def fix_uris(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Remove line breaks within URIs
content = re.sub(r'http://www \.\n\s*w3 \.\n\s*org/', 'http://www.w3.org/', content)
content = re.sub(r'http://schema \.\n\s*org/', 'http://schema.org/', content)
content = re.sub(r'http://example \.\n\s*org/', 'http://example.org/', content)
# Write the fixed content back to the file
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"URIs fixed in {file_path}")
# Usage
#fix_uris('test.ttl')
is_ok = True
# Create a new graph
g = Graph()
# Parse the Turtle file
try:
g.parse("test.ttl", format="turtle")
print("Turtle file is valid.")
except Exception as e:
print(f"An error occurred: {e}")
is_ok = False
# Perform additional validation checks as needed
# For example, check if a specific resource exists
'''
uri = URIRef("http://example.org/some/resource")
if (uri, None, None) in g:
print(f"Resource {uri} exists in the graph.")
else:
print(f"Resource {uri} not found in the graph.")
'''
def fix_turtle_syntax(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Fix common Turtle syntax issues
# Ensure proper spacing around semicolons and periods
#content = re.sub(r'\s*;\s*', ' ;\n', content)
#content = re.sub(r'\s*\.\s*', ' .\n', content)
content = re.sub(r'\s*\:\s*', ':', content)
content = re.sub(r'(example:[^\s]+)\s([^\s]+) (a schema:)', r'\1_\2 \3', content, flags=re.MULTILINE)
# Ensure quotes are properly closed
#content = re.sub(r'\"([^\"]*)\"', r'"\1"', content)
# Write the fixed content back to the file
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Fixed syntax in {file_path}")
# Usage
#if(not is_ok):
# fix_turtle_syntax('test.ttl')
#if(not is_ok):
# fix_turtle_syntax('test.ttl')
xsd_text = 'this a text \n nvdsj schema:mortalityRate "6"^^xsd:integer ;\n schema:rank xsd:integer 3 \n jfjfd f'
xsd_text = re.sub(r'(xsd:integer\s)([0-9]+)', r'"\2"^^\1', xsd_text)
print(xsd_text)
xsd_text = 'this a text \n nvdsj schema:mortalityRate "6"^^xsd:integer ;\n schema:rank xsd:integer 3 \n jfjfd f'
xsd_text = re.sub(r'(xsd:integer\s)([0-9]+)', r'"\2"^^\1', xsd_text)
print(xsd_text)