Download code/generator.py from Vijayarv07/grounding-checker: direct link, hf CLI and curl.
- Browser
- Download file 15.9 kB
-
https://huggingface.co/datasets/Vijayarv07/grounding-checker/resolve/main/code/generator.py
- Command line
-
hf download hf://datasets/Vijayarv07/grounding-checker/code/generator.py
-
curl -L -o generator.py https://huggingface.co/datasets/Vijayarv07/grounding-checker/resolve/main/code/generator.py
15.9 kB
| """Synthetic grounding/faithfulness dataset. | |
| Each example is a short source document plus a one-sentence claim about it, labelled: | |
| 0 = grounded (the source supports the claim) | |
| 1 = not grounded (unsupported or contradicted by the source) | |
| Design goal: make it HARD, so a trivial word-overlap classifier can't win. | |
| - Grounded claims are often paraphrased or aggregated, so they don't copy source wording. | |
| - Not-grounded claims reuse real entities/numbers from the source, so they look plausible. | |
| Everything is generated from structured facts; no scraped or private text. | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| import random | |
| # ---------------------------------------------------------------- fact templates | |
| FIRST = ["Maria", "David", "Aisha", "Chen", "Priya", "Omar", "Sofia", "Liam", "Ingrid", "Raj", | |
| "Elena", "Kofi", "Yuki", "Noah", "Fatima", "Diego", "Hannah", "Wei", "Leila", "Tomas"] | |
| LAST = ["Alvarez", "Okafor", "Nguyen", "Patel", "Andersson", "Haddad", "Rossi", "Kim", "Mbeki", | |
| "Costa", "Larsson", "Reyes", "Tanaka", "Silva", "Novak", "Ibrahim", "Weber", "Owusu"] | |
| CITIES = ["Austin", "Lyon", "Nairobi", "Jakarta", "Porto", "Tallinn", "Medellín", "Pune", | |
| "Leeds", "Osaka", "Calgary", "Valencia", "Accra", "Gdańsk"] | |
| COMPANIES = ["Northwind", "Helios Labs", "Bluefin", "Vireo", "Camberwell Foods", "Opsley", | |
| "Tanager", "Fernweh", "Kestrel Systems", "Marlowe & Co", "Dovetail", "Arcadia Freight"] | |
| PRODUCTS = ["the Atlas tablet", "the Pico earbuds", "the Verge smartwatch", "the Lumen speaker", | |
| "the Drift e-bike", "the Nimbus thermostat", "the Harbor router", "the Sage monitor"] | |
| FIELDS = ["marine biology", "supply-chain logistics", "renewable energy", "medieval history", | |
| "behavioral economics", "materials science", "urban planning", "epidemiology"] | |
| def _person(rng): | |
| return f"{rng.choice(FIRST)} {rng.choice(LAST)}" | |
| def make_facts(rng) -> dict: | |
| kind = rng.choice(["company", "product", "person", "study"]) | |
| if kind == "company": | |
| f = { | |
| "kind": "company", | |
| "name": rng.choice(COMPANIES), | |
| "city": rng.choice(CITIES), | |
| "founded": rng.randint(1985, 2021), | |
| "employees": rng.choice([42, 85, 120, 240, 510, 900, 1300, 2600]), | |
| "revenue": rng.choice([3, 7, 12, 24, 55, 88, 140]), # $M | |
| "sector": rng.choice(["logistics", "consumer hardware", "food distribution", "fintech", "clean energy"]), | |
| "ceo": _person(rng), | |
| "growth": rng.choice([6, 11, 18, 27, 34]), # % YoY | |
| } | |
| elif kind == "product": | |
| f = { | |
| "kind": "product", | |
| "name": rng.choice(PRODUCTS), | |
| "maker": rng.choice(COMPANIES), | |
| "price": rng.choice([49, 89, 129, 199, 299, 449]), | |
| "battery": rng.choice([6, 9, 12, 18, 24, 36]), # hours | |
| "weight": rng.choice([120, 180, 240, 320, 460]), # grams | |
| "released": rng.choice(["March", "June", "September", "November"]), | |
| "year": rng.randint(2022, 2026), | |
| "color_count": rng.choice([2, 3, 4, 5]), | |
| } | |
| elif kind == "person": | |
| f = { | |
| "kind": "person", | |
| "name": _person(rng), | |
| "city": rng.choice(CITIES), | |
| "field": rng.choice(FIELDS), | |
| "years": rng.choice([4, 7, 11, 15, 20]), | |
| "degree": rng.choice(["a master's", "a PhD", "a bachelor's"]), | |
| "awards": rng.choice([0, 1, 2, 3]), | |
| "team": rng.choice([3, 6, 9, 14]), | |
| } | |
| else: | |
| f = { | |
| "kind": "study", | |
| "field": rng.choice(FIELDS), | |
| "n": rng.choice([48, 120, 350, 900, 2100]), | |
| "duration": rng.choice([6, 12, 18, 24, 36]), # months | |
| "effect": rng.choice([8, 14, 22, 31, 45]), # % change | |
| "city": rng.choice(CITIES), | |
| "lead": _person(rng), | |
| "year": rng.randint(2019, 2026), | |
| } | |
| return f | |
| # ---------------------------------------------------------------- source rendering | |
| def render_source(f, rng) -> str: | |
| k = f["kind"] | |
| if k == "company": | |
| return (f"{f['name']} is a {f['sector']} company based in {f['city']}, founded in {f['founded']}. " | |
| f"It employs around {f['employees']} people and reported annual revenue of about " | |
| f"${f['revenue']} million last year. {f['ceo']} serves as chief executive. " | |
| f"Revenue grew {f['growth']}% year over year.") | |
| if k == "product": | |
| return (f"{f['name'].capitalize()} is made by {f['maker']} and sells for ${f['price']}. " | |
| f"It offers up to {f['battery']} hours of battery life and weighs {f['weight']} grams. " | |
| f"It was released in {f['released']} {f['year']} and comes in {f['color_count']} colors.") | |
| if k == "person": | |
| return (f"{f['name']} is a researcher in {f['field']} based in {f['city']}, with {f['years']} years " | |
| f"of experience. They hold {f['degree']} degree and lead a team of {f['team']}. " | |
| f"They have received {f['awards']} industry awards.") | |
| return (f"A study in {f['field']} led by {f['lead']} followed {f['n']} participants over {f['duration']} months " | |
| f"in {f['city']}, completed in {f['year']}. It reported a {f['effect']}% change in the measured outcome.") | |
| # ---------------------------------------------------------------- grounded claims (often paraphrased) | |
| NUM_WORDS = {2: "two", 3: "three", 4: "four", 5: "five", 6: "six"} | |
| def grounded_claim(f, rng) -> str: | |
| k = f["kind"] | |
| opts = [] | |
| if k == "company": | |
| opts += [ | |
| f"The firm operates in the {f['sector']} space.", | |
| f"{f['name']} has its headquarters in {f['city']}.", | |
| f"The company has been around since {f['founded']}.", | |
| f"Its workforce numbers roughly {f['employees']}.", | |
| f"{f['ceo']} is the person at the top of {f['name']}.", | |
| f"Sales climbed by {f['growth']}% compared with the prior year.", | |
| f"Last year's turnover came in near ${f['revenue']} million.", | |
| f"The business has grown over {2026 - f['founded']} years of operation.", | |
| ] | |
| elif k == "product": | |
| opts += [ | |
| f"{f['maker']} is the manufacturer behind this device.", | |
| f"It carries a price tag of ${f['price']}.", | |
| f"On a full charge it can run for as long as {f['battery']} hours.", | |
| f"The unit tips the scales at {f['weight']} grams.", | |
| f"It first went on sale in {f['released']} of {f['year']}.", | |
| f"Buyers can choose from {NUM_WORDS.get(f['color_count'], f['color_count'])} color options.", | |
| f"It is a fairly light device at well under a kilogram.", | |
| ] | |
| elif k == "person": | |
| opts += [ | |
| f"Their work centers on {f['field']}.", | |
| f"They are based in {f['city']}.", | |
| f"They bring {f['years']} years of experience to the role.", | |
| f"They oversee a group of {f['team']} people.", | |
| f"They completed {f['degree']} degree.", | |
| (f"They have not won any industry awards." if f["awards"] == 0 | |
| else f"They have picked up {f['awards']} awards in the industry."), | |
| ] | |
| else: | |
| opts += [ | |
| f"The research focused on {f['field']}.", | |
| f"{f['lead']} headed the project.", | |
| f"It enrolled {f['n']} participants.", | |
| f"The work ran for {f['duration']} months.", | |
| f"Researchers observed a {f['effect']}% shift in the outcome.", | |
| f"The study took place in {f['city']}.", | |
| f"It wrapped up in {f['year']}.", | |
| ] | |
| return rng.choice(opts) | |
| # ---------------------------------------------------------------- not-grounded claims (plausible, reuse real entities) | |
| def _other(rng, pool, not_this): | |
| pool = [x for x in pool if x != not_this] | |
| return rng.choice(pool) | |
| def not_grounded_claim(f, rng) -> tuple[str, str]: | |
| """Return (claim, subtype). Subtypes: invented, altered_number, swapped_entity, contradicted, overgeneral.""" | |
| k = f["kind"] | |
| builders = [] | |
| if k == "company": | |
| builders += [ | |
| (f"{f['name']} recently opened a second office in {_other(rng, CITIES, f['city'])}.", "invented"), | |
| (f"The company employs about {rng.choice([x for x in [42,85,120,240,510,900,1300,2600] if x != f['employees']])} people.", "altered_number"), | |
| (f"{f['name']} is led by {_person(rng)}.", "swapped_entity"), | |
| (f"Revenue actually fell {f['growth']}% last year.", "contradicted"), | |
| (f"The firm is the largest {f['sector']} company in the country.", "overgeneral"), | |
| (f"It was founded in {rng.choice([x for x in range(1985,2022) if abs(x-f['founded'])>3])}.", "altered_number"), | |
| ] | |
| elif k == "product": | |
| builders += [ | |
| (f"{f['name'].capitalize()} is waterproof to 50 meters.", "invented"), | |
| (f"It costs ${_other(rng, [49,89,129,199,299,449], f['price'])}.", "altered_number"), | |
| (f"It is manufactured by {_other(rng, COMPANIES, f['maker'])}.", "swapped_entity"), | |
| (f"Battery life tops out at just {max(1, f['battery']-5)} hours.", "contradicted"), | |
| (f"It is the best-selling device in its category.", "overgeneral"), | |
| (f"It launched in {_other(rng, ['March','June','September','November'], f['released'])} {f['year']}.", "altered_number"), | |
| ] | |
| elif k == "person": | |
| builders += [ | |
| (f"They previously founded a startup in {_other(rng, CITIES, f['city'])}.", "invented"), | |
| (f"They manage a team of {_other(rng, [3,6,9,14], f['team'])}.", "altered_number"), | |
| (f"Their research is primarily in {_other(rng, FIELDS, f['field'])}.", "swapped_entity"), | |
| ((f"They have won {rng.choice([1,2,3])} awards." if f['awards']==0 | |
| else "They have never received any industry award."), "contradicted"), | |
| (f"They are among the most cited researchers in their field.", "overgeneral"), | |
| ] | |
| else: | |
| builders += [ | |
| (f"The study was funded by a government grant.", "invented"), | |
| (f"It followed {_other(rng, [48,120,350,900,2100], f['n'])} participants.", "altered_number"), | |
| (f"It was led by {_person(rng)}.", "swapped_entity"), | |
| (f"The intervention showed no measurable effect.", "contradicted"), | |
| (f"It is the largest study of its kind to date.", "overgeneral"), | |
| (f"The research was conducted in {_other(rng, CITIES, f['city'])}.", "swapped_entity"), | |
| ] | |
| return rng.choice(builders) | |
| def grounded_hard(f, rng): | |
| """Supported, but via inference/aggregation/paraphrase — low lexical overlap, looks 'novel'.""" | |
| k = f["kind"] | |
| opts = [] | |
| if k == "company": | |
| decades = (2026 - f["founded"]) // 10 | |
| opts += [ | |
| (f"The business has been operating for more than {max(1,decades)} decade" + ("s" if decades!=1 else "") + ".", "inference"), | |
| ("It is a mid-sized organization rather than a tiny startup." if f["employees"] >= 240 | |
| else "It remains a relatively small outfit.", "inference"), | |
| ("Revenue moved in a positive direction over the past year." if f["growth"] > 0 else "Revenue slipped over the year.", "paraphrase"), | |
| (f"Hundreds of staff are on its payroll." if f["employees"] >= 100 else "It has only a few dozen staff.", "inference"), | |
| ] | |
| elif k == "product": | |
| opts += [ | |
| ("It sits at the budget end of the range." if f["price"] <= 129 else "It is positioned as a premium option.", "inference"), | |
| ("A single charge comfortably lasts a full workday." if f["battery"] >= 9 else "Its battery barely covers a few hours.", "inference"), | |
| ("It is light enough to carry around easily." if f["weight"] <= 320 else "It is on the heavier side.", "inference"), | |
| ("It arrived on the market in the latter half of the year." if f["released"] in ("September","November") else "It launched in the first half of the year.", "inference"), | |
| ] | |
| elif k == "person": | |
| opts += [ | |
| ("They are a seasoned expert with over a decade in the field." if f["years"] >= 11 else "They are relatively early in their career.", "inference"), | |
| ("They have advanced academic training." if f["degree"] in ("a PhD","a master's") else "Their background is at the undergraduate level.", "paraphrase"), | |
| ("They manage a small group rather than a large department." if f["team"] <= 9 else "They run a sizeable team.", "inference"), | |
| ] | |
| else: | |
| opts += [ | |
| ("The trial ran for well over a year." if f["duration"] > 12 else "The trial was relatively short.", "inference"), | |
| ("It was a large-scale study by sample size." if f["n"] >= 900 else "It was a modest-sized study.", "inference"), | |
| ("The effect observed was substantial." if f["effect"] >= 22 else "The effect observed was fairly small.", "inference"), | |
| ] | |
| c, sub = rng.choice(opts) | |
| return c, sub | |
| def not_grounded_hard(f, rng): | |
| """Unsupported, but reuses real numbers/entities so lexical support stays high.""" | |
| k = f["kind"] | |
| opts = [] | |
| if k == "company": | |
| opts += [ | |
| (f"Its ${f['revenue']} million in revenue makes it the market leader in {f['sector']}.", "unsupported_relation"), | |
| (f"With {f['employees']} employees, it is the largest employer in {f['city']}.", "unsupported_relation"), | |
| (f"{f['ceo']} founded the company back in {f['founded']}.", "unsupported_relation"), | |
| (f"Its {f['growth']}% growth is the fastest in the {f['sector']} sector.", "unsupported_relation"), | |
| ] | |
| elif k == "product": | |
| opts += [ | |
| (f"At ${f['price']}, {f['name']} is the cheapest option on the market.", "unsupported_relation"), | |
| (f"Its {f['battery']}-hour battery is the longest in its class.", "unsupported_relation"), | |
| (f"{f['maker']} designed {f['name']} specifically for professional athletes.", "unsupported_relation"), | |
| (f"The {f['color_count']} color options were chosen after extensive customer surveys.", "unsupported_relation"), | |
| ] | |
| elif k == "person": | |
| opts += [ | |
| (f"Their {f['years']} years in {f['field']} make them the top authority in {f['city']}.", "unsupported_relation"), | |
| (f"They lead their team of {f['team']} remotely from abroad.", "unsupported_relation"), | |
| (f"Their {f['degree']} degree was earned at an Ivy League university.", "unsupported_relation"), | |
| ] | |
| else: | |
| opts += [ | |
| (f"The {f['effect']}% effect proves the intervention is cost-effective.", "unsupported_relation"), | |
| (f"{f['lead']} has run similar studies for over twenty years.", "unsupported_relation"), | |
| (f"The {f['n']} participants were all recruited from {f['city']} hospitals.", "unsupported_relation"), | |
| ] | |
| return rng.choice(opts) | |
| def make_example(idx: int, seed: int = 20261113) -> dict: | |
| rng = random.Random(seed * 7_000_003 + idx) | |
| f = make_facts(rng) | |
| source = render_source(f, rng) | |
| roll = rng.random() | |
| if roll < 0.32: | |
| claim, subtype, label = grounded_claim(f, rng), "grounded", 0 | |
| elif roll < 0.50: | |
| claim, subtype = grounded_hard(f, rng); label = 0 | |
| elif roll < 0.82: | |
| claim, subtype = not_grounded_claim(f, rng); label = 1 | |
| else: | |
| claim, subtype = not_grounded_hard(f, rng); label = 1 | |
| uid = hashlib.sha1(f"{seed}-{idx}".encode()).hexdigest()[:12] | |
| return { | |
| "id": f"grnd-{uid}", | |
| "domain": f["kind"], | |
| "source": source, | |
| "claim": claim, | |
| "label": label, # 1 = not grounded | |
| "subtype": subtype, | |
| } | |