-
Notifications
You must be signed in to change notification settings - Fork 877
/
assessor.js
303 lines (261 loc) · 9.01 KB
/
assessor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import MissingArgument from "../errors/missingArgument";
import removeDuplicateMarks from "../markers/removeDuplicateMarks";
import AssessmentResult from "../values/AssessmentResult.js";
import { showTrace } from "../helpers/errors.js";
import { __, sprintf } from "@wordpress/i18n";
import { filter, find, findIndex, isFunction, isUndefined, map } from "lodash-es";
import LanguageProcessor from "../parse/language/LanguageProcessor";
import { build } from "../parse/build";
// The maximum score of individual assessment is 9. This is why we set the "score rating" here to 9.
const ScoreRating = 9;
/**
* The Assessor is a base class for all assessors.
*/
class Assessor {
/**
* Creates a new Assessor instance.
* @param {Researcher} researcher The researcher to use.
* @param {Object} [options] The assessor options.
*/
constructor( researcher, options ) {
this.type = "assessor";
this.setResearcher( researcher );
this._assessments = [];
this._options = options || {};
}
/**
* Checks if the researcher is defined and sets it.
*
* @param {Researcher} researcher The researcher to use in the assessor.
*
* @throws {MissingArgument} Parameter needs to be a valid researcher object.
* @returns {void}
*/
setResearcher( researcher ) {
if ( isUndefined( researcher ) ) {
throw new MissingArgument( "The assessor requires a researcher." );
}
this._researcher = researcher;
}
/**
* Gets all available assessments.
* @returns {object} assessment
*/
getAvailableAssessments() {
return this._assessments;
}
/**
* Checks whether the Assessment is applicable.
*
* @param {Object} assessment The Assessment object that needs to be checked.
* @param {Paper} paper The Paper object to check against.
* @param {Researcher} [researcher] The Researcher object containing additional information.
* @returns {boolean} Whether or not the Assessment is applicable.
*/
isApplicable( assessment, paper, researcher ) {
if ( assessment.hasOwnProperty( "isApplicable" ) || typeof assessment.isApplicable === "function" ) {
return assessment.isApplicable( paper, researcher );
}
return true;
}
/**
* Determines whether an assessment has a marker.
*
* @param {Object} assessment The assessment to check for.
* @returns {boolean} Whether or not the assessment has a marker.
*/
hasMarker( assessment ) {
return isFunction( this._options.marker ) && ( assessment.hasOwnProperty( "getMarks" ) || typeof assessment.getMarks === "function" );
}
/**
* Returns the specific marker for this assessor.
*
* @returns {Function} The specific marker for this assessor.
*/
getSpecificMarker() {
return this._options.marker;
}
/**
* Returns the paper that was most recently assessed.
*
* @returns {Paper} The paper that was most recently assessed.
*/
getPaper() {
return this._lastPaper;
}
/**
* Returns the marker for a given assessment, composes the specific marker with the assessment getMarks function.
*
* @param {Object} assessment The assessment for which we are retrieving the composed marker.
* @param {Paper} paper The paper to retrieve the marker for.
* @param {Researcher} researcher The researcher for the paper.
* @returns {Function} A function that can mark the given paper according to the given assessment.
*/
getMarker( assessment, paper, researcher ) {
const specificMarker = this._options.marker;
return function() {
let marks = assessment.getMarks( paper, researcher );
marks = removeDuplicateMarks( marks );
specificMarker( paper, marks );
};
}
/**
* Runs the researches defined in the task list or the default researches.
*
* @param {Paper} paper The paper to run assessments on.
* @returns {void}
*/
assess( paper ) {
this._researcher.setPaper( paper );
const languageProcessor = new LanguageProcessor( this._researcher );
const shortcodes = paper._attributes && paper._attributes.shortcodes;
paper.setTree( build( paper, languageProcessor, shortcodes ) );
let assessments = this.getAvailableAssessments();
this.results = [];
assessments = filter( assessments, function( assessment ) {
return this.isApplicable( assessment, paper, this._researcher );
}.bind( this ) );
this.setHasMarkers( false );
this.results = map( assessments, this.executeAssessment.bind( this, paper, this._researcher ) );
this._lastPaper = paper;
}
/**
* Sets the value of has markers with a boolean to determine if there are markers.
*
* @param {boolean} hasMarkers True when there are markers, otherwise it is false.
* @returns {void}
*/
setHasMarkers( hasMarkers ) {
this._hasMarkers = hasMarkers;
}
/**
* Returns true when there are markers.
*
* @returns {boolean} Are there markers
*/
hasMarkers() {
return this._hasMarkers;
}
/**
* Executes an assessment and returns the AssessmentResult.
*
* @param {Paper} paper The paper to pass to the assessment.
* @param {Researcher} researcher The researcher to pass to the assessment.
* @param {Object} assessment The assessment to execute.
* @returns {AssessmentResult} The result of the assessment.
*/
executeAssessment( paper, researcher, assessment ) {
let result;
try {
result = assessment.getResult( paper, researcher );
result.setIdentifier( assessment.identifier );
if ( result.hasMarks() ) {
result.marks = assessment.getMarks( paper, researcher );
result.marks = removeDuplicateMarks( result.marks );
}
if ( result.hasMarks() && this.hasMarker( assessment ) ) {
this.setHasMarkers( true );
result.setMarker( this.getMarker( assessment, paper, researcher ) );
}
} catch ( assessmentError ) {
showTrace( assessmentError );
result = new AssessmentResult();
result.setScore( -1 );
result.setText( sprintf(
/* translators: %1$s expands to the name of the assessment. */
__( "An error occurred in the '%1$s' assessment", "wordpress-seo" ),
assessment.identifier,
assessmentError
) );
}
return result;
}
/**
* Filters out all assessment results that have no score and no text.
*
* @returns {Array<AssessmentResult>} The array with all the valid assessments.
*/
getValidResults() {
return filter( this.results, function( result ) {
return this.isValidResult( result );
}.bind( this ) );
}
/**
* Returns if an assessmentResult is valid.
*
* @param {object} assessmentResult The assessmentResult to validate.
* @returns {boolean} whether or not the result is valid.
*/
isValidResult( assessmentResult ) {
return assessmentResult.hasScore() && assessmentResult.hasText();
}
/**
* Returns the overall score. Calculates the total score by adding all scores and dividing these
* by the number of results times the ScoreRating.
*
* @returns {number} The overall score.
*/
calculateOverallScore() {
const results = this.getValidResults();
const totalScore = results.reduce( ( total, assessmentResult ) => total + assessmentResult.getScore(), 0 );
return Math.round( totalScore / ( results.length * ScoreRating ) * 100 ) || 0;
}
/**
* Register an assessment to add it to the internal assessments object.
*
* @param {string} name The name of the assessment.
* @param {object} assessment The object containing function to run as an assessment and it's requirements.
* @returns {boolean} Whether registering the assessment was successful.
*/
addAssessment( name, assessment ) {
if ( ! assessment.hasOwnProperty( "identifier" ) ) {
assessment.identifier = name;
}
// If the assessor already has the same assessment, remove it and replace it with the new assessment with the same identifier.
if ( this.getAssessment( assessment.identifier ) ) {
this.removeAssessment( assessment.identifier );
}
this._assessments.push( assessment );
return true;
}
/**
* Remove a specific Assessment from the list of Assessments.
*
* @param {string} name The Assessment to remove from the list of assessments.
* @returns {void}
*/
removeAssessment( name ) {
const toDelete = findIndex( this._assessments, function( assessment ) {
return assessment.hasOwnProperty( "identifier" ) && name === assessment.identifier;
} );
if ( -1 !== toDelete ) {
this._assessments.splice( toDelete, 1 );
}
}
/**
* Returns an assessment by identifier
*
* @param {string} identifier The identifier of the assessment.
* @returns {undefined|Assessment} The object if found, otherwise undefined.
*/
getAssessment( identifier ) {
return find( this._assessments, function( assessment ) {
return assessment.hasOwnProperty( "identifier" ) && identifier === assessment.identifier;
} );
}
/**
* Checks which of the available assessments are applicable and returns an array with applicable assessments.
*
* @returns {Array} The array with applicable assessments.
*/
getApplicableAssessments() {
const availableAssessments = this.getAvailableAssessments();
return filter(
availableAssessments,
function( availableAssessment ) {
return this.isApplicable( availableAssessment, this.getPaper(), this._researcher );
}.bind( this )
);
}
}
export default Assessor;