Package: pyduckling-native-phihos
PyDucklingCopyright © 2020– Treble.aiℹ️ This is a fork of the original pyduckling-native library. There are differences to the original:Build against the latest version ofDucklingSupported Python versions range from 3.8 to 3.12x86_64 Linux only, but contributions welcome if you dare to take on the challengeOverviewThis package provides native bindings for Facebook'sDucklingin Python. This package supports all dimensions and languages available on the original library, and it does not require to spawn a Haskell server and does not use HTTP to call the Duckling API.ℹ️ This package is completely Haskell-lessInstallingTo install pyduckling, you can use both conda and pip package managers:# Using pippipinstallpyduckling-native-phihosℹ️ Right now, we only provide package distributions for Linux (x86_64).Version MatrixThe following table shows which PyDuckling version corresponds to which Duckling versionPyDucklingDuckling0.2.0v0.2.0.0 (commit7520daa)Package usagePyDuckling provides access to the parsing capabilities of Duckling used to extract structured data from text.# Core importsfromducklingimport(load_time_zones,parse_ref_time,parse_lang,default_locale_lang,parse_locale,parse_dimensions,parse,Context)# Install with pip install pendulumimportpendulum# Load reference time for time parsingtime_zones=load_time_zones("/usr/share/zoneinfo")bog_now=pendulum.now('America/Bogota').replace(microsecond=0)ref_time=parse_ref_time(time_zones,'America/Bogota',bog_now.int_timestamp)# Load language/locale informationlang_es=parse_lang('ES')default_locale=default_locale_lang(lang_es)locale=parse_locale('ES_CO',default_locale)# Create parsing context with time and language informationcontext=Context(ref_time,locale)# Define dimensions to look-up forvalid_dimensions=["amount-of-money","credit-card-number","distance","duration","email","number","ordinal","phone-number","quantity","temperature","time","time-grain","url","volume"]# Parse dimensions to useoutput_dims=parse_dimensions(valid_dimensions)# Parse a phraseresult=parse('En dos semanas',context,output_dims,False)This wrapper allows access to all the dimensions and languages available on Duckling:DimensionExample inputExample value outputamount-of-money"42€"{"value":42,"type":"value","unit":"EUR"}credit-card-number"4111-1111-1111-1111"{"value":"4111111111111111","issuer":"visa"}distance"6 miles"{"value":6,"type":"value","unit":"mile"}duration"3 mins"{"value":3,"minute":3,"unit":"minute","normalized":{"value":180,"unit":"second"}}email"duckling-team@fb.com"{"value":"duckling-team@fb.com"}number"eighty eight"{"value":88,"type":"value"}ordinal"33rd"{"value":33,"type":"value"}phone-number"+1 (650) 123-4567"{"value":"(+1) 6501234567"}quantity"3 cups of sugar"{"value":3,"type":"value","product":"sugar","unit":"cup"}temperature"80F"{"value":80,"type":"value","unit":"fahrenheit"}time"today at 9am"{"values":[{"value":"2016-12-14T09:00:00.000-08:00","grain":"hour","type":"value"}],"value":"2016-12-14T09:00:00.000-08:00","grain":"hour","type":"value"}url"https://api.wit.ai/message?q=hi"{"value":"https://api.wit.ai/message?q=hi","domain":"api.wit.ai"}volume"4 gallons"{"value":4,"type":"value","unit":"gallon"}DependenciesTo compile pyduckling, you will require the latest nightly release ofRust, alongsideCargo. Also, it requires a Python distribution with its corresponding development headers. Finally, this project depends on the following Cargo crates:PyO3: Library used to produce Python bindings from Rust code.Maturin: Build system to build and publish Rust-based Python packagesAdditionally, this package depends onDuckling-FFI, used to compile the native interface to Duckling on Haskell. In order to compile Duckling-FFI, you will require theStackHaskell manager.Installing locallyVia DockerThe only thing you need is a runningDockerdaemon
and permission to rundocker buildanddocker run.
Then just run./build.shAll build dependencies are already installed in container images. The build script will get them and start
containers for building the Haskell lib and the Python lib. The directories.cache,duckling-ffi/.stack-workandtargetwill appear. The first two are for accelerating future builds and the last one will contain your final
build result.After thebuild.shcompleted successfully, you can find wheel files (binary distributions of the library) insidetarget/wheels.
The Python version (e.g. Python 3.11 =cp311), the libc variant
(manylinuxormusllinux, if you are unsure you probably need manylinux) and the CPU architecture
(currently onlyx86_64) are encoded in the file name. Just pick the file matching to your system and install it with:pipinstall-Utarget/wheels/<myfile>.whlManuallyBesides Rust and Stack, you will require the latest version of maturin installed to compile this project locally:pipinstallmaturintomlFirst, you will need to compile Duckling-FFI in order to produce the shared librarylibducklingffi, to do so, you can use the git submodule found at the root of this repository:cdduckling-ffi
stackbuildThen, you will need to move the resulting binarylibducklingffi.ato theext_libfolder:cpduckling-ffi/libducklingffi.aext_libAfter completing this procedure, it is possible to execute the following command to compile pyduckling:maturindevelopIn order to produce wheels,maturin buildcan be used instead. This project supportsPEP517, thus pip can be used to install this package as well:pipinstall-U.Running testsWe use pytest to run tests as it follows (after callingmaturin develop):pytest-vduckling/testsChangelogPlease see ourCHANGELOGfile to learn more about our new features and improvements.Contribution guidelinesWe follow PEP8 and PEP257 for pure python packages and Rust to compile extensions. We use MyPy type annotations for all functions and classes declared on this package. Feel free to send a PR or create an issue if you have any problem/question.