Compare commits
18 Commits
e4d66e501d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d72fe86325 | ||
|
|
7290880a10 | ||
|
|
12df5d0f5e | ||
|
|
9a7d93867a | ||
|
|
0104b2d396 | ||
|
|
61793326a3 | ||
|
|
53d0b162d1 | ||
|
|
f6c595d0f0 | ||
|
|
4a1e2b9c5e | ||
|
|
14e54f2393 | ||
|
|
2d1c6f161a | ||
|
|
64a2507631 | ||
|
|
de47a7fab3 | ||
|
|
3dcd171227 | ||
|
|
1de46569e8 | ||
|
|
57c153a6c3 | ||
|
|
55ad14e790 | ||
|
|
2b1e514431 |
14
GNUmakefile
14
GNUmakefile
@@ -1,20 +1,20 @@
|
||||
#
|
||||
.DEFAULT: all
|
||||
.PHONY: all clean
|
||||
.PHONY: all clean test
|
||||
|
||||
#
|
||||
DIST?= dist/
|
||||
GO?= go
|
||||
|
||||
#
|
||||
all:: dist/ir-bm25 dist/ir-tfidf
|
||||
all:: dist/ir-bm25 dist/ir-tfidf dist/news-cna
|
||||
@true
|
||||
|
||||
dist/ir-bm25:: cmd/ir-bm25/* internal/**
|
||||
"${GO}" build -o "${DIST}" ./cmd/ir-bm25
|
||||
|
||||
dist/ir-tfidf:: cmd/ir-tfidf/* internal/**
|
||||
"${GO}" build -o "${DIST}" ./cmd/ir-tfidf
|
||||
dist/%:: cmd/%/* internal/**
|
||||
"${GO}" build -o "${DIST}" ./cmd/$(notdir $@)
|
||||
|
||||
clean::
|
||||
rm -rf "${DIST}"
|
||||
|
||||
test::
|
||||
go test ./...
|
||||
|
||||
@@ -4,4 +4,4 @@ This is just an experimental repository.
|
||||
|
||||
## License
|
||||
|
||||
See [LICENSE](LICENSE) file.
|
||||
See [LICENSE](LICENSE) file for source codes (not including for `data/`).
|
||||
|
||||
@@ -1,4 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gslin/go-ir-playground/internal/artifact"
|
||||
"github.com/gslin/go-ir-playground/internal/tokenizer"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Printf("You need to specify a keyword to search.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
articles := artifact.Read("data/articles.json")
|
||||
|
||||
tokens := make(map[string][]string)
|
||||
token_num := 0
|
||||
tf := make(map[string]map[string]int)
|
||||
df := make(map[string]int)
|
||||
|
||||
for _, article := range articles {
|
||||
id := article.Id
|
||||
str := strings.ToLower(article.Title + "\n" + article.Body)
|
||||
|
||||
bag := tokenizer.Tokenize(str)
|
||||
tokens[id] = bag
|
||||
|
||||
token_num += len(bag)
|
||||
|
||||
for _, w := range bag {
|
||||
// Handle TF:
|
||||
if _, ok := tf[w]; !ok {
|
||||
tf[w] = make(map[string]int)
|
||||
}
|
||||
tf[w][id] += strings.Count(str, w)
|
||||
|
||||
// Handle DF:
|
||||
df[w] += 1
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("TF & DF Built")
|
||||
|
||||
n := len(articles)
|
||||
avgdl := float64(token_num / n)
|
||||
q := strings.ToLower(os.Args[1])
|
||||
q_tokens := tokenizer.Tokenize(q)
|
||||
|
||||
for _, article := range articles {
|
||||
id := article.Id
|
||||
|
||||
var score float64 = 0.0
|
||||
for _, w := range q_tokens {
|
||||
if tf[w] != nil {
|
||||
idf := math.Log2((float64(n - df[w]) + 0.5) / (float64(df[w]) + 0.5) + 1)
|
||||
score += idf * (float64(tf[w][id]) * 2.2) / (float64(tf[w][id]) + 1.2 * (0.25 + 0.75 * (float64(n) / avgdl)))
|
||||
}
|
||||
}
|
||||
|
||||
if score > 0 {
|
||||
fmt.Printf("Article %v (https://blog.gslin.org/?p=%v): %v\n", id, id, score)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,11 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Printf("You need to specify a keyword to search.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
articles := artifact.Read("data/articles.json")
|
||||
|
||||
tokens := make(map[string][]string)
|
||||
@@ -18,17 +23,18 @@ func main() {
|
||||
df := make(map[string]int)
|
||||
|
||||
for _, article := range articles {
|
||||
id := article.Id
|
||||
str := strings.ToLower(article.Title + "\n" + article.Body)
|
||||
|
||||
bag := tokenizer.Tokenize(str)
|
||||
tokens[article.Id] = bag
|
||||
tokens[id] = bag
|
||||
|
||||
for _, w := range bag {
|
||||
// Handle TF:
|
||||
if _, ok := tf[w]; !ok {
|
||||
tf[w] = make(map[string]int)
|
||||
}
|
||||
tf[w][article.Id] += strings.Count(str, w)
|
||||
tf[w][id] += strings.Count(str, w)
|
||||
|
||||
// Handle DF:
|
||||
df[w] += 1
|
||||
@@ -37,6 +43,7 @@ func main() {
|
||||
|
||||
fmt.Println("TF & DF Built")
|
||||
|
||||
n := len(articles)
|
||||
q := strings.ToLower(os.Args[1])
|
||||
q_tokens := tokenizer.Tokenize(q)
|
||||
|
||||
@@ -44,12 +51,12 @@ func main() {
|
||||
var score float64 = 0.0
|
||||
for _, w := range q_tokens {
|
||||
if tf[w] != nil {
|
||||
score += float64(tf[w][article.Id]) * math.Log2(float64(len(articles) / df[w]))
|
||||
score += float64(tf[w][article.Id]) * math.Log2(float64(n / df[w]))
|
||||
}
|
||||
}
|
||||
|
||||
if score > 0 {
|
||||
fmt.Printf("Article %v: %v\n", article.Id, score)
|
||||
fmt.Printf("Article %v (https://blog.gslin.org/?p=%v): %v\n", article.Id, article.Id, score)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
39
cmd/news-cna/main.go
Normal file
39
cmd/news-cna/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func main() {
|
||||
url := "https://www.cna.com.tw/list/aall.aspx"
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
log.Fatalf("res.StatusCode: %v", resp.StatusCode)
|
||||
}
|
||||
|
||||
// #jsMainList li
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
doc.Find("#jsMainList li").Each(func (i int, li *goquery.Selection) {
|
||||
href, exists := li.Find("a").Attr("href")
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
title := li.Find("h2").Text()
|
||||
|
||||
fmt.Printf("%v: %s\n", href, title)
|
||||
})
|
||||
}
|
||||
37897
data/articles.json
37897
data/articles.json
File diff suppressed because one or more lines are too long
6
go.mod
6
go.mod
@@ -2,9 +2,13 @@ module github.com/gslin/go-ir-playground
|
||||
|
||||
go 1.21.6
|
||||
|
||||
require github.com/stretchr/testify v1.8.4
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.9.0 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.8.4 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
41
go.sum
41
go.sum
@@ -1,9 +1,50 @@
|
||||
github.com/PuerkitoBio/goquery v1.9.0 h1:zgjKkdpRY9T97Q5DCtcXwfqkcylSFIVCocZmn2huTp8=
|
||||
github.com/PuerkitoBio/goquery v1.9.0/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
|
||||
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -7,6 +7,21 @@ import (
|
||||
"github.com/gslin/go-ir-playground/internal/ngram"
|
||||
)
|
||||
|
||||
func TestBigram(t *testing.T) {
|
||||
a := ngram.Bigram("test")
|
||||
assert.Equal(t, len(a), 0)
|
||||
|
||||
a = ngram.Bigram("測試")
|
||||
assert.Equal(t, len(a), 1)
|
||||
assert.Equal(t, a[0], "測試")
|
||||
|
||||
a = ngram.Bigram("中文測試")
|
||||
assert.Equal(t, len(a), 3)
|
||||
assert.Equal(t, a[0], "中文")
|
||||
assert.Equal(t, a[1], "文測")
|
||||
assert.Equal(t, a[2], "測試")
|
||||
}
|
||||
|
||||
func TestUnigram(t *testing.T) {
|
||||
a := ngram.Unigram("test")
|
||||
assert.Equal(t, len(a), 1)
|
||||
|
||||
14
internal/tokenizer/tokenizer_test.go
Normal file
14
internal/tokenizer/tokenizer_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package tokenizer_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/gslin/go-ir-playground/internal/tokenizer"
|
||||
)
|
||||
|
||||
func TestTokenize(t *testing.T) {
|
||||
a := tokenizer.Tokenize("test")
|
||||
assert.Equal(t, len(a), 1)
|
||||
assert.Equal(t, a[0], "test")
|
||||
}
|
||||
Reference in New Issue
Block a user