DEV Community

StagingBench Team
StagingBench Team

Posted on AI-assisted

Parse free-text policy fields without guessing

A policy sentence can contain a useful number without becoming a structured policy. Consider "30-day money-back guarantee" and "Unused credits refundable within 30 days of purchase". Both contain a duration. The surrounding words describe different conditions.

The refund policy entries in StagingBench provide a small example for learning how to extract a value while preserving uncertainty. There are 12 tools, 9 recorded refund policy entries and 3 empty refund policy entries. A parser should keep those empty entries distinguishable from text that contains no matching duration.

Define the extraction rule

For this tutorial, extract the first number matching either a number followed by "-day" or " day", or the phrase "within" followed by a number and " days".

The regular expression is r"(\d+)[- ]day|within (\d+) days". The \d+ expression matches digits. The parentheses capture the digits so they can be converted into an integer. The vertical bar separates the two alternatives.

This is a narrow text rule. It does not identify the legal meaning of a refund period, determine eligibility or collect every duration in a statement. Defining that scope before writing the code helps prevent the extracted number from acquiring a meaning the parser never established.

Read and classify each value

Save the following as a script and run it in a directory containing policies.csv, a flat export of the per tool pricing fields from results.json with a tool and a refund_policy column. It uses only the standard library, reads the CSV and prints one classification per tool.

import csv
import re

DAY_PATTERN = re.compile(r"(\d+)[- ]day|within (\d+) days")

def refund_days(value):
    if value is None or value == "":
        return None
    match = DAY_PATTERN.search(value)
    if match is None:
        return "text, no days"
    return int(next(group for group in match.groups()
                    if group is not None))

with open("policies.csv", encoding="utf-8", newline="") as source:
    for row in csv.DictReader(source):
        print(row["tool"], refund_days(row["refund_policy"]))
Enter fullscreen mode Exit fullscreen mode

Using csv.DictReader matters because policy text can contain commas inside a quoted field. Splitting each line on commas would fail to respect that structure.

The helper returns three kinds of result. An integer means the expression found a duration. None preserves a missing value. "text, no days" means policy text exists, but the expression found no match.

Applied to all 12 published refund entries, the totals are:

  • Days extracted: 8 tools
  • Text without days: 1 tool
  • Empty: 3 tools

These categories account for all 12 rows. They also preserve the distinction between the 9 recorded policies and the 3 empty entries.

Understand what the match loses

One recorded statement contains "within 30 days of first purchase or 7 days of a renewal". The snippet returns the first match, 30. It does not capture the separate renewal period or attach either number to its condition.

A "30-day" statement also produces a number, but that extraction does not retain whether the statement applies only to the first payment. Likewise, extracting a duration from wording about unused credits does not establish that every payment qualifies for a refund.

Keep the original text available alongside any derived value. The integer is an aid to inspection. Replacing the policy sentence with that integer would discard conditions the parser has not represented.

Keep None meaningful

An empty refund entry must remain None. Converting it to zero would imply a duration that was never recorded. Converting it to "text, no days" would falsely suggest that a policy statement had been inspected and contained no matching days.

The text category is also deliberately limited. It means the chosen expression found no duration. It does not mean the policy has no conditions or that refunds are always available. A cancellation statement can contain substantial policy information without containing any number of days.

Add a few focused tests

A small test can call refund_days with a handful of strings and compare each result with an expected value using assert.

Use an empty string expecting None, "30-day money-back guarantee" expecting 30, "Refund within 7 days" expecting 7, and "No refunds" expecting "text, no days". Add a sentence containing two periods to confirm that the current function intentionally returns the first match.

Those checks document the helper's behavior. They do not prove that the expression understands policies. Before using extracted durations for anything beyond inspection, return to the wording and the dated sources described in the methodology.

Top comments (0)