Lingua Franca
Lingua Franca is our multilingual language parsing and formatting library. This is what allows Mycroft to both understand and respond with naturally expressed dates, numbers and times.
Lingua Franca (noun)
a framework that is adopted as the common language between speakers with different native tongues

Supported Languages

Currently supported languages include:
    English
    French
    German
    Hungarian
    Italian
    Portuguese
    Swedish
There is also active progress being made on:
    Danish
    Dutch
    Spanish

Formatting

Convert data into spoken equivalents

Pronounce numbers

spoken versions of numbers
1
from lingua_franca.format import nice_number, pronounce_number
2
3
assert nice_number(25/6) == "4 and a sixth"
4
assert nice_number(201) == "201"
5
assert nice_number(3.14159269) == "3 and a seventh"
6
7
assert pronounce_number(3.14159269) == "three point one four"
8
assert pronounce_number(0) == "zero"
9
assert pronounce_number(10) == "ten"
10
assert pronounce_number(201) == "two hundred and one"
11
assert pronounce_number(102.3) == "one hundred and two point three"
12
assert pronounce_number(
13
4092949192) == "four billion, ninety two million, nine hundred and forty nine thousand, one hundred and ninety two"
14
15
assert pronounce_number(100034000000299792458, short_scale=True) == \
16
"one hundred quintillion, thirty four quadrillion, " \
17
"two hundred and ninety nine million, seven hundred and ninety " \
18
"two thousand, four hundred and fifty eight"
19
20
assert pronounce_number(100034000000299792458, short_scale=False) == \
21
"one hundred trillion, thirty four thousand billion, " \
22
"two hundred and ninety nine million, seven hundred and ninety " \
23
"two thousand, four hundred and fifty eight"
Copied!

Pronounce datetime objects

spoken date for datetime.datetime objects
1
from lingua_franca.format import nice_date, nice_date_time, nice_time
2
import datetime
3
4
dt = datetime.datetime(2017, 1, 31, 13, 22, 3)
5
6
assert nice_date(dt) == "tuesday, january thirty-first, twenty seventeen"
7
8
assert nice_time(dt) == "one twenty two"
9
assert nice_time(dt, use_ampm=True) == "one twenty two p.m."
10
assert nice_time(dt, speech=False) == "1:22"
11
assert nice_time(dt, speech=False, use_ampm=True) == "1:22 PM"
12
assert nice_time(dt, speech=False, use_24hour=True) == "13:22"
13
assert nice_time(dt, speech=False, use_24hour=True, use_ampm=True) == "13:22"
14
assert nice_time(dt, use_24hour=True, use_ampm=True) == "thirteen twenty two"
15
assert nice_time(dt, use_24hour=True, use_ampm=False) == "thirteen twenty two"
16
17
assert nice_date_time(dt) == "tuesday, january thirty-first, twenty seventeen at one twenty two"
Copied!

Pronounce durations

spoken number of seconds or datetime.timedelta objects
1
from lingua_franca.format import nice_duration
2
3
4
assert nice_duration(1) == "one second"
5
assert nice_duration(3) == "three seconds"
6
assert nice_duration(1, speech=False) == "0:01"
7
assert nice_duration(61), "one minute one second"
8
assert nice_duration(61, speech=False) == "1:01"
9
assert nice_duration(5000) == "one hour twenty three minutes twenty seconds"
10
assert nice_duration(5000, speech=False), "1:23:20"
11
assert nice_duration(50000) == "thirteen hours fifty three minutes twenty seconds"
12
assert nice_duration(50000, speech=False) == "13:53:20"
13
assert nice_duration(500000) == "five days eighteen hours fifty three minutes twenty seconds"
14
assert nice_duration(500000, speech=False), "5d 18:53:20"
15
16
from datetime import timedelta
17
18
assert nice_duration(timedelta(seconds=500000), speech=False) == "5d 18:53:20"
Copied!

Parsing

Extract data from natural language text

Extract numbers

1
from lingua_franca.parse import extract_number, extract_numbers
2
3
# extract a number
4
assert extract_number("nothing") is False
5
assert extract_number("two million five hundred thousand tons of spinning "
6
"metal") == 2500000
7
assert extract_number("six trillion") == 6000000000000.0
8
assert extract_number("six trillion", short_scale=False) == 6e+18
9
10
assert extract_number("1 and 3/4 cups") == 1.75
11
assert extract_number("1 cup and a half") == 1.5
12
13
## extracts all numbers
14
assert extract_numbers("nothing") == []
15
assert extract_numbers("this is a one twenty one test") == [1.0, 21.0]
16
assert extract_numbers("1 dog, seven pigs, macdonald had a farm, "
17
"3 times 5 macarena") == [1, 7, 3, 5]
Copied!

Extract durations

extract datetime.timedelta objects
1
## extract durations
2
from lingua_franca.parse import extract_duration
3
from datetime import timedelta
4
5
assert extract_duration("nothing") == (None, 'nothing')
6
7
assert extract_duration("Nineteen minutes past the hour") == (
8
timedelta(minutes=19),
9
"past the hour")
10
assert extract_duration("wake me up in three weeks, four hundred ninety seven"
11
" days, and three hundred 91.6 seconds") == (
12
timedelta(weeks=3, days=497, seconds=391.6),
13
"wake me up in , , and")
14
assert extract_duration(
15
"The movie is one hour, fifty seven and a half minutes long") == (
16
timedelta(hours=1, minutes=57.5),
17
"the movie is , long")
Copied!

Extract dates

extract datetime.datetime objects
1
## extract date times
2
from datetime import datetime
3
from lingua_franca.parse import extract_datetime, normalize
4
5
def extractWithFormat(text):
6
date = datetime(2017, 6, 27, 13, 4) # Tue June 27, 2017 @ 1:04pm
7
[extractedDate, leftover] = extract_datetime(text, date)
8
extractedDate = extractedDate.strftime("%Y-%m-%d %H:%M:%S")
9
return [extractedDate, leftover]
10
11
12
def testExtract(text, expected_date, expected_leftover):
13
res = extractWithFormat(normalize(text))
14
assert res[0] == expected_date
15
assert res[1] == expected_leftover
16
17
18
testExtract("now is the time",
19
"2017-06-27 13:04:00", "is time")
20
testExtract("in a couple minutes",
21
"2017-06-27 13:06:00", "")
22
testExtract("What is the day after tomorrow's weather?",
23
"2017-06-29 00:00:00", "what is weather")
24
testExtract("Remind me at 10:45 pm",
25
"2017-06-27 22:45:00", "remind me")
26
testExtract("what is the weather on friday morning",
27
"2017-06-30 08:00:00", "what is weather")
28
testExtract("what is tomorrow's weather",
29
"2017-06-28 00:00:00", "what is weather")
30
testExtract("remind me to call mom next tuesday",
31
"2017-07-04 00:00:00", "remind me to call mom")
32
testExtract("remind me to call mom in 3 weeks",
33
"2017-07-18 00:00:00", "remind me to call mom")
34
testExtract("set an alarm for tonight 9:30",
35
"2017-06-27 21:30:00", "set alarm")
36
testExtract("on the evening of june 5th 2017 remind me to call my mother",
37
"2017-06-05 19:00:00", "remind me to call my mother")
Copied!
Last modified 2yr ago