feat(parser/keyword): show occurrences summary
This commit is contained in:
		
							parent
							
								
									5569703dfb
								
							
						
					
					
						commit
						8aa4d6d70b
					
				|  | @ -2,6 +2,7 @@ package keyword | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"sort" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/joint-online-judge/JOJ3/internal/stage" | 	"github.com/joint-online-judge/JOJ3/internal/stage" | ||||||
|  | @ -27,23 +28,47 @@ type Keyword struct{} | ||||||
| func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { | func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { | ||||||
| 	score := conf.Score | 	score := conf.Score | ||||||
| 	comment := "" | 	comment := "" | ||||||
|  | 	matchCount := make(map[string]int) | ||||||
|  | 	scoreChange := make(map[string]int) | ||||||
| 	for _, match := range conf.Matches { | 	for _, match := range conf.Matches { | ||||||
| 		for _, keyword := range match.Keywords { | 		for _, keyword := range match.Keywords { | ||||||
| 			keywordMatchCount := 0 |  | ||||||
| 			for _, file := range conf.Files { | 			for _, file := range conf.Files { | ||||||
| 				content := executorResult.Files[file] | 				content := executorResult.Files[file] | ||||||
| 				keywordMatchCount += strings.Count(content, keyword) | 				matchCount[keyword] += strings.Count(content, keyword) | ||||||
| 			} | 			} | ||||||
| 			if match.MaxMatchCount > 0 { | 			if match.MaxMatchCount > 0 { | ||||||
| 				keywordMatchCount = min(keywordMatchCount, match.MaxMatchCount) | 				matchCount[keyword] = min( | ||||||
|  | 					matchCount[keyword], match.MaxMatchCount) | ||||||
| 			} | 			} | ||||||
| 			if keywordMatchCount > 0 { | 			score += -match.Score * matchCount[keyword] | ||||||
| 				score -= keywordMatchCount * match.Score | 			scoreChange[keyword] = -match.Score * matchCount[keyword] | ||||||
| 				comment += fmt.Sprintf( |  | ||||||
| 					"Matched keyword %d time(s): %s\n", |  | ||||||
| 					keywordMatchCount, keyword) |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	type Result struct { | ||||||
|  | 		Keyword     string | ||||||
|  | 		Count       int | ||||||
|  | 		ScoreChange int | ||||||
|  | 	} | ||||||
|  | 	var results []Result | ||||||
|  | 	for keyword, count := range matchCount { | ||||||
|  | 		results = append(results, Result{ | ||||||
|  | 			Keyword:     keyword, | ||||||
|  | 			Count:       count, | ||||||
|  | 			ScoreChange: scoreChange[keyword], | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | 	sort.Slice(results, func(i, j int) bool { | ||||||
|  | 		if results[i].ScoreChange != results[j].ScoreChange { | ||||||
|  | 			return results[i].ScoreChange < results[j].ScoreChange | ||||||
|  | 		} | ||||||
|  | 		if results[i].Count != results[j].Count { | ||||||
|  | 			return results[i].Count > results[j].Count | ||||||
|  | 		} | ||||||
|  | 		return results[i].Keyword < results[j].Keyword | ||||||
|  | 	}) | ||||||
|  | 	for i, result := range results { | ||||||
|  | 		comment += fmt.Sprintf("%d. `%s`: %d occurrence(s), %d point(s)\n", | ||||||
|  | 			i+1, result.Keyword, result.Count, result.ScoreChange) | ||||||
| 	} | 	} | ||||||
| 	return stage.ParserResult{ | 	return stage.ParserResult{ | ||||||
| 		Score:   score, | 		Score:   score, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user