Python常见实体提取库Duckling,多语言,实体如日期、金额、距离

简介

duckling是wit.ai的Duckling Clojure库的Python包装

用于以多种语言提取日期、金额、距离等常见实体




安装

pip install duckling

python-duckling需要安装JVM才能运行




中文

from duckling import *

d = DucklingWrapper(language=Language.CHINESE)
print(d.parse_duration("我今天跑了两个小时"))  # 解析时长
print(d.parse_number("我25岁了"))  # 解析数值
print(d.parse_ordinal("我是第一,你是第二"))  # 解析序数
print(d.parse_temperature("人体最适宜的温度是25摄氏度"))  # 解析温度
print(d.parse_time("我们十一点半见"))  # 解析时间
print(d.parse_timezone("中国统一用一个时区UTC"))  # 解析时区
# [{'dim': 'duration', 'text': '两个小时', 'start': 5, 'end': 9, 'value': {'value': 2.0, 'unit': 'hour', 'year': None, 'month': None, 'day': None, 'hour': 2, 'minute': None, 'second': None}}, {'dim': 'duration', 'text': '两个小时', 'start': 5, 'end': 9, 'value': {'value': 2.0, 'unit': 'hour', 'year': None, 'month': None, 'day': None, 'hour': 2, 'minute': None, 'second': None}}]
# [{'dim': 'number', 'text': '25', 'start': 1, 'end': 3, 'value': {'value': 25.0}}]
# [{'dim': 'ordinal', 'text': '第一', 'start': 2, 'end': 4, 'value': {'value': 1}}, {'dim': 'ordinal', 'text': '第二', 'start': 7, 'end': 9, 'value': {'value': 2}}]
# [{'dim': 'temperature', 'text': '25摄氏度', 'start': 9, 'end': 14, 'value': {'value': 25.0, 'unit': 'celsius'}}]
# [{'dim': 'time', 'text': '十一点半', 'start': 2, 'end': 6, 'value': {'value': '2020-05-14T11:30:00.000+08:00', 'grain': 'minute', 'others': [{'grain': 'minute', 'value': '2020-05-14T11:30:00.000+08:00'}, {'grain': 'minute', 'value': '2020-05-14T23:30:00.000+08:00'}, {'grain': 'minute', 'value': '2020-05-15T11:30:00.000+08:00'}]}}]
# [{'dim': 'timezone', 'text': 'UTC', 'start': 9, 'end': 12, 'value': {'value': 'UTC'}}]




英文

from duckling import *

d = DucklingWrapper()
print(d.parse_time("Let's meet at 11:45am"))  # 解析时间
print(d.parse_cycle("coming week"))  # 解析周期,失效
print(d.parse_distance("A circle around the equator is 40075.02 kilometers"))  # 解析距离
print(d.parse_duration("I ran for 2 hours today"))  # 解析时长
print(d.parse_email("Shoot me an email at [email protected]"))  # 解析邮箱
print(d.parse_leven_product("5 cups of sugar"))  # 解析产品
print(d.parse_leven_unit("two pounds of meat"))  # 解析单位
print(d.parse_money("You owe me 10 dollars"))  # 解析金钱
print(d.parse_number("I'm 25 years old"))  # 解析数值
print(d.parse_ordinal("I'm first, you're 2nd"))  # 解析序数
print(d.parse_phone_number("424-242-4242 is obviously a fake number"))  # 解析电话号码
print(d.parse_quantity("5 cups of sugar"))  # 解析总量
print(d.parse_temperature("Let's change the temperature from thirty two celsius to 65 degrees"))  # 解析温度
print(d.parse_time("Let's meet at 11:45am"))  # 解析时间
print(d.parse_timezone("My timezone is pdt"))  # 解析时区
print(d.parse_unit("6 degrees outside"))  # 解析单位(失效)
print(d.parse_unit_of_duration("1 second"))  # 解析时长的单位(失效)
print(d.parse_url("http://frank-blechschmidt.com is under construction, but you can check my github github.com/FraBle"))  # 解析URL
print(d.parse_volume("1 gallon is 3785ml"))  # 解析体积
# [{'dim': 'time', 'text': 'at 11:45am', 'start': 11, 'end': 21, 'value': {'value': '2020-05-14T11:45:00.000+08:00', 'grain': 'minute', 'others': [{'grain': 'minute', 'value': '2020-05-14T11:45:00.000+08:00'}, {'grain': 'minute', 'value': '2020-05-15T11:45:00.000+08:00'}, {'grain': 'minute', 'value': '2020-05-16T11:45:00.000+08:00'}]}}]
# []
# [{'dim': 'distance', 'text': '40075.02 kilometers', 'start': 31, 'end': 50, 'value': {'value': 40075.02, 'unit': 'kilometre'}}]
# [{'dim': 'duration', 'text': '2 hours', 'start': 10, 'end': 17, 'value': {'value': 2.0, 'unit': 'hour', 'year': None, 'month': None, 'day': None, 'hour': 2, 'minute': None, 'second': None}}]
# [{'dim': 'email', 'text': '[email protected]', 'start': 21, 'end': 51, 'value': {'value': '[email protected]'}}]
# [{'dim': 'leven-product', 'text': 'sugar', 'start': 10, 'end': 15, 'value': {'value': 'sugar'}}]
# [{'dim': 'leven-unit', 'text': 'pounds', 'start': 4, 'end': 10, 'value': {'value': 'pound'}}]
# [{'dim': 'amount-of-money', 'text': '10 dollars', 'start': 11, 'end': 21, 'value': {'value': 10.0, 'unit': '$'}}]
# [{'dim': 'number', 'text': '25', 'start': 4, 'end': 6, 'value': {'value': 25.0}}]
# [{'dim': 'ordinal', 'text': 'first', 'start': 4, 'end': 9, 'value': {'value': 1}}, {'dim': 'ordinal', 'text': '2nd', 'start': 18, 'end': 21, 'value': {'value': 2}}]
# [{'dim': 'phone-number', 'text': '424-242-4242 ', 'start': 0, 'end': 13, 'value': {'value': '424-242-4242 '}}]
# [{'dim': 'quantity', 'text': '5 cups of sugar', 'start': 0, 'end': 15, 'value': {'value': 5, 'unit': 'cup', 'product': 'sugar'}}]
# [{'dim': 'temperature', 'text': '65 degrees', 'start': 56, 'end': 66, 'value': {'value': 65.0, 'unit': 'degree'}}, {'dim': 'temperature', 'text': 'thirty two celsius', 'start': 34, 'end': 52, 'value': {'value': 32.0, 'unit': 'celsius'}}]
# [{'dim': 'time', 'text': 'at 11:45am', 'start': 11, 'end': 21, 'value': {'value': '2020-05-14T11:45:00.000+08:00', 'grain': 'minute', 'others': [{'grain': 'minute', 'value': '2020-05-14T11:45:00.000+08:00'}, {'grain': 'minute', 'value': '2020-05-15T11:45:00.000+08:00'}, {'grain': 'minute', 'value': '2020-05-16T11:45:00.000+08:00'}]}}]
# [{'dim': 'timezone', 'text': 'pdt', 'start': 15, 'end': 18, 'value': {'value': 'PDT'}}]
# []
# []
# [{'dim': 'url', 'text': 'http://frank-blechschmidt.com', 'start': 0, 'end': 29, 'value': {'value': 'http://frank-blechschmidt.com'}}, {'dim': 'url', 'text': 'github.com/FraBle', 'start': 81, 'end': 98, 'value': {'value': 'github.com/FraBle'}}, {'dim': 'url', 'text': 'http://frank-blechschmidt.com', 'start': 0, 'end': 29, 'value': {'value': 'http://frank-blechschmidt.com'}}]
# [{'dim': 'volume', 'text': '3785ml', 'start': 12, 'end': 18, 'value': {'value': 3785.0, 'unit': 'millilitre', 'latent': False}}, {'dim': 'volume', 'text': '1 gallon', 'start': 0, 'end': 8, 'value': {'value': 1.0, 'unit': 'gallon', 'latent': False}}]




函数列表

函数 描述 例子
parse 对所有维度进行解析 Let’s meet at 11:45am
parse_cycle 解析出现的周期(失效) coming week
parse_distance 解析距离 I commute 5 miles everyday
parse_duration 解析时长 I ran for 2 hours today
parse_email 解析邮箱 Shoot me an email at [email protected]
parse_leven_product 解析产品 5 cups of sugar
parse_leven_unit 解析单位 two pounds of meat
parse_money 解析金钱 You owe me 10 dollars
parse_number 解析数值 I’m 25 years old
parse_ordinal 解析序数 I’m first, you’re 2nd
parse_phone_number 解析号码 424-242-4242 is obviously a fake number
parse_quantity 解析总量 5 cups of sugar
parse_temperature 解析温度 Let’s change the temperature from thirty two celsius to 65 degrees
parse_time 解析时间 Let’s meet at 11:45am
parse_timezone 解析时区 My timezone is pdt
parse_unit 解析单位(失效) 6 degrees outside
parse_unit_of_duration 解析时长的单位(失效) 1 second
parse_url 解析URL http://frank-blechschmidt.com is under construction, but you can check my github github.com/FraBle
parse_volume 解析体积 1 gallon is 3785ml




参考文献

  1. FraBle/python-duckling: Python wrapper for wit.ai’s Duckling Clojure library

你可能感兴趣的:(Python,python,实体提取)