Esta página foi traduzida por máquina e ainda não foi revisada por uma pessoa. O inglês é a referência; correções são bem-vindas no GitHub. GitHub
Cookwala / SDK e cenários / 078
Arabic-Indic and Persian digits work in every command
Phones set to an Arabic or Persian keyboard send different digit characters. The grammar accepts them wherever a digit may appear, in quantities, temperatures, dates and counts.
Leia primeiro: /docs/HUMANITARIAN-PROFILE/ /humanitarian/
Passos
An offer with Arabic-Indic quantity and temperature.
parseSmsA handover with Arabic-Indic kilograms.
parseSmsA farm offer with Persian digits in the quantity and the harvest date.
parseSmsA distribution with Arabic-Indic counts.
parseSms
Resultado
You know a program can run on the phones people already have, in the digits they already type. Next: scenario 002 walks through a full SMS day.
Linha de comando
cookwala sms "OFFER ٣٦KG YOGURT C ٤C UB0511"
cookwala sms "FARM ۱۲۰KG TOMATO A HV۰۴۱۱"Saída gravada
Status: ok
[1] parseSms: {"ok": true, "command": "OFFER", "kg": 36.0, "item": "yogurt", "storage": "chilled", "tempC": 4.0, "dateMark": {"kind": "use_by", "dayMonth": "05-11"}, "foodClasses": ["dairy"], "findings": []}
[2] parseSms: {"ok": true, "command": "HAND", "offer": "A7K", "kgAccepted": 30.0, "kgRejected": 6.0, "reason": "packaging_damaged", "reasonCode": "PACK", "tempC": 4.6, "findings": []}
[3] parseSms: {"ok": true, "command": "OFFER", "kg": 120.0, "item": "tomato", "storage": "ambient", "origin": "farm", "harvested": "04-11", "findings": []}
[4] parseSms: {"ok": true, "command": "DIST", "meals": 410, "people": 410, "kg": 96.0, "findings": []}Código
Executar: python scenarios/out/078-arabic-and-persian-digits-in-messages/python.py
# Scenario 078: Arabic-Indic and Persian digits work in every command
# Phones set to an Arabic or Persian keyboard send different digit characters. The grammar accepts them wherever a digit may appear, in quantities, temperatures, dates and counts.
# Run a hub first: python hub/cookwala_hub.py --recipes examples
import json, os
from cookwala.client import CookwalaClient, CookwalaProblem
def load(path):
with open(path, encoding="utf-8") as f:
return json.load(f)
def pick(obj, path):
for part in path.replace("]", "").replace("[", ".").split("."):
obj = obj[int(part)] if part.isdigit() else (len(obj) if part == "length" else obj.get(part))
return obj
c = CookwalaClient(os.environ.get("COOKWALA_HUB", "http://localhost:7878"))
# Step 1: An offer with Arabic-Indic quantity and temperature.
offer = c.parse_sms("OFFER ٣٦KG YOGURT C ٤C UB0511")
assert pick(offer, "ok") == True, pick(offer, "ok")
print("ok", "ok", "=", True)
assert pick(offer, "kg") == 36.0, pick(offer, "kg")
print("ok", "kg", "=", 36.0)
assert pick(offer, "tempC") == 4.0, pick(offer, "tempC")
print("ok", "tempC", "=", 4.0)
assert pick(offer, "storage") == "chilled", pick(offer, "storage")
print("ok", "storage", "=", "chilled")
# Step 2: A handover with Arabic-Indic kilograms.
hand = c.parse_sms("HAND A7K ٣٠ REJ ٦ PACK T4.6")
assert pick(hand, "kgAccepted") == 30.0, pick(hand, "kgAccepted")
print("ok", "kgAccepted", "=", 30.0)
assert pick(hand, "kgRejected") == 6.0, pick(hand, "kgRejected")
print("ok", "kgRejected", "=", 6.0)
assert pick(hand, "reason") == "packaging_damaged", pick(hand, "reason")
print("ok", "reason", "=", "packaging_damaged")
# Step 3: A farm offer with Persian digits in the quantity and the harvest date.
farm = c.parse_sms("FARM ۱۲۰KG TOMATO A HV۰۴۱۱")
assert pick(farm, "kg") == 120.0, pick(farm, "kg")
print("ok", "kg", "=", 120.0)
assert pick(farm, "harvested") == "04-11", pick(farm, "harvested")
print("ok", "harvested", "=", "04-11")
assert pick(farm, "origin") == "farm", pick(farm, "origin")
print("ok", "origin", "=", "farm")
# Step 4: A distribution with Arabic-Indic counts.
dist = c.parse_sms("DIST ٤١٠ MEALS ٤١٠ PEOPLE ٩٦KG")
assert pick(dist, "meals") == 410, pick(dist, "meals")
print("ok", "meals", "=", 410)
assert pick(dist, "people") == 410, pick(dist, "people")
print("ok", "people", "=", 410)
assert pick(dist, "kg") == 96.0, pick(dist, "kg")
print("ok", "kg", "=", 96.0)
print("scenario complete")