Skip to content

Commit

Permalink
Merge pull request #20 from snipsco/release/0.2.2
Browse files Browse the repository at this point in the history
Release/0.2.2
  • Loading branch information
ClemDoum authored Jun 19, 2019
2 parents 01204a8 + df7f674 commit 04b9b86
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 61 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.

## [0.2.2] - 2019-06-18
### Added
- Gazetteer entity extension API [#18](https://github.com/snipsco/snips-nlu-parsers/pull/18))

### Changed
- Bumped Rustling to `0.18.1` [#20](https://github.com/snipsco/snips-nlu-parsers/pull/20)
- Extended Python dependencies version upper bound [#19](https://github.com/snipsco/snips-nlu-parsers/pull/19)

## [0.2.1] - 2019-04-08
### Added
- Expose complete and by language builtin entities json configuration retrieval API [#13](https://github.com/snipsco/snips-nlu-parsers/pull/13)
Expand All @@ -23,6 +31,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

[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
[0.2.0]: https://github.com/snipsco/snips-nlu-parsers/compare/0.1.2...0.2.0
[0.1.2]: https://github.com/snipsco/snips-nlu-parsers/compare/0.1.1...0.1.2
Expand Down
8 changes: 4 additions & 4 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.1"
version = "0.2.2"
authors = ["Adrien Ball <[email protected]>"]
edition = "2018"

Expand All @@ -20,9 +20,9 @@ regex = "0.2"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
gazetteer-entity-parser = { git = "https://github.com/snipsco/gazetteer-entity-parser", tag = "0.6.0" }
rustling-ontology = { git = "https://github.com/snipsco/rustling-ontology", tag = "0.18.0" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.6" }
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.7" }
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.1"
version = "0.2.2"
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.6" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.6" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.7" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.7" }
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.1"
version = "0.2.2"
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.6" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.6" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.7" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.7" }
snips-nlu-parsers = { path = "../.." }

[lib]
Expand Down
32 changes: 28 additions & 4 deletions ffi/ffi-macros/src/builtin_entity_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::ffi::CStr;
use std::slice;

use failure::ResultExt;
use ffi_utils::{convert_to_c_string, CReprOf, CStringArray, RawPointerConverter};
use libc;
use serde_json;
use snips_nlu_ontology::{BuiltinEntity, BuiltinEntityKind, BuiltinGazetteerEntityKind};
use snips_nlu_ontology_ffi_macros::{CBuiltinEntity, CBuiltinEntityArray};

use snips_nlu_parsers::{BuiltinEntityParser, BuiltinEntityParserLoader, EntityValue};

use crate::Result;
use ffi_utils::{convert_to_c_string, CReprOf, CStringArray, RawPointerConverter};
use snips_nlu_ontology::{BuiltinEntity, BuiltinEntityKind};
use snips_nlu_ontology_ffi_macros::{CBuiltinEntity, CBuiltinEntityArray};
use snips_nlu_parsers::{BuiltinEntityParser, BuiltinEntityParserLoader};

#[repr(C)]
pub struct CBuiltinEntityParser(*const libc::c_void);
Expand All @@ -22,6 +23,14 @@ macro_rules! get_parser {
}};
}

macro_rules! get_parser_mut {
($opaque:ident) => {{
let container: &$crate::CBuiltinEntityParser = unsafe { &*$opaque };
let x = container.0 as *mut BuiltinEntityParser;
unsafe { &mut *x }
}};
}

pub fn create_builtin_entity_parser(
ptr: *mut *const CBuiltinEntityParser,
json_config: *const libc::c_char,
Expand All @@ -38,6 +47,21 @@ pub fn create_builtin_entity_parser(
Ok(())
}

pub fn extend_gazetteer_entity_json(
ptr: *const CBuiltinEntityParser,
entity_name: *const libc::c_char,
entity_values_json: *const libc::c_char,
) -> Result<()> {
let parser = get_parser_mut!(ptr);
let entity_identifier = unsafe { CStr::from_ptr(entity_name) }.to_str()?;
let entity_kind = BuiltinGazetteerEntityKind::from_identifier(entity_identifier)?;
let entity_values_json_str = unsafe { CStr::from_ptr(entity_values_json) }.to_str()?;
let entity_values: Vec<EntityValue> = serde_json::from_str(entity_values_json_str)?;

parser.extend_gazetteer_entity(entity_kind, entity_values.into_iter())?;
Ok(())
}

pub fn persist_builtin_entity_parser(
ptr: *const CBuiltinEntityParser,
path: *const libc::c_char,
Expand Down
5 changes: 3 additions & 2 deletions ffi/ffi-macros/src/gazetteer_entity_parser.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::ffi::CStr;
use std::slice;

use ffi_utils::{convert_to_c_string, CReprOf, CStringArray, RawPointerConverter};
use libc;
use serde_json;

use crate::Result;
use ffi_utils::{convert_to_c_string, CReprOf, CStringArray, RawPointerConverter};
use snips_nlu_parsers::{GazetteerEntityMatch, GazetteerParser, GazetteerParserBuilder};

use crate::Result;

#[repr(C)]
pub struct CGazetteerEntityParser(*const libc::c_void);

Expand Down
19 changes: 16 additions & 3 deletions ffi/ffi-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod builtin_entity_parser;
mod gazetteer_entity_parser;

pub use builtin_entity_parser::*;
pub use gazetteer_entity_parser::*;

mod builtin_entity_parser;
mod gazetteer_entity_parser;

type Result<T> = ::std::result::Result<T, ::failure::Error>;

#[macro_export]
Expand Down Expand Up @@ -33,6 +33,19 @@ macro_rules! export_nlu_parsers_c_symbols {
wrap!($crate::load_builtin_entity_parser(ptr, parser_path))
}

#[no_mangle]
pub extern "C" fn snips_nlu_parsers_extend_gazetteer_entity_json(
ptr: *const $crate::CBuiltinEntityParser,
entity_name: *const libc::c_char,
entity_values_json: *const libc::c_char,
) -> ::ffi_utils::SNIPS_RESULT {
wrap!($crate::extend_gazetteer_entity_json(
ptr,
entity_name,
entity_values_json
))
}

#[no_mangle]
pub extern "C" fn snips_nlu_parsers_extract_builtin_entities(
ptr: *const $crate::CBuiltinEntityParser,
Expand Down
3 changes: 2 additions & 1 deletion ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ffi_utils::{generate_error_handling, wrap};
use snips_nlu_parsers_ffi_macros::export_nlu_parsers_c_symbols;
use snips_nlu_ontology_ffi_macros::export_nlu_ontology_c_symbols;

use snips_nlu_parsers_ffi_macros::export_nlu_parsers_c_symbols;

generate_error_handling!(snips_nlu_parsers_get_last_error);

export_nlu_ontology_c_symbols!();
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.1"
version = "0.2.2"
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.1" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.6" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.6" }
snips-nlu-parsers-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-parsers", tag = "0.2.2" }
snips-nlu-ontology = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.7" }
snips-nlu-ontology-ffi-macros = { git = "https://github.com/snipsco/snips-nlu-ontology", tag = "0.64.7" }
1 change: 1 addition & 0 deletions python/ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ffi_utils::*;
use snips_nlu_ontology_ffi_macros::export_nlu_ontology_c_symbols;

use snips_nlu_parsers_ffi_macros::export_nlu_parsers_c_symbols;

generate_error_handling!(snips_nlu_parsers_get_last_error);
Expand Down
4 changes: 2 additions & 2 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
readme = f.read()

required = [
"future==0.16.0",
"pathlib==1.0.1; python_version < '3.4'",
"future>=0.16,<0.18",
"pathlib>=1.0,<2.0; python_version < '3.4'",
]

rust_extension = RustExtension(
Expand Down
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.1
0.2.2
40 changes: 32 additions & 8 deletions python/snips_nlu_parsers/builtin_entity_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from _ctypes import byref, pointer
from _ctypes import byref
from builtins import bytes, str
from ctypes import c_char_p, c_int, c_void_p, string_at
from pathlib import Path
Expand All @@ -14,7 +14,7 @@ def __init__(self, parser):

@classmethod
def build(cls, language, gazetteer_entity_parser_path=None):
"""Build a `BuiltinEntityParser`
"""Builds a `BuiltinEntityParser`
Args:
language (str): Language identifier
Expand All @@ -30,7 +30,7 @@ def build(cls, language, gazetteer_entity_parser_path=None):
parser_config = dict(
language=language.upper(),
gazetteer_parser_path=gazetteer_entity_parser_path)
parser = pointer(c_void_p())
parser = c_void_p()
json_parser_config = bytes(json.dumps(parser_config), encoding="utf8")
exit_code = lib.snips_nlu_parsers_create_builtin_entity_parser(
byref(parser), json_parser_config)
Expand All @@ -39,7 +39,7 @@ def build(cls, language, gazetteer_entity_parser_path=None):
return cls(parser)

def parse(self, text, scope=None):
"""Extract builtin entities from *text*
"""Extracts builtin entities from *text*
Args:
text (str): Input
Expand Down Expand Up @@ -72,8 +72,32 @@ def parse(self, text, scope=None):
result = string_at(ptr)
return json.loads(result.decode("utf8"))

def extend_gazetteer_entity(self, entity_name, entity_values):
"""Extends a builtin gazetteer entity with custom values
Args:
entity_name (str): Gazetteer entity identifier
entity_values (list of dict): List of entity values represented as
dictionaries with a 'raw_value' key and a 'resolved_value' key
Returns:
The same object, updated.
Raises:
ValueError: when the entity name is unknown or not present in the
parser
"""
if not entity_values:
return self
entity_values_json = bytes(json.dumps(entity_values), encoding="utf8")
exit_code = lib.snips_nlu_parsers_extend_gazetteer_entity_json(
self._parser, entity_name.encode("utf8"), entity_values_json)
check_ffi_error(exit_code, "Something went wrong when extending the "
"builtin entity '%s'" % entity_name)
return self

def persist(self, path):
"""Persist the gazetteer parser on disk at the provided path"""
"""Persists the builtin entity parser on disk at the provided path"""
if isinstance(path, Path):
path = str(path)
exit_code = lib.snips_nlu_parsers_persist_builtin_entity_parser(
Expand All @@ -83,12 +107,12 @@ def persist(self, path):

@classmethod
def from_path(cls, parser_path):
"""Create a :class:`GazetteerEntityParser` from a gazetteer parser
"""Creates a :class:`BuiltinEntityParser` from a builtin entity parser
persisted on disk
"""
if isinstance(parser_path, Path):
parser_path = str(parser_path)
parser = pointer(c_void_p())
parser = c_void_p()
parser_path = bytes(parser_path, encoding="utf8")
exit_code = lib.snips_nlu_parsers_load_builtin_entity_parser(
byref(parser), parser_path)
Expand All @@ -97,5 +121,5 @@ def from_path(cls, parser_path):
return cls(parser)

def __del__(self):
if lib is not None and hasattr(self, '_parser'):
if lib is not None and self._parser is not None:
lib.snips_nlu_parsers_destroy_builtin_entity_parser(self._parser)
8 changes: 4 additions & 4 deletions python/snips_nlu_parsers/gazetteer_entity_parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
from _ctypes import byref, pointer
from _ctypes import byref
from builtins import bytes, str
from ctypes import c_char_p, c_int, c_void_p, string_at
from pathlib import Path

from snips_nlu_parsers.utils import (CStringArray, check_ffi_error, lib,
string_pointer)
string_pointer)


class GazetteerEntityParser(object):
Expand Down Expand Up @@ -55,7 +55,7 @@ def build(cls, build_config):
]
}
"""
parser = pointer(c_void_p())
parser = c_void_p()
json_parser_config = bytes(json.dumps(build_config), encoding="utf8")
exit_code = lib.snips_nlu_parsers_build_gazetteer_entity_parser(
byref(parser), json_parser_config)
Expand Down Expand Up @@ -113,7 +113,7 @@ def from_path(cls, parser_path):
"""
if isinstance(parser_path, Path):
parser_path = str(parser_path)
parser = pointer(c_void_p())
parser = c_void_p()
parser_path = bytes(parser_path, encoding="utf8")
exit_code = lib.snips_nlu_parsers_load_gazetteer_entity_parser(
byref(parser), parser_path)
Expand Down
Loading

0 comments on commit 04b9b86

Please sign in to comment.