You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB
Go

1 year ago
package main
import (
"bufio"
"flag"
"io/fs"
"log"
"os"
"path/filepath"
)
func main() {
var (
vaultDir string
)
flag.StringVar(&vaultDir, "vault", "./vault", "path to Obsidian vault")
flag.Parse()
absVaultPath, err := filepath.Abs(vaultDir)
if err != nil {
log.Fatalf("error generating absolute path: %s\n", err)
}
log.Println("absVaultPath:", absVaultPath)
vaultRoot := os.DirFS(absVaultPath)
noteLengthsChar := make(map[string]int)
noteLengthsWord := make(map[string]int)
fs.WalkDir(vaultRoot, ".", func(path string, d fs.DirEntry, err error) error {
absFilePath := filepath.Join(vaultDir, path)
f, err := os.Open(absFilePath)
if err != nil {
log.Printf("error opening note to calculate stats: %s\n", err)
return err
}
fStat, err := f.Stat()
if err != nil {
log.Printf("error calling stat() on file: %s\n", err)
return err
}
basePath := filepath.Base(path)
noteLengthsChar[basePath] = int(fStat.Size())
r := bufio.NewScanner(f)
for r.Scan() {
noteLengthsWord[basePath] += 1
}
return nil
})
log.Printf("Average character count: %.2f\n", averageMap(noteLengthsChar))
log.Printf("Average word count: %.2f\n", averageMap(noteLengthsWord))
}
func averageMap(m map[string]int) float64 {
sum := 0.0
for _, v := range m {
sum += float64(v)
}
return sum / float64(len(m))
}