| """Correct-by-construction IES4 training data. |
| Builds valid IES4 graphs with telicent-ies-tool across 12 patterns, double-validates |
| each (telicent SHACL + our term-membership validator), and emits |
| {pattern, facts, turtle} records. A deterministic natural-language 'facts' string is |
| attached; a later step paraphrases it with the local Qwen for linguistic diversity. |
| Run under .venv311 (telicent needs py>=3.10).""" |
| import sys, json, pathlib, random, argparse |
| sys.path.insert(0, str(pathlib.Path(__file__).parent)) |
| from ies_tool.ies_tool import (IESTool, Person, Organisation, Event, Location, |
| Device, Asset, Communication, PartyInCommunication) |
| from iesval import validate_turtle |
|
|
| random.seed(101) |
| ROOT = pathlib.Path("/Users/fabio/projects/qwen-ies-ft") |
|
|
| GIVEN = ["Fred","Alice","Barry","Priya","Vladimir","Chen","Amara","Sofia","Liam","Yusuf", |
| "Grace","Diego","Nadia","Tom","Ivy","Omar","Hannah","Kofi","Elena","Sam"] |
| SUR = ["Smith","Okafor","Patel","Ivanov","Wang","Rossi","Khan","Nguyen","Brown","Silva", |
| "Adebayo","Muller","Kowalski","Andersson","Costa","Haddad","Murphy","Reyes"] |
| ORGS = ["Acme Logistics Ltd","Northgate Shipping","Meridian Bank","Halcyon Devices", |
| "Blue Harbour Freight","Sterling Manufacturing","Corvus Security","Pine Valley Clinic", |
| "Redwood Analytics","Orion Telecom","Harbourline Ferries","Vertex Engineering"] |
| PLACES = ["Port of Dover","Heathrow Terminal 4","Manchester Depot","University Hospital", |
| "Felixstowe Docks","Kings Cross Station","Birmingham Warehouse","Gatwick Cargo", |
| "Southampton Container Port","Leeds Distribution Centre","Bristol Harbour"] |
| CITIES = ["London","Manchester","Birmingham","Glasgow","Cardiff","Belfast","Leeds","Dover"] |
| DEVICES = ["Nokia handset","Samsung Galaxy phone","Toyota Hilux van","DAF articulated lorry", |
| "Dell laptop","Yamaha outboard motor","forklift truck","Land Rover Defender"] |
| POSTS = ["Warehouse Supervisor","Chief Engineer","Compliance Officer","Ship's Master", |
| "Security Analyst","Ward Nurse","Logistics Coordinator","Border Officer"] |
| UNITS = [("kilogram","kg"),("metre","m"),("year","yr"),("centimetre","cm")] |
|
|
| def yr(a=1970,b=2005): return random.randint(a,b) |
| def date(y=None): |
| y = y or yr(2010,2024) |
| return f"{y}-{random.randint(1,12):02d}-{random.randint(1,28):02d}" |
| def dt(): return date(yr(2018,2024))+f"T{random.randint(6,20):02d}:00:00" |
|
|
| def new(): |
| t = IESTool(); t.default_data_namespace = "http://data.gov.uk/testdata#"; return t |
|
|
| |
| def t_employment(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR); org=random.choice(ORGS) |
| o=Organisation(tool=t, name=org) |
| dob=date(yr(1960,1998)) |
| p=Person(tool=t, given_name=g, surname=s, date_of_birth=dob) |
| start=date(yr(2012,2020)); p.works_for(o, start=start) |
| facts=f"{g} {s}, born {dob}, has worked for {org} since {start}." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_employment_ended(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR); org=random.choice(ORGS) |
| o=Organisation(tool=t, name=org) |
| p=Person(tool=t, given_name=g, surname=s) |
| a=yr(2008,2016); b=yr(2017,2023) |
| p.works_for(o, start=date(a), end=date(b)) |
| facts=f"{g} {s} worked for {org} from {a} until {b}, then left." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_birth_death(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR) |
| bp=Location(tool=t); bp.add_name(random.choice(CITIES)) |
| p=Person(tool=t, given_name=g, surname=s) |
| p.add_birth(date_of_birth=date(yr(1930,1960)), place_of_birth=bp) |
| if random.random()<0.6: |
| dp=Location(tool=t); dp.add_name(random.choice(CITIES)) |
| p.add_death(date_of_death=date(yr(2000,2023)), place_of_death=dp) |
| facts=f"{g} {s} was born in one city and later died in another." |
| else: |
| facts=f"{g} {s} was born in a UK city." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_event_participation(): |
| t=new(); loc=Location(tool=t); loc.add_name(random.choice(PLACES)) |
| ev=Event(tool=t, start=dt(), end=dt()); ev.add_name(random.choice( |
| ["team meeting","container inspection","cargo handover","security briefing","site visit"])) |
| ev.in_location(loc) |
| people=[] |
| for _ in range(random.randint(2,4)): |
| p=Person(tool=t, given_name=random.choice(GIVEN), surname=random.choice(SUR)) |
| ev.add_participant(p); people.append(p) |
| facts=(f"{len(people)} people took part in an event at {loc}, which had a start and end time.") |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_identifier(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR) |
| p=Person(tool=t, given_name=g, surname=s) |
| num=f"{random.randint(10,99)}{random.choice('ABCDEFGH')}{random.randint(1000,9999)}" |
| p.add_identifier(num, id_class="http://ies.data.gov.uk/ontology/ies4#NationalIdentityNumber") |
| facts=f"{g} {s} is identified by the national identity number {num}." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_ownership(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR); d=random.choice(DEVICES) |
| p=Person(tool=t, given_name=g, surname=s) |
| dev=Device(tool=t); dev.add_name(d) |
| p.owns(dev, start=date(yr(2015,2022))) |
| facts=f"{g} {s} owns a {d}, acquired at a known date." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_post_holding(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR); org=random.choice(ORGS); post=random.choice(POSTS) |
| o=Organisation(tool=t, name=org) |
| pp=o.create_post(name=post, start=date(yr(2016,2021))) |
| p=Person(tool=t, given_name=g, surname=s) |
| p.in_post(pp, start=date(yr(2016,2021))) |
| facts=f"{g} {s} holds the post of {post} at {org}." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_location_state(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR) |
| loc=Location(tool=t); loc.add_name(random.choice(PLACES)) |
| p=Person(tool=t, given_name=g, surname=s) |
| p.create_state(start=dt(), end=dt(), in_location=loc) |
| facts=f"{g} {s} was present at {loc} for a bounded period of time." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_access(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR); d=random.choice(DEVICES) |
| p=Person(tool=t, given_name=g, surname=s) |
| dev=Device(tool=t); dev.add_name(d) |
| p.has_access_to(dev, start=date(yr(2019,2023))) |
| facts=f"{g} {s} has access to a {d} from a given date." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_org_owns_asset(): |
| t=new(); org=random.choice(ORGS); d=random.choice(DEVICES) |
| o=Organisation(tool=t, name=org); a=Asset(tool=t); a.add_name(d) |
| o.owns(a, start=date(yr(2010,2020))) |
| facts=f"{org} owns a {d} as one of its assets." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_possession(): |
| t=new(); g=random.choice(GIVEN); s=random.choice(SUR); d=random.choice(DEVICES) |
| p=Person(tool=t, given_name=g, surname=s) |
| dev=Device(tool=t); dev.add_name(d) |
| p.in_possession_of(dev, start=dt()) |
| facts=f"{g} {s} was in possession of a {d} from a certain time." |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_communication(): |
| t=new(); g1,s1=random.choice(GIVEN),random.choice(SUR); g2,s2=random.choice(GIVEN),random.choice(SUR) |
| p1=Person(tool=t, given_name=g1, surname=s1); p2=Person(tool=t, given_name=g2, surname=s2) |
| d1=Device(tool=t); d1.add_name(random.choice(DEVICES)); d2=Device(tool=t); d2.add_name(random.choice(DEVICES)) |
| st=dt() |
| comm=Communication(tool=t, start=st) |
| pa=comm.create_party(); pa.add_person(p1); pa.add_device(d1) |
| pb=comm.create_party(); pb.add_person(p2); pb.add_device(d2) |
| facts=(f"{g1} {s1} and {g2} {s2} took part in a communication that began at {st}, " |
| f"each using their own device.") |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_composite_case(): |
| |
| t=new(); g,s=random.choice(GIVEN),random.choice(SUR); org=random.choice(ORGS) |
| o=Organisation(tool=t, name=org) |
| p=Person(tool=t, given_name=g, surname=s, date_of_birth=date(yr(1965,1995))) |
| p.works_for(o, start=date(yr(2014,2020))) |
| dev=Device(tool=t); dev.add_name(random.choice(DEVICES)); p.in_possession_of(dev, start=dt()) |
| loc=Location(tool=t); loc.add_name(random.choice(PLACES)) |
| ev=Event(tool=t, start=dt(), end=dt()); ev.add_name(random.choice( |
| ["site inspection","handover meeting","security review"])); ev.in_location(loc) |
| ev.add_participant(p) |
| facts=(f"{g} {s} works for {org}, was in possession of a device, and attended an event " |
| f"with a start and end time held at {loc}.") |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| def t_composite_org(): |
| |
| t=new(); org=random.choice(ORGS); post=random.choice(POSTS); g,s=random.choice(GIVEN),random.choice(SUR) |
| o=Organisation(tool=t, name=org) |
| pp=o.create_post(name=post, start=date(yr(2015,2020))) |
| p=Person(tool=t, given_name=g, surname=s); p.in_post(pp, start=date(yr(2016,2021))) |
| a=Asset(tool=t); a.add_name(random.choice(DEVICES)); o.owns(a, start=date(yr(2012,2019))) |
| loc=Location(tool=t); loc.add_name(random.choice(PLACES)) |
| ev=Event(tool=t, start=dt(), end=dt()); ev.add_name("audit visit"); ev.in_location(loc) |
| ev.add_participant(p) |
| facts=(f"{org} has the post of {post}, held by {g} {s}, owns an asset, and hosted an " |
| f"audit visit at {loc} that {g} attended.") |
| return facts, t.get_rdf(rdf_format="turtle")["triples"] |
|
|
| TEMPLATES = [t_employment, t_employment_ended, t_birth_death, t_event_participation, |
| t_identifier, t_ownership, t_post_holding, t_location_state, |
| t_access, t_org_owns_asset, t_possession, |
| t_communication, t_composite_case, t_composite_org] |
|
|
| def main(): |
| ap=argparse.ArgumentParser(); ap.add_argument("--n", type=int, default=1200) |
| args=ap.parse_args() |
| out=(ROOT/"data"/"ground.jsonl").open("w") |
| kept=0; fail=0; per={} |
| for i in range(args.n): |
| fn=TEMPLATES[i % len(TEMPLATES)] |
| try: |
| facts, ttl = fn() |
| except Exception as e: |
| fail+=1; continue |
| ok, reason, n, _ = validate_turtle(ttl, min_triples=5) |
| if ok: |
| out.write(json.dumps({"pattern":fn.__name__, "facts":facts, "turtle":ttl})+"\n") |
| kept+=1; per[fn.__name__]=per.get(fn.__name__,0)+1 |
| else: |
| fail+=1 |
| out.close() |
| print(f"ground pairs kept={kept} failed={fail}") |
| for k in sorted(per): print(f" {k}: {per[k]}") |
|
|
| if __name__=="__main__": |
| main() |
|
|