Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: sync report logic \w Linguist #214

Merged
merged 9 commits into from
Apr 9, 2019
35 changes: 17 additions & 18 deletions internal/code-generator/generator/samplesfreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"sort"
"strconv"
Expand Down Expand Up @@ -41,7 +40,7 @@ func Frequencies(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit st
}

func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
entries, err := ioutil.ReadDir(samplesDir)
langDirs, err := ioutil.ReadDir(samplesDir)
if err != nil {
return nil, err
}
Expand All @@ -52,13 +51,14 @@ func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
var tokens = make(map[string]map[string]int)
var languageTokens = make(map[string]int)

for _, entry := range entries {
if !entry.IsDir() {
for _, langDir := range langDirs {
if !langDir.IsDir() {
log.Println(err)
bzz marked this conversation as resolved.
Show resolved Hide resolved
continue
}

samples, err := getSamples(samplesDir, entry)
lang := langDir.Name()
samples, err := getSamplesFrom(filepath.Join(samplesDir, lang))
if err != nil {
log.Println(err)
}
Expand All @@ -73,7 +73,6 @@ func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
continue
}

lang := entry.Name()
languageTotal += len(samples)
languages[lang] = len(samples)
tokensTotal += len(samplesTokens)
Expand All @@ -93,22 +92,23 @@ func getFrequencies(samplesDir string) (*samplesFrequencies, error) {
}, nil
}

func getSamples(samplesDir string, langDir os.FileInfo) ([]string, error) {
const samplesSubDir = "filenames"
samples := []string{}
path := filepath.Join(samplesDir, langDir.Name())
entries, err := ioutil.ReadDir(path)
func getSamplesFrom(samplesLangDir string) ([]string, error) {
bzz marked this conversation as resolved.
Show resolved Hide resolved
const samplesLangFilesDir = "filenames"
var samples []string
bzz marked this conversation as resolved.
Show resolved Hide resolved
sampleFiles, err := ioutil.ReadDir(samplesLangDir)
if err != nil {
return nil, err
}

for _, entry := range entries {
if entry.Mode().IsRegular() {
samples = append(samples, filepath.Join(path, entry.Name()))
for _, sampleFile := range sampleFiles {
filename := filepath.Join(samplesLangDir, sampleFile.Name())
if sampleFile.Mode().IsRegular() {
samples = append(samples, filename)
continue
}

if entry.IsDir() && entry.Name() == samplesSubDir {
subSamples, err := getSubSamples(samplesDir, langDir.Name(), entry)
if sampleFile.IsDir() && sampleFile.Name() == samplesLangFilesDir {
subSamples, err := getSubSamplesFrom(filename)
if err != nil {
return nil, err
}
Expand All @@ -121,9 +121,8 @@ func getSamples(samplesDir string, langDir os.FileInfo) ([]string, error) {
return samples, nil
}

func getSubSamples(samplesDir, langDir string, subLangDir os.FileInfo) ([]string, error) {
func getSubSamplesFrom(path string) ([]string, error) {
bzz marked this conversation as resolved.
Show resolved Hide resolved
subSamples := []string{}
path := filepath.Join(samplesDir, langDir, subLangDir.Name())
entries, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
Expand Down