跳到正文
DEV Community· StagingBench Team·· 56 分鐘前AI 評分42

如何把自由文字失敗訊息轉化為原因統計

Turn a free-text failure message into counted reasons

AI 導讀

解析流程以 Python 標準庫讀取 framing_reasons.csv,從標記為 False 的失敗訊息中,為每筆記錄整理重疊、縮放、旋轉及新增畫面區域四類原因。24 筆失敗中,8 筆有一項原因、9 筆有兩項、5 筆有三項、2 筆有四項;各類原因分別出現 18、14、4 及 13 次。計數只反映訊息記錄的原因,不會重新套用通過門檻;另有 26 筆沒有 framing 判定的記錄不納入統計。

正文

A free-text failure message can contain more structure than its format suggests. Semicolons separate observations, recurring phrases identify criteria, and numbers describe the measured values. Extracting that structure makes it possible to distinguish a failed record from the several reasons attached to it.

The published benchmark links its broader records through the downloadable results. For this exercise, the working input is framing_reasons.csv, a flat export with the columns tool, room, framing_pass and framing.

Decide what the counter measures

Count recorded reasons rather than attempting to reconstruct the underlying measurements. That distinction matters because the messages contain displayed values, which may not preserve the precision used for the original judgment.

Map the recurring phrases to four categories:

  • "only X% of the original frame visible" means overlap.
  • "zoom" followed by a number means zoom.
  • "rotated" followed by a signed number means rotation.
  • A percentage "outside the original frame" means invented area.

A record can match several patterns. The parser should therefore collect a set of categories for each row, not stop after the first match.

Filter on the exact text "False" in framing_pass. Testing whether the field is merely nonempty would also retain "True", because both strings contain characters. Empty fields belong to the unknown category and should not enter the failure reason counts.

Run the parser

Save or run this code in the directory containing framing_reasons.csv. It uses only standard library modules and leaves the input unchanged.

import csv
import re
from collections import Counter

patterns = {
    'overlap': r'only ([0-9.]+)% of the original frame visible',
    'zoom': r'zoom ([0-9.]+)',
    'rotation': r'rotated (-?[0-9.]+) deg',
    'invented area': r'([0-9.]+)% of the output is outside the original frame',
}
distribution, criteria, singles = Counter(), Counter(), Counter()
with open('framing_reasons.csv', newline='') as source:
    for row in csv.DictReader(source):
        if row['framing_pass'] != 'False':
            continue
        reasons = {name for name, pattern in patterns.items()
                   if re.search(pattern, row['framing'])}
        if not reasons:
            raise ValueError('Unparsed failure message')
        distribution[len(reasons)] += 1
        criteria.update(reasons)
        if len(reasons) == 1:
            singles.update(reasons)

print('Reasons per record:', dict(sorted(distribution.items())))
print('Criteria:', dict(sorted(criteria.items())))
print('Single reasons:', dict(sorted(singles.items())))

The actual output is:

Reasons per record: {1: 8, 2: 9, 3: 5, 4: 2}
Criteria: {'invented area': 13, 'overlap': 18, 'rotation': 4, 'zoom': 14}
Single reasons: {'overlap': 7, 'zoom': 1}

Read the counters correctly

The distribution counts records. Eight failures mention a single criterion, nine mention two, five mention three, and two mention four. Those frequencies sum to 24 failed records.

The criterion counter answers a different question: how many failed records mention each criterion? Overlap appears in 18, zoom in 14, invented area in 13, and rotation in four. Their sum is 49 because the categories coexist within records.

The final counter isolates failures with exactly one recorded reason. Seven mention only overlap, and one mentions only zoom. Rotation and invented area do not appear in this output because neither occurs alone. An absent counter key means no matching single reason record was counted.

Using a set prevents repeated wording within a message from counting the same criterion more than once. Updating the criterion counter with that set adds one occurrence for each category present in the record.

Keep parsing separate from threshold checking

The published pass conditions require at least 97% overlap, zoom from 0.99 to 1.02, rotation of at most 0.5 degrees in magnitude, and at most 2% invented area.

The snippet does not apply those thresholds. It counts which reasons the messages name. This preserves the distinction between reporting the recorded explanation and independently reassessing a displayed number.

Check what the parser leaves unresolved

The unmatched message check catches a failed row with no recognized reasons. It does not guarantee that every clause in a partly recognized message was understood. If the message format changes, inspect unfamiliar wording before extending the patterns.

The patterns also assume the decimal notation and phrases used in these records. They are intentionally small and readable. Generalizing them without examples could hide differences that deserve examination.

Finally, keep unknown records outside this analysis. Across the full CSV, 26 records have no framing verdict following failed alignment. The parser neither labels them successful nor treats them as additional failures. Its result is a count of recorded reasons within the 24 explicit failures.

來源:DEV Community · dev.to