yoastseo
Version:
Yoast client-side content analysis
127 lines (120 loc) • 5.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = getPassiveVoice;
exports.getPeriphrasticPassives = exports.getMorphologicalPassives = void 0;
var _getSentences = _interopRequireDefault(require("../helpers/sentence/getSentences.js"));
var _stripHTMLTags = require("../helpers/sanitize/stripHTMLTags.js");
var _Sentence = _interopRequireDefault(require("../../languageProcessing/values/Sentence.js"));
var _lodash = require("lodash");
var _htmlParser = _interopRequireDefault(require("../helpers/html/htmlParser"));
var _helpers = require("../helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* Looks for morphological passive voice.
* Supported morphological languages: "ru", "sv", "id", "ar", "he", "tr", "fa".
* Farsi is implemented as morphological because the periphrastic passives are used as compound verbs (among other compound passives).
* @param {Paper} paper The paper object.
* @param {Researcher} researcher The researcher.
*
* @returns {Object} The found passive sentences.
*/
const getMorphologicalPassives = function (paper, researcher) {
const isPassiveSentence = researcher.getHelper("isPassiveSentence");
let text = paper.getText();
text = (0, _htmlParser.default)(text);
text = (0, _helpers.filterShortcodesFromHTML)(text, paper._attributes && paper._attributes.shortcodes);
const memoizedTokenizer = researcher.getHelper("memoizedTokenizer");
const sentences = (0, _getSentences.default)(text, memoizedTokenizer).map(function (sentence) {
return new _Sentence.default(sentence);
});
const totalNumberSentences = sentences.length;
const passiveSentences = [];
(0, _lodash.forEach)(sentences, function (sentence) {
const strippedSentence = (0, _stripHTMLTags.stripFullTags)(sentence.getSentenceText()).toLocaleLowerCase();
sentence.setPassive(isPassiveSentence(strippedSentence));
if (sentence.isPassive() === true) {
passiveSentences.push(sentence.getSentenceText());
}
});
return {
total: totalNumberSentences,
passives: passiveSentences
};
};
/**
* Looks for periphrastic passive voice.
* Supported periphrastic languages: "en", "de", "nl", "fr", "es", "it", "pt", "pl", "sk".
*
* @param {Paper} paper The paper object.
* @param {Researcher} researcher The researcher.
*
* @returns {Object} The found passive sentences.
*/
exports.getMorphologicalPassives = getMorphologicalPassives;
const getPeriphrasticPassives = function (paper, researcher) {
const getClauses = researcher.getHelper("getClauses");
let text = paper.getText();
text = (0, _htmlParser.default)(text);
text = (0, _helpers.filterShortcodesFromHTML)(text, paper._attributes && paper._attributes.shortcodes);
const memoizedTokenizer = researcher.getHelper("memoizedTokenizer");
const sentences = (0, _getSentences.default)(text, memoizedTokenizer).map(function (sentence) {
return new _Sentence.default(sentence);
});
const totalNumberSentences = sentences.length;
const passiveSentences = [];
(0, _lodash.forEach)(sentences, function (sentence) {
const strippedSentence = (0, _stripHTMLTags.stripFullTags)(sentence.getSentenceText()).toLocaleLowerCase();
// The functionality based on sentencePart objects should be rewritten using array indices of stopwords and auxiliaries.
// Divide a sentence into clauses and return an array of clause objects that have been checked for passiveness.
const clauses = getClauses(strippedSentence);
sentence.setClauses(clauses);
// Check sentence passiveness based on its clause passiveness.
if (sentence.isPassive()) {
passiveSentences.push(sentence.getSentenceText());
}
});
return {
total: totalNumberSentences,
passives: passiveSentences
};
};
/**
* Looks for both morphological and periphrastic passive voice
* Supported languages with both morphological and periphrastic passives: "hu", "nb".
* Due to technical difficulties "nb" is only implemented as periphrastic at the moment. Languages that have not been implemented yet: "da".
*
* @param {Paper} paper The paper object.
* @param {Researcher} researcher The researcher.
*
* @returns {Object} The found passive sentences.
*/
exports.getPeriphrasticPassives = getPeriphrasticPassives;
const getMorphologicalAndPeriphrasticPassive = function (paper, researcher) {
const morphologicalPassives = getMorphologicalPassives(paper, researcher);
const periphrasticPassives = getPeriphrasticPassives(paper, researcher).passives;
return {
total: morphologicalPassives.total,
passives: periphrasticPassives.concat(morphologicalPassives.passives)
};
};
/**
* Looks for passive voice.
*
* @param {Paper} paper The paper object.
* @param {Researcher} researcher The researcher.
*
* @returns {Object} The found passive sentences.
*/
function getPassiveVoice(paper, researcher) {
const passiveType = researcher.getConfig("passiveConstructionType");
if (passiveType === "periphrastic") {
return getPeriphrasticPassives(paper, researcher);
}
if (passiveType === "morphological") {
return getMorphologicalPassives(paper, researcher);
}
return getMorphologicalAndPeriphrasticPassive(paper, researcher);
}
//# sourceMappingURL=getPassiveVoiceResult.js.map