NewClassifier creates a classifier from taxonomy and config.
(taxonomy *config.Taxonomy, cfg config.Classification, sources []config.Source)
| 41 | |
| 42 | // NewClassifier creates a classifier from taxonomy and config. |
| 43 | func NewClassifier(taxonomy *config.Taxonomy, cfg config.Classification, sources []config.Source) (*Classifier, error) { |
| 44 | c := &Classifier{ |
| 45 | taxonomy: taxonomy, |
| 46 | cfg: cfg, |
| 47 | sources: sources, |
| 48 | } |
| 49 | |
| 50 | // Compile path rules |
| 51 | for _, r := range cfg.PathRules { |
| 52 | c.pathRules = append(c.pathRules, compiledPathRule{ |
| 53 | pattern: r.Pattern, |
| 54 | category: r.Category, |
| 55 | categoryFromDirname: r.CategoryFromDirname, |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | // Compile content patterns |
| 60 | for _, p := range cfg.ContentPatterns { |
| 61 | re, err := regexp.Compile("(?i)" + p.Pattern) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | c.contentREs = append(c.contentREs, compiledContentPattern{re: re, category: p.Category}) |
| 66 | } |
| 67 | |
| 68 | return c, nil |
| 69 | } |
| 70 | |
| 71 | // ClassifyFile determines which categories a source file belongs to. |
| 72 | func (c *Classifier) ClassifyFile(sourceName, sourceBasePath, relPath string) (FileClassification, error) { |