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