Skip to content

Commit

Permalink
Use regexp2
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Nov 7, 2023
1 parent ddc2c04 commit 91c6aa3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ require (
gopkg.in/neurosnap/sentences.v1 v1.0.7
)

require github.com/neurosnap/sentences v1.1.2 // indirect
require (
github.com/errata-ai/regexp2 v1.7.0 // indirect
github.com/neurosnap/sentences v1.1.2 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/errata-ai/regexp2 v1.7.0 h1:N+weOlhwTd5iyDTcTCAMljXnfzkftcOZrdXno6G+QPM=
github.com/errata-ai/regexp2 v1.7.0/go.mod h1:59rO+jaxayJPF1WKI5m9R5F3Y3zR2Wn0DHnQbxtPm4A=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/neurosnap/sentences v1.1.2 h1:iphYOzx/XckXeBiLIUBkPu2EKMJ+6jDbz/sLJZ7ZoUw=
Expand Down
10 changes: 5 additions & 5 deletions strcase/sentence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package strcase

import (
"fmt"
"regexp"
"strings"

"github.com/errata-ai/regexp2"
"github.com/jdkato/twine/internal"
)

var reNumberList = regexp.MustCompile(`\d+\.`)
var reNumberList = regexp2.MustCompileStd(`\d+\.`)
var defaultSentOpts = CaseOpts{
vocab: []string{},
indicator: func(word string, idx int) bool {
if strings.HasSuffix(word, ":") {
return true
} else if idx == 0 && reNumberList.MatchString(word) {
} else if idx == 0 && reNumberList.MatchStringStd(word) {
return true
}
return false
Expand Down Expand Up @@ -51,7 +51,7 @@ func (sc *SentenceConverter) Convert(s string) string {
if len(sc.vocab) > 0 {
ps = fmt.Sprintf(`\b(?:%s)\b|%s`, strings.Join(sc.vocab, "|"), ps)
}
re := regexp.MustCompile(`(?i)` + ps)
re := regexp2.MustCompileStd(`(?i)` + ps)

tokens := re.FindAllString(s, -1)
for i, token := range tokens {
Expand All @@ -74,7 +74,7 @@ func (sc *SentenceConverter) Convert(s string) string {

func (sc *SentenceConverter) inVocab(s string) string {
for _, token := range sc.vocab {
matched, _ := regexp.MatchString(token, s)
matched, _ := regexp2.MatchString(token, s)
if strings.ToLower(token) == strings.ToLower(s) {
return token
} else if matched {
Expand Down
3 changes: 2 additions & 1 deletion strcase/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"unicode/utf8"

"github.com/errata-ai/regexp2"
"github.com/jdkato/twine/internal"
"github.com/jdkato/twine/nlp/tag"
)
Expand Down Expand Up @@ -102,7 +103,7 @@ func (tc *TitleConverter) Convert(s string) string {

func (tc *TitleConverter) inVocab(s string) string {
for _, token := range tc.vocab {
matched, _ := regexp.MatchString(token, s)
matched, _ := regexp2.MatchString(token, s)
if strings.ToLower(token) == strings.ToLower(s) {
return token
} else if matched {
Expand Down

0 comments on commit 91c6aa3

Please sign in to comment.