feat(parser/keyword): use Keywords in Matches
This commit is contained in:
		
							parent
							
								
									f23d8932ea
								
							
						
					
					
						commit
						cc3b4b0b13
					
				|  | @ -8,7 +8,8 @@ import ( | |||
| ) | ||||
| 
 | ||||
| type Match struct { | ||||
| 	Keyword       string | ||||
| 	Keywords      []string | ||||
| 	Keyword       string // TODO: remove me
 | ||||
| 	Score         int | ||||
| 	MaxMatchCount int | ||||
| } | ||||
|  | @ -26,18 +27,21 @@ type Keyword struct{} | |||
| func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { | ||||
| 	score := conf.Score | ||||
| 	comment := "" | ||||
| 	for _, file := range conf.Files { | ||||
| 		content := executorResult.Files[file] | ||||
| 		for _, match := range conf.Matches { | ||||
| 			count := strings.Count(content, match.Keyword) | ||||
| 			if match.MaxMatchCount > 0 { | ||||
| 				count = min(count, match.MaxMatchCount) | ||||
| 	for _, match := range conf.Matches { | ||||
| 		for _, keyword := range match.Keywords { | ||||
| 			keywordMatchCount := 0 | ||||
| 			for _, file := range conf.Files { | ||||
| 				content := executorResult.Files[file] | ||||
| 				keywordMatchCount += strings.Count(content, keyword) | ||||
| 			} | ||||
| 			if count > 0 { | ||||
| 				score -= count * match.Score | ||||
| 			if match.MaxMatchCount > 0 { | ||||
| 				keywordMatchCount = min(keywordMatchCount, match.MaxMatchCount) | ||||
| 			} | ||||
| 			if keywordMatchCount > 0 { | ||||
| 				score -= keywordMatchCount * match.Score | ||||
| 				comment += fmt.Sprintf( | ||||
| 					"Matched keyword %d time(s): %s\n", | ||||
| 					count, match.Keyword) | ||||
| 					keywordMatchCount, keyword) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | @ -54,6 +58,13 @@ func (*Keyword) Run(results []stage.ExecutorResult, confAny any) ( | |||
| 	if err != nil { | ||||
| 		return nil, true, err | ||||
| 	} | ||||
| 	// TODO: remove me on Matches.Keyword field removed
 | ||||
| 	for i := range conf.Matches { | ||||
| 		match := &conf.Matches[i] | ||||
| 		if match.Keyword != "" && len(match.Keywords) == 0 { | ||||
| 			match.Keywords = []string{match.Keyword} | ||||
| 		} | ||||
| 	} | ||||
| 	// TODO: remove me on FullScore field removed
 | ||||
| 	if conf.FullScore != 0 && conf.Score == 0 { | ||||
| 		conf.Score = conf.FullScore | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user