Skip to content

Latest commit

 

History

History
89 lines (64 loc) · 3.05 KB

getting-started.md

File metadata and controls

89 lines (64 loc) · 3.05 KB

Getting started

This page takes you from an empty directory to your first date read from text and your first time written as words, in five steps. Each comment after a print shows the output of a run in a fresh environment.

Step 1: Install

Make an environment and install the package. It needs no voice assistant.

uv venv
uv pip install --prerelease=allow ovos-date-parser

Run each snippet of this page with uv run python, or with .venv/bin/python. Save a snippet to a file and pass the file name, or paste it into the prompt.

Step 2: Read a date from text

extract_datetime gives the datetime that a phrase names and the words that are left over. A phrase like "next friday" counts from anchorDate. Pass a fixed anchor to get the same answer on every run.

from datetime import datetime
from ovos_date_parser import extract_datetime

anchor = datetime(2024, 1, 5)
when, leftover = extract_datetime("lets meet next friday at 8am", "en", anchorDate=anchor)
print(when)  # 2024-01-12 08:00:00
print(leftover)  # lets meet

Step 3: Read a duration from text

extract_duration gives a timedelta and the words that are left over.

from ovos_date_parser import extract_duration

delta, leftover = extract_duration("set a timer for 5 minutes", "en")
print(delta)  # 0:05:00
print(leftover)  # set a timer

Step 4: Write a time, a date and a duration as words

nice_time, nice_date and nice_duration give words that a speech engine can say.

from datetime import datetime
from ovos_date_parser import nice_time, nice_date, nice_duration

print(nice_time(datetime(2024, 1, 5, 15, 30), "en"))  # half past three
print(nice_date(datetime(2024, 1, 5), "en"))  # friday, january fifth, twenty twenty four
print(nice_duration(3690, "en"))  # one hour one minute thirty seconds

Step 5: Change the language

Each call takes a language code as its second argument. Reading and writing cover different sets of languages, and languages.md lists both.

from datetime import datetime
from ovos_date_parser import extract_datetime, nice_time

print(nice_time(datetime(2024, 1, 5, 15, 30), "de"))  # halb vier
print(extract_datetime("amanhã às 15h30", "pt", anchorDate=datetime(2024, 1, 5)))  # [datetime.datetime(2024, 1, 6, 15, 30), '']

Where to go next

How it works shows which calls pass to chronologia and which code writes each language. The API reference lists every function with its parameters.


Index · ← Home · Adding a language →