Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,5 @@ type SentenceScore struct {
// code
type Analysis struct {
Language Language `json:"lang"`
Words []Score `json:"words"`
Sentences []SentenceScore `json:"sentences,omitempty"`
Score uint8 `json:"score"`
}
22 changes: 11 additions & 11 deletions sentiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ func (m Models) SentimentAnalysis(sentence string, lang Language) *Analysis {

analysis := &Analysis{
Language: lang,
Words: []Score{},
Score: uint8(0),
}

sentences := strings.FieldsFunc(sentence, SplitSentences)
mySentences := []SentenceScore{}
myWords := []Score{}
if len(sentences) > 1 {
analysis.Sentences = []SentenceScore{}

for _, s := range sentences {
class, P := m[lang].Probability(s)

analysis.Sentences = append(analysis.Sentences, SentenceScore{
mySentences = append(mySentences, SentenceScore{
Sentence: s,
Score: ScaleScores(class, P, lang),
Probability: P,
Expand All @@ -41,7 +41,7 @@ func (m Models) SentimentAnalysis(sentence string, lang Language) *Analysis {
for _, word := range w {
class, P := m[lang].Probability(word)

analysis.Words = append(analysis.Words, Score{
myWords = append(myWords, Score{
Word: word,
Score: ScaleScores(class, P, lang),
Probability: P,
Expand All @@ -52,18 +52,18 @@ func (m Models) SentimentAnalysis(sentence string, lang Language) *Analysis {
if class, P := m[lang].Probability(sentence); !math.IsNaN(P) {
fmt.Printf("Sentence: %v\tClass > Probability: %v > %v\n", sentence, int(class), P)
analysis.Score = ScaleScores(class, P, lang)
} else if len(analysis.Sentences) != 0 {
} else if len(mySentences) != 0 {
sum := float64(0)
for i := range analysis.Sentences {
sum += float64(analysis.Sentences[i].Score)
for i := range mySentences {
sum += float64(mySentences[i].Score)
}
analysis.Score = uint8(math.Floor(sum / float64(len(analysis.Sentences))))
analysis.Score = uint8(math.Floor(sum / float64(len(mySentences))))
} else {
sum := float64(0)
for i := range analysis.Words {
sum += float64(analysis.Words[i].Score)
for i := range myWords {
sum += float64(myWords[i].Score)
}
analysis.Score = uint8(math.Floor(sum / float64(len(analysis.Sentences))))
analysis.Score = uint8(math.Floor(sum / float64(len(mySentences))))
}

return analysis
Expand Down