in src/js/webview/CSSSelectorResolver.js [301:347]
static getScore(
candidate: string,
fieldName: string,
contextSelector: string
): number {
// Ends recursion on empty candidate
if (!candidate) {
return 0;
}
// Extracts components
// ex components('article head h1') = ['article', 'head', 'h1']
let components = candidate.split(/\s+/);
// Extracts leaf
// ex: leaf('article head h1') = 'h1'
let leaf = components[components.length - 1];
// Extracts trunk
// ex: trunk('article head h1') = 'article head'
let trunk = components
.slice(0, components.length - 1)
.join(' ')
.trim();
// Extracts features for score calculations
// This will recursively look for the trunk score
let features: { [string]: number } = {
leafHasID: leaf.indexOf('#') !== -1 ? 1 : 0,
leafHasClass: leaf.indexOf('.') !== -1 ? 1 : 0,
leafHasTagName: leaf.match(/^[a-zA-Z]+/) ? 1 : 0,
endsWithNumber: leaf.match(/[0-9]+$/) ? 1 : 0,
numberOfComponents: components.length ? components.length : 0,
trunkScore: this.getScore(trunk, fieldName, contextSelector),
};
let weights = this.getFeatureWeights(fieldName);
// score = features * FEATURE_WEIGHTS
let score = Object.keys(features)
.map(key => weights[key] * features[key])
.reduce((total, current) => total + current, 0);
score = this.filterScore(score, candidate, contextSelector, fieldName);
return score;
}