wordcounter

package module
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2023 License: MIT Imports: 14 Imported by: 0

README

example workflow codecov

Wordcounter

wordcounter is a tool mainly for Chinese characters count in a file like Markdown, Plain Text, etc. I create it for my writing word count stats purpose.

you can use it as a command line tool:

$ wcg count ./testdata --total
+---------------------------------------+-------+--------------+-----------------+------------+
| FILE                                  | LINES | CHINESECHARS | NONCHINESECHARS | TOTALCHARS |
+---------------------------------------+-------+--------------+-----------------+------------+
| D:\Repos\wordcounter\testdata\foo.md  |     1 |           12 |               1 |         13 |
| D:\Repos\wordcounter\testdata\test.md |     1 |            4 |               1 |          5 |
| Total                                 |     2 |           16 |               2 |         18 |
+---------------------------------------+-------+--------------+-----------------+------------+

or run it as a server(default host is localhost and port is 8080):

$ wcg server

 ┌───────────────────────────────────────────────────┐ 
 │                    WordCounter                    │ 
 │                   Fiber v2.47.0                   │ 
 │               http://127.0.0.1:8080               │ 
 │       (bound on host 0.0.0.0 and port 8080)       │ 
 │                                                   │ 
 │ Handlers ............. 3  Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID ............. 49653 │ 
 └───────────────────────────────────────────────────┘ 


$ curl -s \
--location 'localhost:8080/v1/wordcounter/count' \
--header 'Content-Type: application/json' \
--data '{
    "content": "落霞与孤鹜齐飞,秋水共长天一色"
}' | jq

{
  "data": {
    "lines": 1,
    "chinese_chars": 14,
    "non_chinese_chars": 1,
    "total_chars": 15
  },
  "error": "",
  "msg": "ok"
}

Features:

  • Generate statistics for content: About the number of lines, Chinese characters, non-Chinese characters, and total characters in the document content (with the option to include a total count).
  • Support for single file: if you prefer not to install plugins or rely on specific writing applications, this feature should suit your needs.
  • Support exporting tabular statistics: You can export the results to CSV or Excel. this feature is useful when you have more file or directory to count.
  • Support multiple platforms: You can use this tool on Linux, macOS, and Windows as well.
  • Support ignore mode: you can create a file named .wcignore which is similar to .gitignore can record patterns of files or folders that should not be scanned for counting, or specific patterns can be specified directly in the command line.
  • Provide count API for automation: In server mode, you can combine your automation tools like Automator, Keyboard Maestro to count content of a file.

How to use?

Use as command line

you can download the binary from release.

$ wcg --help
wordcounter is a simple tool that counts the chinese characters in a file

Usage:
  wcg [command]

Available Commands:
  count       Count for a file or directory
  server      Run wordcounter as a server, only support pure text content

Flags:
  -h, --help   help for wcg

Use "wcg [command] --help" for more information about a command.

or clone the repository and just build from source

note:

  • If you try to build from source, please ensure your OS has installed Go 1.19 or later.
  • If you are in China, highly recommend use goproxy to config your Go proxy firstly before installation and building.
git clone https://github.com/100gle/wordcounter
cd wordcounter

# config goproxy as you need.
# go env -w GO111MODULE=on
# go env -w GOPROXY=https://goproxy.cn,direct

go mod tidy

# linux/macOS
go build -o wcg ./cmd/wordcounter/main.go

# windows
go build -o wcg.exe ./cmd/wordcounter/main.go

note: wcg is a short of wordcounter-go.

Use as library
go get -u github.com/100gle/wordcounter

there are two optional counters for you:

FileCounter is a counter for single file.

package wordcounter

import (
    wcg "github.com/100gle/wordcounter"
)

func main() {
    counter := wcg.NewFileCounter("testdata/foo.md")
    counter.Count()

    // will output ascii table in console
    tbl := counter.ExportTable()
    fmt.Println(tbl)    

    // there are other optional export methods for you
    // counter.ExportCSV()
    // counter.ExportCSV("counter.csv") // Export to specific file

    // counter.ExportExcel("counter.xlsx")
}

DirCounter is a counter based on FileCounter for directory. It will recursively count the item if it is a directory. It supports to set some patterns like .gitignore to exclude some directories or files.

package wordcounter

import (
    wcg "github.com/100gle/wordcounter"
)

func main() {
    ignores := []string{"*.png", "*.jpg", "**/*.js"}
    counter := wcg.NewDirCounter("testdata", ignores...)
    counter.Count()

    // will output ascii table in console
    tbl := counter.ExportTable()
    fmt.Println(tbl)

    // there are other optional export methods for you
    // counter.ExportCSV()
    // counter.ExportCSV("counter.csv") // Export to specific file

    // counter.ExportExcel("counter.xlsx")
}

Licence

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToSliceOfString

func ConvertToSliceOfString(input [][]interface{}) [][]string

func DiscoverIgnoreFile

func DiscoverIgnoreFile() []string

func ToAbsolutePath

func ToAbsolutePath(path string) string

ToAbsolutePath detects if a path is absolute or not. If not, it converts path to absolute.

Types

type CountBody added in v0.1.8

type CountBody struct {
	Content string `json:"content"`
}

type DirCounter

type DirCounter struct {
	IgnoreList []string
	Fcs        []*FileCounter
	Exporter   *Exporter
	WithTotal  bool
	// contains filtered or unexported fields
}

func NewDirCounter

func NewDirCounter(dirname string, ignores ...string) *DirCounter

func (*DirCounter) Count

func (dc *DirCounter) Count() error

func (*DirCounter) EnableTotal

func (dc *DirCounter) EnableTotal()

func (*DirCounter) ExportCSV

func (dc *DirCounter) ExportCSV(filename ...string) (string, error)

func (*DirCounter) ExportExcel

func (dc *DirCounter) ExportExcel(filename ...string) error

func (*DirCounter) ExportTable

func (dc *DirCounter) ExportTable() string

func (*DirCounter) GetHeaderAndRows

func (dc *DirCounter) GetHeaderAndRows() []Row

func (*DirCounter) GetRows

func (dc *DirCounter) GetRows() []Row

func (*DirCounter) Ignore

func (dc *DirCounter) Ignore(pattern string)

func (*DirCounter) IsIgnored

func (dc *DirCounter) IsIgnored(filename string) bool

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

func NewExporter

func NewExporter() *Exporter

func (*Exporter) ExportCSV

func (e *Exporter) ExportCSV(data []Row, filename ...string) (string, error)

func (*Exporter) ExportExcel

func (e *Exporter) ExportExcel(data []Row, filename ...string) error

func (*Exporter) ExportTable

func (e *Exporter) ExportTable(data []Row) string

type FileCounter

type FileCounter struct {
	FileName string
	// contains filtered or unexported fields
}

func NewFileCounter

func NewFileCounter(filename string) *FileCounter

func (*FileCounter) Count

func (fc *FileCounter) Count() error

func (*FileCounter) ExportCSV

func (fc *FileCounter) ExportCSV(filename ...string) (string, error)

func (*FileCounter) ExportExcel

func (fc *FileCounter) ExportExcel(filename ...string) error

func (*FileCounter) ExportTable

func (fc *FileCounter) ExportTable() string

func (*FileCounter) GetHeader

func (fc *FileCounter) GetHeader() Row

func (*FileCounter) GetHeaderAndRow

func (fc *FileCounter) GetHeaderAndRow() []Row

func (*FileCounter) GetRow

func (fc *FileCounter) GetRow() Row

type Row

type Row = []interface{}

func GetTotal

func GetTotal(fcs []*FileCounter) Row

type Stats

type Stats struct {
	Lines           int `json:"lines,omitempty"`
	ChineseChars    int `json:"chinese_chars,omitempty"`
	NonChineseChars int `json:"non_chinese_chars,omitempty"`
	TotalChars      int `json:"total_chars,omitempty"`
}

func (*Stats) Header

func (s *Stats) Header() Row

func (*Stats) HeaderAndRows

func (s *Stats) HeaderAndRows() []Row

func (*Stats) ToRow

func (s *Stats) ToRow() Row

type TabularExporter

type TabularExporter interface {
	ExportCSV(data []Row, filename ...string) (string, error)
	ExportExcel(data []Row, filename ...string) error
	ExportTable(data []Row) string
}

type TextCounter

type TextCounter struct {
	S *Stats
}

func NewTextCounter

func NewTextCounter() *TextCounter

func (*TextCounter) Count

func (c *TextCounter) Count(input interface{}) error

type WordCounterServer added in v0.1.8

type WordCounterServer struct {
	Srv *fiber.App
}

func NewWordCounterServer added in v0.1.8

func NewWordCounterServer() *WordCounterServer

func (*WordCounterServer) Count added in v0.1.8

func (s *WordCounterServer) Count(ctx *fiber.Ctx) error

func (*WordCounterServer) Run added in v0.1.8

func (s *WordCounterServer) Run(port int) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL