fix: patch the opinion profile

This is not the best algorithm.
One should iterate over characters instead of over grades.
pull/21/head
Dominique Merle 3 years ago
parent 071ebd5c8d
commit 05195d2a31

@ -8,6 +8,8 @@ import (
"strings"
)
const minimumDefinitionLength = 7
// TextFormatter is the default formatter.
// It displays the proposals with their merit profiles and ranks.
// It does not use color (yet). ANSI colors are appalling.
@ -81,9 +83,17 @@ func (t *TextFormatter) Format(
legendDefinitions := make([]string, 0, 16)
for gradeIndex, gradeName := range grades {
maximumDefinitionLength := chartWidth - 2
if maximumDefinitionLength < minimumDefinitionLength {
maximumDefinitionLength = minimumDefinitionLength
}
legendDefinitions = append(
legendDefinitions,
fmt.Sprintf("%s=%s", getCharForIndex(gradeIndex), truncateString(gradeName, chartWidth-3, '…')),
fmt.Sprintf(
"%s=%s",
getCharForIndex(gradeIndex),
truncateString(gradeName, maximumDefinitionLength, '…'),
),
)
}

@ -107,12 +107,16 @@ func (t *TextOpinionFormatter) Format(
legendDefinitions := make([]string, 0, 16)
for _, proposalResult := range proposalsResults {
maximumDefinitionLength := chartWidth - 2
if maximumDefinitionLength < minimumDefinitionLength {
maximumDefinitionLength = minimumDefinitionLength
}
legendDefinitions = append(
legendDefinitions,
fmt.Sprintf(
"%s=%s",
getCharForIndex(proposalResult.Index),
truncateString(proposals[proposalResult.Index], chartWidth-2, '…'),
truncateString(proposals[proposalResult.Index], maximumDefinitionLength, '…'),
),
)
}
@ -135,15 +139,29 @@ func makeAsciiOpinionProfile(
widthFloat := float64(width)
maximumValueFloat := float64(maximumValue)
cumul := 0.0
for proposalIndex, proposalTally := range tallies {
gradeTallyInt := proposalTally.Tally[gradeIndex]
gradeTally := float64(gradeTallyInt)
proposalChar := getCharForIndex(proposalIndex)
amountOfCharsFloat := widthFloat*(gradeTally)/maximumValueFloat + cumul
amountOfChars := int(math.Round(amountOfCharsFloat))
if amountOfCharsFloat > 0.0 {
if amountOfChars == 0 {
cumul = amountOfCharsFloat
} else {
cumul = 0
}
}
ascii += strings.Repeat(
proposalChar,
int(math.Round(widthFloat*gradeTally/maximumValueFloat)),
amountOfChars,
)
}
for len(ascii) > width {
ascii = ascii[0 : len(ascii)-1]
}
return
}

Loading…
Cancel
Save