vendor/github.com/ysmood/got/lib/lcs/sequence.go

133 lines · 2549 bytes

  1package lcs
  2
  3import (
  4	"bufio"
  5	"bytes"
  6	"regexp"
  7)
  8
  9// Sequence list
 10type Sequence []Comparable
 11
 12// Sub from p, it will automatically decompress the compressed p.
 13func (xs Sequence) Sub(idx Indices) Sequence {
 14	s := make(Sequence, len(idx))
 15	for i, ix := range idx {
 16		s[i] = xs[ix]
 17	}
 18	return s
 19}
 20
 21// IsSubsequenceOf returns true if x is a subsequence of y
 22func (xs Sequence) IsSubsequenceOf(ys Sequence) bool {
 23	for i, j := 0, 0; i < len(xs); i++ {
 24		for {
 25			if j >= len(ys) {
 26				return false
 27			}
 28			if eq(xs[i], ys[j]) {
 29				j++
 30				break
 31			}
 32			j++
 33		}
 34	}
 35
 36	return true
 37}
 38
 39// Histogram of each Comparable
 40func (xs Sequence) Histogram() map[string][]int {
 41	h := map[string][]int{}
 42	for i := len(xs) - 1; i >= 0; i-- {
 43		s := xs[i].String()
 44		h[s] = append(h[s], i)
 45	}
 46	return h
 47}
 48
 49// Occurrence returns the position of each element of y in x.
 50func (xs Sequence) Occurrence(y Sequence) [][]int {
 51	o := make([][]int, len(y))
 52	h := xs.Histogram()
 53
 54	for i, c := range y {
 55		if indexes, has := h[c.String()]; has {
 56			o[i] = indexes
 57		}
 58	}
 59
 60	return o
 61}
 62
 63// Comparable interface
 64type Comparable interface {
 65	// String for comparison, such as the hash
 66	String() string
 67}
 68
 69// Element of a line, a word, or a character
 70type Element string
 71
 72// String returns the full content
 73func (e Element) String() string {
 74	return string(e)
 75}
 76
 77// NewChars from string
 78func NewChars(s string) Sequence {
 79	cs := Sequence{}
 80	for _, r := range s {
 81		cs = append(cs, Element(r))
 82	}
 83	return cs
 84}
 85
 86// NewWords from string list
 87func NewWords(words []string) Sequence {
 88	cs := make(Sequence, len(words))
 89	for i, word := range words {
 90		cs[i] = Element(word)
 91	}
 92	return cs
 93}
 94
 95// NewLines from string. It will split the s via newlines.
 96func NewLines(s string) Sequence {
 97	sc := bufio.NewScanner(bytes.NewBufferString(s))
 98	cs := Sequence{}
 99	for i := 0; sc.Scan(); i++ {
100		cs = append(cs, Element(sc.Text()))
101	}
102
103	if len(s) > 0 && s[len(s)-1] == '\n' {
104		cs = append(cs, Element(""))
105	}
106
107	return cs
108}
109
110// RegWord to match a word
111var regWord = regexp.MustCompile(`(?s)` + // enable . to match newline
112	`[[:alpha:]]{1,12}` + // match alphabets, length limit is 12
113	`|[[:digit:]]{1,3}` + // match digits, length limit is 3
114	`|.` + // match others as single-char words
115	``)
116
117// RegRune to match a rune
118var regRune = regexp.MustCompile(`(?s).`)
119
120// SplitKey for context
121var SplitKey = struct{}{}
122
123// Split a line into words
124func Split(s string) []string {
125	var reg *regexp.Regexp
126	if len(s) <= 100 {
127		reg = regRune
128	} else {
129		reg = regWord
130	}
131
132	return reg.FindAllString(s, -1)
133}