Skip to content

Commit

Permalink
Add datetime subtypes and geo entities (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemDoum authored Jul 12, 2019
1 parent 04784c4 commit ac356ff
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 48 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
### Changed
- Bump `snips-nlu-ontology` to `0.65.0` [#26](https://github.com/snipsco/snips-nlu-parsers/pull/26)
- Bump `rustling-ontology` to `0.19.0` [#26](https://github.com/snipsco/snips-nlu-parsers/pull/26)

## [0.2.3] - 2019-07-10
### Changed
- Bump `snips-nlu-ontology` to `0.64.8`
Expand Down Expand Up @@ -35,6 +40,7 @@ All notable changes to this project will be documented in this file.
- bump `snips-nlu-ontology` to `0.63.0`
- re-export `gazetteer-entity-parser` crate

[Unreleased]: https://github.com/snipsco/snips-nlu-parsers/compare/0.2.3...HEAD
[0.2.3]: https://github.com/snipsco/snips-nlu-parsers/compare/0.2.2...0.2.3
[0.2.2]: https://github.com/snipsco/snips-nlu-parsers/compare/0.2.1...0.2.2
[0.2.1]: https://github.com/snipsco/snips-nlu-parsers/compare/0.2.0...0.2.1
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "snips-nlu-parsers"
version = "0.2.3"
version = "0.3.0-SNAPSHOT"
authors = ["Adrien Ball <[email protected]>"]
edition = "2018"

Expand All @@ -21,8 +21,8 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
gazetteer-entity-parser = { git = "https://github.com/snipsco/gazetteer-entity-parser", tag = "0.7.0" }
rustling-ontology = { git = "https://github.com/snipsco/rustling-ontology", tag = "0.18.1" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
rustling-ontology = { git = "https://github.com/snipsco/rustling-ontology", tag = "0.19.0" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
snips-nlu-utils = { git = "https://github.com/snipsco/snips-nlu-utils", tag = "0.8.0" }

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "snips-nlu-parsers-ffi"
version = "0.2.3"
version = "0.3.0-SNAPSHOT"
authors = ["Adrien Ball <[email protected]>"]
edition = "2018"

[dependencies]
failure = "0.1"
ffi-utils = { git = "https://github.com/snipsco/snips-utils-rs", rev = "291ce1d" }
libc = "0.2"
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
snips-nlu-parsers-ffi-macros = { path = "ffi-macros" }

[lib]
Expand Down
6 changes: 3 additions & 3 deletions ffi/ffi-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "snips-nlu-parsers-ffi-macros"
version = "0.2.3"
version = "0.3.0-SNAPSHOT"
authors = ["Adrien Ball <[email protected]>"]
edition = "2018"

Expand All @@ -10,8 +10,8 @@ ffi-utils = { git = "https://github.com/snipsco/snips-utils-rs", rev = "291ce1d"
libc = "0.2"
serde = "1.0"
serde_json = "1.0"
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
snips-nlu-parsers = { path = "../.." }

[lib]
Expand Down
8 changes: 4 additions & 4 deletions python/ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "snips-nlu-parsers-python-ffi"
version = "0.2.3"
version = "0.3.0-SNAPSHOT"
authors = ["Adrien Ball <[email protected]>"]
edition = "2018"

Expand All @@ -12,6 +12,6 @@ crate-type = ["cdylib"]
failure = "0.1"
libc = "0.2"
ffi-utils = { git = "https://github.com/snipsco/snips-utils-rs", rev = "291ce1d" }
snips-nlu-parsers-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-parsers", tag = "0.2.3" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.8" }
snips-nlu-parsers-ffi-macros = { path = "../../ffi/ffi-macros" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.65.0" }
2 changes: 1 addition & 1 deletion python/snips_nlu_parsers/__version__
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.3
0.3.0-SNAPSHOT
68 changes: 58 additions & 10 deletions src/builtin_entity_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ impl BuiltinEntityParserLoader {
Some(parser_path) => Some(GazetteerParser::from_path(parser_path)?),
None => None,
};

Ok(BuiltinEntityParser {
gazetteer_parser,
rustling_parser,
Expand Down Expand Up @@ -99,7 +98,6 @@ impl BuiltinEntityParser {
})
.flat_map(|kind| kind.try_ontology_into().ok())
.collect::<Vec<OutputKind>>();

let rustling_entities = if rustling_output_kinds.is_empty() {
vec![]
} else {
Expand Down Expand Up @@ -277,15 +275,65 @@ mod test {
fn test_entities_extraction() {
let parser = BuiltinEntityParserLoader::new(Language::EN).load().unwrap();
assert_eq!(
vec![BuiltinEntityKind::Number, BuiltinEntityKind::Time],
vec![BuiltinEntityKind::Number, BuiltinEntityKind::Date],
parser
.extract_entities("Book me a restaurant for two people tomorrow", None)
.unwrap()
.iter()
.map(|e| e.entity_kind)
.collect_vec()
);

assert_eq!(
vec![BuiltinEntityKind::Datetime],
parser
.extract_entities("Book me restaurant for two people tomorrow", None)
.extract_entities("Book me a restaurant for tomorrow", Some(&[BuiltinEntityKind::Datetime]))
.unwrap()
.iter()
.map(|e| e.entity_kind)
.collect_vec()
);

assert_eq!(
vec![BuiltinEntityKind::Datetime],
parser
.extract_entities("Book me a restaurant for tomorrow at 8pm", None)
.unwrap()
.iter()
.map(|e| e.entity_kind)
.collect_vec()
);

assert_eq!(
vec![BuiltinEntityKind::Date],
parser
.extract_entities("Book me a restaurant for tomorrow", Some(&[BuiltinEntityKind::Date]))
.unwrap()
.iter()
.map(|e| e.entity_kind)
.collect_vec()
);

assert_eq!(
vec![BuiltinEntityKind::TimePeriod],
parser
.extract_entities("Book the meeting room from 10am to 11am", None)
.unwrap()
.iter()
.map(|e| e.entity_kind)
.collect_vec()
);

assert_eq!(
vec![BuiltinEntityKind::Time, BuiltinEntityKind::Time],
parser
.extract_entities("Book the meeting room from 10am to 11am", Some(&[BuiltinEntityKind::Time]))
.unwrap()
.iter()
.map(|e| e.entity_kind)
.collect_vec()
);

assert_eq!(
vec![BuiltinEntityKind::Duration],
parser
Expand Down Expand Up @@ -416,7 +464,7 @@ mod test {
#[test]
fn test_entities_extraction_for_non_space_separated_languages() {
let parser = BuiltinEntityParserLoader::new(Language::JA).load().unwrap();
let expected_time_value = InstantTimeValue {
let expected_datetime_value = InstantTimeValue {
value: "2013-02-10 00:00:00 +01:00".to_string(),
grain: Grain::Day,
precision: Precision::Exact,
Expand All @@ -425,8 +473,8 @@ mod test {
let expected_entity = BuiltinEntity {
value: "二 千 十三 年二 月十 日".to_string(),
range: 10..24,
entity_kind: BuiltinEntityKind::Time,
entity: InstantTime(expected_time_value.clone()),
entity_kind: BuiltinEntityKind::Datetime,
entity: InstantTime(expected_datetime_value.clone()),
};

let parsed_entities = parser.extract_entities(
Expand All @@ -439,9 +487,9 @@ mod test {
assert_eq!(expected_entity.range, parsed_entity.range);
assert_eq!(expected_entity.entity_kind, parsed_entity.entity_kind);

if let SlotValue::InstantTime(ref parsed_time) = parsed_entity.entity {
assert_eq!(expected_time_value.grain, parsed_time.grain);
assert_eq!(expected_time_value.precision, parsed_time.precision);
if let SlotValue::InstantTime(ref parsed_datetime) = parsed_entity.entity {
assert_eq!(expected_datetime_value.grain, parsed_datetime.grain);
assert_eq!(expected_datetime_value.precision, parsed_datetime.precision);
} else {
panic!("")
}
Expand Down
2 changes: 1 addition & 1 deletion src/conversion/gazetteer_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pub fn convert_to_slot_value(
}
}
};
return match_entity_kind_to_slot_value!(MusicAlbum, MusicArtist, MusicTrack);
return match_entity_kind_to_slot_value!(City, Country, MusicAlbum, MusicArtist, MusicTrack, Region);
}
45 changes: 22 additions & 23 deletions src/conversion/rustling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use rustling_ontology::Grain as RustlingGrain;
use rustling_ontology::Lang as RustlingLanguage;
use rustling_ontology::output::{
AmountOfMoneyOutput, DurationOutput, FloatOutput, IntegerOutput, OrdinalOutput, Output,
OutputKind, PercentageOutput, TemperatureOutput, TimeIntervalOutput, TimeOutput,
OutputKind, PercentageOutput, TemperatureOutput,
DatetimeIntervalOutput, DatetimeOutput, DatetimeIntervalKind,
};
use rustling_ontology::ParserMatch;
use snips_nlu_ontology::*;
Expand Down Expand Up @@ -44,8 +45,8 @@ impl OntologyFrom<PercentageOutput> for PercentageValue {
}
}

impl OntologyFrom<TimeOutput> for InstantTimeValue {
fn ontology_from(rustling_output: TimeOutput) -> Self {
impl OntologyFrom<DatetimeOutput> for InstantTimeValue {
fn ontology_from(rustling_output: DatetimeOutput) -> Self {
Self {
value: rustling_output.moment.to_string(),
grain: Grain::ontology_from(rustling_output.grain),
Expand All @@ -54,18 +55,18 @@ impl OntologyFrom<TimeOutput> for InstantTimeValue {
}
}

impl OntologyFrom<TimeIntervalOutput> for TimeIntervalValue {
fn ontology_from(rustling_output: TimeIntervalOutput) -> Self {
match rustling_output {
TimeIntervalOutput::After(after) => Self {
impl OntologyFrom<DatetimeIntervalOutput> for TimeIntervalValue {
fn ontology_from(rustling_output: DatetimeIntervalOutput) -> Self {
match rustling_output.interval_kind {
DatetimeIntervalKind::After(after) => Self {
from: Some(after.moment.to_string()),
to: None,
},
TimeIntervalOutput::Before(before) => Self {
DatetimeIntervalKind::Before(before) => Self {
from: None,
to: Some(before.moment.to_string()),
},
TimeIntervalOutput::Between {
DatetimeIntervalKind::Between {
start,
end,
precision: _,
Expand Down Expand Up @@ -168,8 +169,8 @@ impl OntologyFrom<Output> for SlotValue {
Output::Integer(v) => SlotValue::Number(v.ontology_into()),
Output::Ordinal(v) => SlotValue::Ordinal(v.ontology_into()),
Output::Temperature(v) => SlotValue::Temperature(v.ontology_into()),
Output::Time(v) => SlotValue::InstantTime(v.ontology_into()),
Output::TimeInterval(v) => SlotValue::TimeInterval(v.ontology_into()),
Output::Datetime(v) => SlotValue::InstantTime(v.ontology_into()),
Output::DatetimeInterval(v) => SlotValue::TimeInterval(v.ontology_into()),
}
}
}
Expand All @@ -184,18 +185,8 @@ pub fn convert_to_builtin(input: &str, parser_match: ParserMatch<Output>) -> Bui
}

impl<'a> OntologyFrom<&'a Output> for BuiltinEntityKind {
fn ontology_from(v: &Output) -> Self {
match *v {
Output::AmountOfMoney(_) => BuiltinEntityKind::AmountOfMoney,
Output::Duration(_) => BuiltinEntityKind::Duration,
Output::Float(_) => BuiltinEntityKind::Number,
Output::Integer(_) => BuiltinEntityKind::Number,
Output::Ordinal(_) => BuiltinEntityKind::Ordinal,
Output::Temperature(_) => BuiltinEntityKind::Temperature,
Output::Time(_) => BuiltinEntityKind::Time,
Output::TimeInterval(_) => BuiltinEntityKind::Time,
Output::Percentage(_) => BuiltinEntityKind::Percentage,
}
fn ontology_from(output: &Output) -> Self {
BuiltinEntityKind::ontology_from(&output.kind())
}
}

Expand All @@ -207,7 +198,11 @@ impl<'a> OntologyFrom<&'a OutputKind> for BuiltinEntityKind {
OutputKind::Number => BuiltinEntityKind::Number,
OutputKind::Ordinal => BuiltinEntityKind::Ordinal,
OutputKind::Temperature => BuiltinEntityKind::Temperature,
OutputKind::Datetime => BuiltinEntityKind::Datetime,
OutputKind::Date => BuiltinEntityKind::Date,
OutputKind::Time => BuiltinEntityKind::Time,
OutputKind::DatePeriod => BuiltinEntityKind::DatePeriod,
OutputKind::TimePeriod => BuiltinEntityKind::TimePeriod,
OutputKind::Percentage => BuiltinEntityKind::Percentage,
}
}
Expand All @@ -221,7 +216,11 @@ impl<'a> TryOntologyFrom<&'a BuiltinEntityKind> for OutputKind {
BuiltinEntityKind::Number => Ok(OutputKind::Number),
BuiltinEntityKind::Ordinal => Ok(OutputKind::Ordinal),
BuiltinEntityKind::Temperature => Ok(OutputKind::Temperature),
BuiltinEntityKind::Datetime => Ok(OutputKind::Datetime),
BuiltinEntityKind::Date => Ok(OutputKind::Date),
BuiltinEntityKind::Time => Ok(OutputKind::Time),
BuiltinEntityKind::DatePeriod => Ok(OutputKind::DatePeriod),
BuiltinEntityKind::TimePeriod => Ok(OutputKind::TimePeriod),
BuiltinEntityKind::Percentage => Ok(OutputKind::Percentage),
_ => Err(format_err!("Cannot convert {:?} into rustling type", v)),
}
Expand Down

0 comments on commit ac356ff

Please sign in to comment.