Skip to content

Commit

Permalink
Made lowestParag
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Dec 14, 2024
1 parent c30cde4 commit 54f880f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 97 deletions.
1 change: 1 addition & 0 deletions internal/adapters/output/latex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 33 additions & 9 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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--")
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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:] {
Expand Down
88 changes: 0 additions & 88 deletions internal/core/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 54f880f

Please sign in to comment.