Skip to content

Commit

Permalink
Merge pull request #311 from ikawaha/fix/deepsource-issue
Browse files Browse the repository at this point in the history
Fix deepsource issues
  • Loading branch information
ikawaha authored Oct 17, 2023
2 parents 29cbc56 + ac3bd7a commit ef87d6e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
15 changes: 7 additions & 8 deletions cmd/server/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"context"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -11,6 +10,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/ikawaha/kagome-dict/dict"
"github.com/ikawaha/kagome-dict/ipa"
Expand Down Expand Up @@ -106,8 +106,9 @@ func command(ctx context.Context, opt *option) error {
mux.Handle("/", &TokenizeDemoHandler{tokenizer: t})
mux.Handle("/tokenize", &TokenizeHandler{tokenizer: t})
srv := http.Server{
Addr: opt.http,
Handler: mux,
Addr: opt.http,
Handler: mux,
ReadHeaderTimeout: 20 * time.Second,
}
ch := make(chan error)
go func() {
Expand All @@ -132,17 +133,15 @@ func Run(ctx context.Context, args []string) error {
if err := opt.parse(args); err != nil {
Usage()
PrintDefaults(flag.ContinueOnError)
return errors.New("")
return fmt.Errorf("option parse error: %w", err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
select {
case <-c:
cancel()
}
<-c
cancel()
}()
return command(ctx, opt)
}
Expand Down
52 changes: 32 additions & 20 deletions sample/_example/db_search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"errors"
"fmt"
"log"
"os"
"strings"

"github.com/ikawaha/kagome-dict/ipa"
Expand All @@ -45,6 +46,13 @@ import (
)

func main() {
if err := run(); err != nil {
log.Println(err)
os.Exit(1)
}
}

func run() error {
// Contents to be inserted into the database. Each element represents a line
// of text and will be inserted into a row of the database.
lines := []string{
Expand All @@ -58,26 +66,30 @@ func main() {

// Create a database. In-memory database is used for simplicity.
db, err := sql.Open("sqlite3", ":memory:")
PanicOnError(err)

if err != nil {
return err
}
defer db.Close()

// Create tables.
// The first table "contents_fts" is for storing the original content, and
// the second table "fts" is for storing the tokenized content.
_, err = db.Exec(`
if _, err = db.Exec(`
CREATE TABLE IF NOT EXISTS contents_fts(docid INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT);
CREATE VIRTUAL TABLE IF NOT EXISTS fts USING fts4(words);
`)
PanicOnError(err)
`); err != nil {
return err
}

// Insert contents
for _, line := range lines {
rowID, err := insertContent(db, line)
PanicOnError(err)

err = insertSearchToken(db, rowID, line)
PanicOnError(err)
if err != nil {
return err
}
if err := insertSearchToken(db, rowID, line); err != nil {
return err
}
}

// Search by word.
Expand All @@ -98,7 +110,9 @@ func main() {
fmt.Println("Searching for:", searchWord)

rowIDsFound, err := searchFTS4(db, searchWord)
PanicOnError(err)
if err != nil {
return err
}

if len(rowIDsFound) == 0 {
fmt.Println(" No results found")
Expand All @@ -108,8 +122,9 @@ func main() {
// Print search results
for _, rowID := range rowIDsFound {
cont, err := retrieveContent(db, rowID)
PanicOnError(err)

if err != nil {
return err
}
fmt.Printf(" Found content: %s at line: %v\n", cont, rowID)
}
}
Expand All @@ -124,6 +139,8 @@ func main() {
// Found content: 北方の海の色は、青うございました。 at line: 3
// Searching for: 北
// Found content: 北の海にも棲んでいたのであります。 at line: 2

return nil
}

func insertContent(db *sql.DB, content string) (int64, error) {
Expand All @@ -142,7 +159,9 @@ func insertSearchToken(db *sql.DB, rowID int64, content string) error {
// This example uses the IPA dictionary, but it may be more efficient to use
// the 'Uni' dictionary if memory is available.
tknzr, err := tokenizer.New(ipa.Dict(), tokenizer.OmitBosEos())
PanicOnError(err)
if err != nil {
return err
}

seg := tknzr.Wakati(content)
tokenizedContent := strings.Join(seg, " ")
Expand All @@ -156,13 +175,6 @@ func insertSearchToken(db *sql.DB, rowID int64, content string) error {
return err
}

// PanicOnError exits the program with panic if the given error is not nil.
func PanicOnError(err error) {
if err != nil {
log.Panic(err)
}
}

func retrieveContent(db *sql.DB, rowID int) (string, error) {
rows, err := db.Query(
`SELECT rowid, content FROM contents_fts WHERE rowid=?`,
Expand Down

0 comments on commit ef87d6e

Please sign in to comment.