Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Calculate less weight for templates and static files (#168)
Browse files Browse the repository at this point in the history
Signed-off-by: thepetk <[email protected]>
  • Loading branch information
thepetk authored Apr 13, 2023
1 parent db5b043 commit 98a0064
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions go/pkg/apis/recognizer/language_recognizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"path/filepath"
"sort"
"strings"

enricher "github.com/redhat-developer/alizer/go/pkg/apis/enricher"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
Expand Down Expand Up @@ -41,7 +42,7 @@ func analyze(path string, ctx *context.Context) ([]model.Language, error) {
}
extensionsGrouped := extractExtensions(paths)
extensionHasProgrammingLanguage := false
totalProgrammingOccurrences := 0
totalProgrammingPoints := 0
for extension := range extensionsGrouped {
languages := languagesFile.GetLanguagesByExtension(extension)
if len(languages) == 0 {
Expand All @@ -67,15 +68,15 @@ func analyze(path string, ctx *context.Context) ([]model.Language, error) {
}
}
if extensionHasProgrammingLanguage {
totalProgrammingOccurrences += extensionsGrouped[extension]
totalProgrammingPoints += extensionsGrouped[extension]
extensionHasProgrammingLanguage = false
}
}

var languagesFound []model.Language
for name, item := range languagesDetected {
tmpWeight := float64(item.weight) / float64(totalProgrammingOccurrences)
tmpWeight = float64(int(tmpWeight*10000)) / 10000
tmpWeight := float64(item.weight) / float64(totalProgrammingPoints)
tmpWeight = float64(int(tmpWeight*100)) / 100
if tmpWeight > 0.02 {
tmpLanguage := model.Language{
Name: name,
Expand Down Expand Up @@ -118,15 +119,30 @@ func AnalyzeFile(configFile string, targetLanguage string) (model.Language, erro
return tmpLanguage, nil
}

func isStaticFileExtension(path string) bool {
staticDirs := [2]string{"static/", "templates/"}
for _, dir := range staticDirs {
if strings.Contains(path, dir) {
return true
}
}
return false
}

func extractExtensions(paths []string) map[string]int {
extensions := make(map[string]int)
for _, path := range paths {
extension := filepath.Ext(path)
if len(extension) == 0 {
continue
}
count := extensions[extension] + 1
extensions[extension] = count
extensionPoints := extensions[extension]
if !isStaticFileExtension(path) {
extensionPoints = extensionPoints + 100
} else {
extensionPoints = extensionPoints + 10
}
extensions[extension] = extensionPoints
}
return extensions
}

0 comments on commit 98a0064

Please sign in to comment.