From 54f880f46535aaf781854d6986087d832b222303 Mon Sep 17 00:00:00 2001 From: Theo-Hafsaoui Date: Sat, 14 Dec 2024 14:03:45 +0100 Subject: [PATCH] Made lowestParag --- internal/adapters/output/latex_test.go | 1 + internal/core/generate.go | 42 +++++++++--- internal/core/generate_test.go | 88 -------------------------- 3 files changed, 34 insertions(+), 97 deletions(-) diff --git a/internal/adapters/output/latex_test.go b/internal/adapters/output/latex_test.go index eaf75db..0374007 100644 --- a/internal/adapters/output/latex_test.go +++ b/internal/adapters/output/latex_test.go @@ -11,6 +11,7 @@ import ( func removeWhitespace(s string) string { return strings.ReplaceAll(strings.ReplaceAll(s, " ", ""), "\n", "") } + func TestApplySectionToTemplate(t *testing.T) { t.Run("happy path - should apply Professional section template with headers and items", func(t *testing.T) { diff --git a/internal/core/generate.go b/internal/core/generate.go index 3b23b2e..b59a71e 100644 --- a/internal/core/generate.go +++ b/internal/core/generate.go @@ -31,6 +31,8 @@ type BuilderService struct { compiler Compiler } +const DO_NOT_RM_EDUCATION = true + // generate the templates for the cvs defined in the assets directory func (s *CVService) GenerateTemplates() error { slog.Info("--Generating CVs--") @@ -94,12 +96,12 @@ func generateCVFrom(cv CV, params Params, root string, slog.Info("Generating for:" + cvName) cvTemplate := template if shouldBeShorter { - removeLowestSection(&cv, keywords) + removeLowestParagraphe(&cv, keywords) } for _, section := range cv.Sections { for _, paragraph := range section.Paragraphes { headers := []string{paragraph.H1, paragraph.H2, - paragraph.H3, paragraph.H4} + paragraph.H3, paragraph.H4} items := paragraph.Items sortByScore(items, keywords) cvTemplate, err = processor.ApplySectionToTemplate( @@ -117,18 +119,37 @@ func generateCVFrom(cv CV, params Params, root string, return nil } -// Sort items and after remove the last items -func removeLowestSection(cv *CV, keywords []string) { +// Remove the lowest paragraph in the project and experience sections +func removeLowestParagraphe(cv *CV, keywords []string) { if len(cv.Sections) == 0 { return } - l_i := getLowestSection(*cv, keywords) - cv.Sections = append(cv.Sections[:l_i], cv.Sections[l_i+1:]...) + s_i,p_i := getLowestParagraphAcrossSections(*cv, keywords) + if s_i<0 || p_i < 0{ + slog.Warn("No paragraphe to remove found") + return + } + cv.Sections[s_i].Paragraphes = append(cv.Sections[s_i].Paragraphes[:p_i], cv.Sections[s_i].Paragraphes[p_i+1:]...) } -// Return the index of the section from the CV with the lowest score -func getLowestSection(cv CV, keywords []string) int { - return getLowestIndex(cv.Sections, keywords, getScoreSection) +// Return the index of the paragraph across all sections with the lowest score +func getLowestParagraphAcrossSections(cv CV, keywords []string) (s_i int, p_i int) { + minScore := int(^uint(0) >> 1) + s_i,p_i = -1,-1 + for secIdx, section := range cv.Sections { + slog.Warn(section.Title) + if section.Title == "education" && DO_NOT_RM_EDUCATION{ + continue + } + parIdx := getLowestParagraphe(section, keywords) + if parIdx >= 0 { + currentScore := getScoreParagraphe(section.Paragraphes[parIdx], keywords) + if currentScore < minScore { + minScore, s_i, p_i = currentScore, secIdx, parIdx + } + } + } + return s_i, p_i } // Return the index of the paragraph from the section with the lowest score @@ -139,6 +160,9 @@ func getLowestParagraphe(section Section, keywords []string) int { // generic function to get the index of the element with the lowest score // TODO ? an interface to avoid the any ? func getLowestIndex[T any](items []T, keywords []string, getScore func(T, []string) int) int { + if len(items)==0{ + return -1 + } min_score := getScore(items[0], keywords) min_idx := 0 for idx, item := range items[1:] { diff --git a/internal/core/generate_test.go b/internal/core/generate_test.go index 19d0fb3..dae0a55 100644 --- a/internal/core/generate_test.go +++ b/internal/core/generate_test.go @@ -249,94 +249,6 @@ func TestSortByScore(t *testing.T) { } } -func TestGetLowestSection(t *testing.T) { - keywords := []string{"experience", "skills", "projects"} - tests := []struct { - cv CV - expected int - }{ - { - cv: CV{ - Lang: "en", - Sections: []Section{ - { - Title: "Work Experience", - Paragraphes: []Paragraphe{ - {H1: "Experience 1", Items: []string{"experience", "project"}}, - }, - }, - { - Title: "Education", - Paragraphes: []Paragraphe{ - {H1: "Education 1", Items: []string{"education", "degree"}}, - }, - }, - { - Title: "Skills", - Paragraphes: []Paragraphe{ - {H1: "Skills", Items: []string{"skills", "knowledge"}}, - }, - }, - }, - }, - expected: 1, - }, - { - cv: CV{ - Lang: "en", - Sections: []Section{ - { - Title: "Skills", - Paragraphes: []Paragraphe{ - {H1: "Technical Skills", Items: []string{"skills", "knowledge"}}, - }, - }, - { - Title: "Projects", - Paragraphes: []Paragraphe{ - {H1: "Project 1", Items: []string{"project", "skill"}}, - }, - }, - { - Title: "Summary", - Paragraphes: []Paragraphe{ - {H1: "Personal Summary", Items: []string{"summary"}}, - }, - }, - }, - }, - expected: 1, - }, - { - cv: CV{ - Lang: "en", - Sections: []Section{ - { - Title: "Summary", - Paragraphes: []Paragraphe{ - {H1: "Summary", Items: []string{"introduction"}}, - }, - }, - { - Title: "Skills", - Paragraphes: []Paragraphe{ - {H1: "Skills", Items: []string{"skills"}}, - }, - }, - }, - }, - expected: 0, - }, - } - - for _, tt := range tests { - result := getLowestSection(tt.cv, keywords) - if result != tt.expected { - t.Errorf("expected index %v but got %v", tt.expected, result) - } - } -} - func TestGetLowestParagraphe(t *testing.T) { keywords := []string{"skills", "experience", "project"} tests := []struct {