diff --git a/app.py b/app.py index ad190d8..3f60b55 100644 --- a/app.py +++ b/app.py @@ -1570,7 +1570,12 @@ def parse_mixed_rules(text, status): "Words like nanage, nannagai, nanna, nanu, ondu, alli, hatra, mujhe, mera, meri, ek, mein, paas mean " "me, my, one, in or near: they are never part of an item or a place name. " "The campus places are: " + ", ".join(sorted({p["short"] for p in campus.CAMPUS["places"]})) + ". " - "Departments, labs, the library, the bank and offices are in the Main Block. When the student names " + "Other names for them: garage or mechanical parking is Mech Parking; mechanical block, mech department, " + "robotics or RAI is Mech Blocks; the library, labs, CSE, ISE, ECE, EEE, civil and AI/ML departments, " + "quadrangle, admin office, principal, placement cell, seminar hall and the bank are in the Main Block; " + "chemistry or physics is Chem/Phy; MCA is MBA; workshop is Workshops; Upahara Darshini is KIMS Canteen; " + "the ATM is ATM; plain \"canteen\" may be Canteen, Puff shop or Nandini, so leave it as Canteen. " + "When the student names " "one of these places, however it is spelt or in whatever language (e.g. मेक पार्किंग is Mech Parking, " "ಕ್ಯಾಂಟೀನ್ is Canteen), write that place's name exactly as listed. " ) @@ -1581,7 +1586,9 @@ def parse_mixed_rules(text, status): '"description": one or two English sentences for whoever might own it: the item, any details the ' "student gave (colour, brand, size, marks, what was with it) and where it was found or lost, e.g. " '"A black notebook found in the computer science lab." Not in the first person, ' - f'"category": one of {REPORT_CATEGORIES}, "location": the place on campus in English or "", ' + f'"category": one of {REPORT_CATEGORIES}, ' + '"location": where exactly, in English, in the student\'s words, e.g. "CS lab, 3rd floor", or "", ' + '"place": the campus place from the list that is in, or "", ' '"status": "Lost" or "Found"}. Do not invent details.' ) @@ -1828,9 +1835,26 @@ def clean_parsed_report(fields, text): if named: result["category"] = named result["status"] = fields.get("status") if fields.get("status") in {"Lost", "Found"} else fallback["status"] + # When the words alone don't point to a place but the model named one from the + # list, add that name, so the map picker and the matcher both see it. + named = place_by_name(fields.get("place")) + if named and not campus.find_places(result["location"]): + spot = result["location"].strip() + result["location"] = (f"{spot} ({campus.place_short(named)})" if spot else campus.place_short(named))[:120] return result +def place_by_name(name): + """The place id for a name from the campus list (short or full name), or None.""" + if not isinstance(name, str) or not name.strip(): + return None + wanted = campus.normalise(name) + for place in campus.CAMPUS["places"]: + if wanted in (campus.normalise(place["short"]), campus.normalise(place["name"])): + return place["id"] + return None + + @app.post("/report/parse") @login_required def parse_report(): @@ -1910,7 +1934,8 @@ def parse_retrace(): "gave (colour, brand, size, marks) and where it was found or lost, e.g. \"A black notebook found in the " 'computer science lab.\" Not in the first person, ' f'"category": one of {REPORT_CATEGORIES}, ' - '"location": where the item was, in English, or "", ' + '"location": where exactly, in English, in their words, e.g. "CS lab, 3rd floor", or "", ' + '"place": the campus place from the list that is in, or "", ' '"places": every campus place they mention, by its name from the list, in the order they went there, ' '"when": "today", "yesterday" or "week"}. Do not invent anything they did not say.' ) diff --git a/tests/test_app.py b/tests/test_app.py index 3f8cb5a..f7a6b7e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1009,6 +1009,22 @@ def test_the_prompts_list_the_campus_places(self): self.assertIn("Mech Parking", prompt) self.assertIn("nannagai", prompt) + def test_the_models_place_is_kept_when_the_words_are_vague(self): + fields = clean_parsed_report({"title": "Bottle", "description": "A bottle.", "category": "Accessories", + "location": "near the bikes", "place": "Mech Parking", "status": "Lost"}, "x") + self.assertEqual(fields["location"], "near the bikes (Mech Parking)") + fields = clean_parsed_report({"title": "Pen", "location": "CS lab, 3rd floor", "place": "Main Block", + "status": "Found", "category": "Stationery"}, "x") + self.assertEqual(fields["location"], "CS lab, 3rd floor") + fields = clean_parsed_report({"title": "Pen", "location": "", "place": "Made-up Hall", + "status": "Found", "category": "Stationery"}, "lost a pen") + self.assertNotIn("Made-up", fields["location"]) + + def test_the_prompts_teach_the_other_names(self): + for prompt in (app_module.REPORT_PROMPT, app_module.VOICE_PROMPT): + self.assertIn("mechanical parking is Mech Parking", prompt) + self.assertIn('"place"', prompt) + def test_hinglish_and_kanglish_without_a_model(self): found = parse_report_rules("Mujhe computer lab Mein Ek key Mili Hai") self.assertEqual((found["title"], found["status"], found["category"]), ("Key", "Found", "Keys"))