THINK FIRST·CODE LATER

← All labs

A tiny natural-language-to-chart translator

Problem

Build a rule-based NL2VIS translator to understand what such systems must decide. Input: schema field:T|N|O|Q …, then one question per line (lower-case it).

Rules: the measures are quantitative fields mentioned in the question (whole words), the groups are mentioned non-quantitative fields (in schema order). The aggregate is the first keyword found in the order: average/mean → mean, total/sum → sum, "number of"/count → count, maximum/max → max, minimum → min, median → median; default mean. If there is no measure and the aggregate is not count, print CLARIFY: which quantity? (one of pm25, temperature); if there is no group, CLARIFY: by which attribute? (one of date, district). Otherwise x = first group; mark = line if x is temporal or the question contains "trend" or "over", else bar; y = count() or agg(measure); colour = second group if any. A question ending with for WORD (WORD not a field) filters the first nominal field to WORD in title case.

Build the chart with Altair and print Q: <question> and bar: x=district:N, y=mean(pm25):Q (channels sorted, aggregates written agg(field) or count(), type initial), with | filter (datum['district'] === 'Lucheng') when filtered.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v12-1-data-1.txt

schema date:T district:N pm25:Q temperature:Q
average pm25 by district
trend of pm25 over date by district
maximum temperature by district for lucheng
show me the data
average pm25
number of readings by district
What your program must print for v12-1-data-1.txt
Q: average pm25 by district
   bar: x=district:N, y=mean(pm25):Q
Q: trend of pm25 over date by district
   line: color=district:N, x=date:T, y=mean(pm25):Q
Q: maximum temperature by district for lucheng
   bar: x=district:N, y=max(temperature):Q | filter (datum['district'] === 'Lucheng')
Q: show me the data
   CLARIFY: which quantity? (one of pm25, temperature)
Q: average pm25
   CLARIFY: by which attribute? (one of date, district)
Q: number of readings by district
   bar: x=district:N, y=count():Q

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v12-1-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v12-1-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v12-1-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.