Skip to content

Commit b88618b

Browse files
committed
add go module support, simplify things, and now if ranks is empty not return an error
1 parent c9bcea2 commit b88618b

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/JesusIslam/tldr
2+
3+
require (
4+
github.com/alixaxel/pagerank v0.0.0-20160306110729-14bfb4c1d88c
5+
github.com/onsi/ginkgo v1.7.0
6+
github.com/onsi/gomega v1.4.3
7+
)

go.sum

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/alixaxel/pagerank v0.0.0-20160306110729-14bfb4c1d88c h1:UUHM6/UM34ESICar/DWOhLt2rqYabsvfjmupiY9z+iE=
2+
github.com/alixaxel/pagerank v0.0.0-20160306110729-14bfb4c1d88c/go.mod h1:e7Vic/xXDZAQ8ftWoLnVrXseAAvt54SVYrcirjCKcX0=
3+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
4+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5+
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
6+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
7+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
8+
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
9+
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
10+
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
11+
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
12+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
13+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
14+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
15+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
16+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
17+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
18+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
19+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
20+
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
21+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
22+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
23+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
24+
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
25+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

tldr.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ WARNING: This package is not thread safe, so you cannot use *Bag from many gorou
77
package tldr
88

99
import (
10-
"errors"
10+
"encoding/json"
1111
"sort"
1212
"strings"
1313
"unicode"
@@ -38,9 +38,14 @@ type Bag struct {
3838
vectorLength int
3939
}
4040

41+
func (b *Bag) String() string {
42+
r, _ := json.MarshalIndent(b, "", " ")
43+
return string(r)
44+
}
45+
4146
// The default values of each settings
4247
const (
43-
VERSION = "0.5.0"
48+
VERSION = "0.6.0"
4449
DEFAULT_ALGORITHM = "pagerank"
4550
DEFAULT_WEIGHING = "hamming"
4651
DEFAULT_DAMPING = 0.85
@@ -55,7 +60,6 @@ func defaultWordTokenizer(sentence string) []string {
5560
for i, word := range words {
5661
words[i] = SanitizeWord(word)
5762
}
58-
5963
return words
6064
}
6165

@@ -125,21 +129,18 @@ func (bag *Bag) Summarize(text string, num int) ([]string, error) {
125129
switch bag.Algorithm {
126130
case "centrality":
127131
bag.centrality()
128-
break
129132
case "pagerank":
130133
bag.pageRank()
131-
break
132134
case "custom":
133135
bag.Ranks = bag.customAlgorithm(bag.Edges)
134-
break
135136
default:
136137
bag.pageRank()
137138
}
138139

139140
// if no ranks, return error
140141
lenRanks := len(bag.Ranks)
141142
if lenRanks == 0 {
142-
return nil, errors.New("Ranks is empty")
143+
return nil, nil
143144
}
144145

145146
// guard so it won't crash but return only the highest rank sentence
@@ -263,14 +264,11 @@ func (bag *Bag) createEdges() {
263264
case "jaccard":
264265
commonElements := Intersection(src.vector, dst.vector)
265266
weight = 1.0 - float64(len(commonElements))/((float64(bag.vectorLength)*2)-float64(len(commonElements)))
266-
break
267267
case "hamming":
268268
differentElements := SymmetricDifference(src.vector, dst.vector)
269269
weight = float64(len(differentElements))
270-
break
271270
case "custom":
272271
weight = bag.customWeighing(src.vector, dst.vector)
273-
break
274272
default:
275273
differentElements := SymmetricDifference(src.vector, dst.vector)
276274
weight = float64(len(differentElements))

util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func Intersection(src, dst []int) []int {
155155
}
156156
}
157157
var result []int
158-
for k, _ := range intersect {
158+
for k := range intersect {
159159
result = append(result, k)
160160
}
161161
return result

0 commit comments

Comments
 (0)