timenlp

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2021 License: Apache-2.0 Imports: 9 Imported by: 1

README

简介

Time-NLP 中文语句中的时间语义识别的golang版本

python 版本https://github.com/sunfiyes/Time-NLPY

python3 版本 https://github.com/zhanzecheng/Time_NLP

Java 版本https://github.com/shinyke/Time-NLP

PHP 版本https://github.com/crazywhalecc/Time-NLP-PHP

使用

go get -u github.com/bububa/TimeNLP

功能说明

用于句子中时间词的抽取和转换

import (
    "log"

    "github.com/bububa/TimeNLP"
)

func main() {
    target := "Hi,all.下周一下午三点开会"
    preferFuture := true
    tn := timenlp.TimeNormalizer(preferFuture)
    ret, err := tn.Parse(target)
    if err != nil {
        log.Fatalln(err)
    }
    log.Printf("%+v\n", ret)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lunar

type Lunar struct {
	IsLeap bool
	Day    int
	Month  int
	Year   int
}

type LunarSolarConverter

type LunarSolarConverter struct {
	// 1888~2111年农历数据表
	// 农历数据 每个元素的存储格式如下
	// 16~13    12          11~0
	// 闰几月 闰月日数  1~12月份农历日数(大小月)
	// 注:1、bit0表示农历1月份日数,为1表示30天,为0表示29天。bit1表示农历2月份日数,依次类推。
	// 2、bit12表示闰月日数,1为30天,0为29天。bit16~bit13表示第几月是闰月(注:为0表示该年无闰月)
	// 数据来源参考: http://data.weather.gov.hk/gts/time/conversion1_text_c.htm
	LunarMonthDays []int
	// 额外添加数据,方便快速计算阴历转阳历 每个元素的存储格式如下:
	// 12~7         6~5    4~0
	// 离元旦多少天  春节月  春节日
	Solar []int
}

func NewLunarSolarConverter

func NewLunarSolarConverter() *LunarSolarConverter

func (*LunarSolarConverter) GetBigInt

func (l *LunarSolarConverter) GetBigInt(data int, length int, shift int) int

func (*LunarSolarConverter) LunarToSolar

func (l *LunarSolarConverter) LunarToSolar(lunar Lunar) Solar

type RangeTimeEnum

type RangeTimeEnum int

范围时间的默认时间点

const (
	DAY_BREAK     RangeTimeEnum = 3  // 黎明
	EARLY_MORNING RangeTimeEnum = 8  // 早
	MORNING       RangeTimeEnum = 10 // 上午
	NOON          RangeTimeEnum = 12 // 中午、午间
	AFTERNOON     RangeTimeEnum = 15 // 下午、午后
	NIGHT         RangeTimeEnum = 18 // 晚上、傍晚
	LATE_NIGHT    RangeTimeEnum = 20 // 晚、晚间
	MID_NIGHT     RangeTimeEnum = 23 // 深夜
)

type Result

type Result struct {
	NormalizedString string        `json:"normalized_string,omitempty"`
	Type             ResultType    `json:"type,omitempty"`
	Points           []ResultPoint `json:"points,omitempty"`
}

type ResultPoint

type ResultPoint struct {
	Time   time.Time
	Pos    int `json:"pos,omitempty"`
	Length int `json:"length,omitempty"`
}

type ResultType

type ResultType string
const (
	DELTA     ResultType = "delta"
	SPAN      ResultType = "span"
	TIMESTAMP ResultType = "timestamp"
)

type Solar

type Solar struct {
	Day   int
	Month int
	Year  int
}

func NewSolarFromInt

func NewSolarFromInt(g int) Solar

func (Solar) ToInt

func (s Solar) ToInt() int

type SolorTermData

type SolorTermData struct {
	Key   float64
	Month int
	Years [][]int
}

type StringPreHandler

type StringPreHandler struct{}

func (StringPreHandler) DelKeyword

func (s StringPreHandler) DelKeyword(target string, rules string) string

该方法删除一字符串中所有匹配某一规则字串 可用于清理一个字符串中的空白符和语气助词 :param target: 待处理字符串 :param rules: 删除规则 :return: 清理工作完成后的字符串

func (StringPreHandler) NumberTranslator

func (s StringPreHandler) NumberTranslator(target string) string

该方法可以将字符串中所有的用汉字表示的数字转化为用阿拉伯数字表示的数字 如"这里有一千两百个人,六百零五个来自中国"可以转化为 "这里有1200个人,605个来自中国" 此外添加支持了部分不规则表达方法 如两万零六百五可转化为20650 两百一十四和两百十四都可以转化为214 一六零加一五八可以转化为160+158 该方法目前支持的正确转化范围是0-99999999 该功能模块具有良好的复用性 :param target: 待转化的字符串 :return: 转化完毕后的字符串

func (StringPreHandler) WordToNum

func (s StringPreHandler) WordToNum(str string) int64

方法numberTranslator的辅助方法,可将[零-九]正确翻译为[0-9] :param s: 大写数字 :return: 对应的整形数,如果不是数字返回-1

type TimeNormalizer

type TimeNormalizer struct {
	// contains filtered or unexported fields
}

时间表达式识别的主要工作类

func NewTimeNormalizer

func NewTimeNormalizer(isPreferFuture bool) *TimeNormalizer

func (*TimeNormalizer) Parse

func (n *TimeNormalizer) Parse(target string, timeBase time.Time) (*Result, error)

TimeNormalizer的构造方法,根据提供的待分析字符串和timeBase进行时间表达式提取

type TimePoint

type TimePoint [6]int

时间表达式单元规范化对应的内部类, 对应时间表达式规范化的每个字段, 六个字段分别是:年-月-日-时-分-秒, 每个字段初始化为-1

var DefaultTimePoint TimePoint = [6]int{-1, -1, -1, -1, -1, -1}

默认时间表达式单元

func NewTimePointFromTime

func NewTimePointFromTime(t time.Time) TimePoint

func (TimePoint) ToTime

func (t TimePoint) ToTime(loc *time.Location) time.Time

type TimeUnit

type TimeUnit struct {
	// contains filtered or unexported fields
}

时间语句分析

func NewTimeUnit

func NewTimeUnit(expTime string, pos int, length int, normalizer *TimeNormalizer, tpCtx TimePoint) *TimeUnit

func (TimeUnit) Time

func (t TimeUnit) Time() time.Time

func (TimeUnit) ToResultPoint

func (t TimeUnit) ToResultPoint() ResultPoint

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL