Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
added some documentation about the tim.Time type
Browse files Browse the repository at this point in the history
  • Loading branch information
shixzie committed Jun 27, 2017
1 parent ee9de89 commit 4652698
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,25 @@ go get github.com/Shixzie/nlp
You will always begin by creating a NL type calling nlp.New(), the NL type is a
Natural Language Processor that owns 3 funcs, RegisterModel(), Learn() and P().

### RegisterModel(i interface{}, samples []string) error
### RegisterModel(i interface{}, samples []string, ops ...ModelOption) error

RegisterModel takes 2 parameters, an empty struct and a set of samples.
RegisterModel takes 3 parameters, an empty struct, a set of samples and some options for the model.

The empty struct lets nlp know all possible values inside the text, for example:
```go
type Song struct {
Name string // fields must be exported
Artist string
Name string // fields must be exported
Artist string
ReleasedAt time.Time
}
err := nl.RegisterModel(Song{}, someSamples)
err := nl.RegisterModel(Song{}, someSamples, nlp.WithTimeFormat("2006"))
if err != nil {
panic(err)
}
// ...
```

tells nlp that inside the text may be a Song.Name and a Song.Artist.
tells nlp that inside the text may be a Song.Name, a Song.Artist and a Song.ReleasedAt.

The samples are the key part about nlp, not just because they set the *limits*
between *keywords* but also because they will be used to choose which model
Expand All @@ -62,6 +63,7 @@ songSamples := []string{
"play {Name} from {Artist}",
"play {Name}",
"from {Artist} play {Name}",
"play something from {ReleasedAt}",
}
```

Expand Down Expand Up @@ -154,19 +156,21 @@ and then **a pointer to a filled struct with the type used to register the model

```go
type Song struct {
Name string
Artist string
Name string
Artist string
ReleasedAt time.Time
}

songSamples := []string{
"play {Name} by {Artist}",
"play {Name} from {Artist}",
"play {Name}",
"from {Artist} play {Name}",
"play something from {ReleasedAt}",
}

nl := nlp.New()
err := nl.RegisterModel(Song{}, songSamples)
err := nl.RegisterModel(Song{}, songSamples, nlp.WithTimeFormat("2006"))
if err != nil {
panic(err)
}
Expand Down
9 changes: 5 additions & 4 deletions nlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ type field struct {
type ModelOption func(*model)

// WithTimeFormat sets the format used in time.Parse(format, val),
// note that format can't contain any spaces
// note that format can't contain any spaces, the default is 01-02-2006_3:04pm
func WithTimeFormat(format string) ModelOption {
return func(m *model) {
m.timeFormat = strings.Replace(format, " ", "", -1)
}
}

// WithTimeLocation sets the location used in time.ParseInLocation(format, value, loc)
// WithTimeLocation sets the location used in time.ParseInLocation(format, value, loc),
// the default is time.Local
func WithTimeLocation(loc *time.Location) ModelOption {
return func(m *model) {
if loc != nil {
Expand All @@ -108,7 +109,8 @@ func WithTimeLocation(loc *time.Location) ModelOption {
}

// RegisterModel registers a model i and creates possible patterns
// from samples.
// from samples, the default layout when parsing time is 01-02-2006_3:04pm
// and the default location is time.Local.
// Samples must have special formatting:
//
// "play {Name} by {Artist}"
Expand Down Expand Up @@ -265,7 +267,6 @@ order:
}

// fmt.Printf("orders: %v\n\n", limitsOrder)

// fmt.Printf("scores: %v\n", scores)

bestScore, bestMapping := -1, -1
Expand Down

0 comments on commit 4652698

Please sign in to comment.