From f241b02adb15b0cd5795d8c5aaf3e37470c35517 Mon Sep 17 00:00:00 2001 From: Martijn van der Klis Date: Wed, 8 Jan 2025 14:07:44 +0100 Subject: [PATCH] Fixes running researches that require a tree in the experimental content analysis API --- apps/content-analysis-api/routes/research.js | 8 ++++++-- packages/yoastseo/src/parse/language/LanguageProcessor.js | 3 +-- packages/yoastseo/src/parse/language/index.js | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 packages/yoastseo/src/parse/language/index.js diff --git a/apps/content-analysis-api/routes/research.js b/apps/content-analysis-api/routes/research.js index c861e42c78d..eb252c65c88 100644 --- a/apps/content-analysis-api/routes/research.js +++ b/apps/content-analysis-api/routes/research.js @@ -1,4 +1,6 @@ const { Paper } = require( "yoastseo" ); +const { build } = require( "yoastseo/build/parse/build" ); +const { LanguageProcessor } = require( "yoastseo/build/parse/language" ); const { getResearcher } = require( "../helpers/get-researcher" ); module.exports = function( app ) { @@ -45,10 +47,11 @@ module.exports = function( app ) { request.body.text || "", request.body || {} ); + paper.setTree( build( paper, new LanguageProcessor( researcher ), paper._attributes && paper._attributes.shortcodes ) ); researcher.setPaper( paper ); const sentenceLengths = researcher.getResearch( "countSentencesFromText" ); - const responseBody = sentenceLengths.map( sentence => ( { sentence: sentence.sentence, length: sentence.sentenceLength } ) ); + const responseBody = sentenceLengths.map( sentence => ( { sentence: sentence.sentence.text, length: sentence.sentenceLength } ) ); response.json( responseBody ); } ); @@ -59,10 +62,11 @@ module.exports = function( app ) { request.body.text || "", request.body || {} ); + paper.setTree( build( paper, new LanguageProcessor( researcher ), paper._attributes && paper._attributes.shortcodes ) ); researcher.setPaper( paper ); const paragraphLengths = researcher.getResearch( "getParagraphLength" ); - const responseBody = paragraphLengths.map( paragraph => ( { paragraph: paragraph.text, length: paragraph.countLength } ) ); + const responseBody = paragraphLengths.map( paragraph => ( { length: paragraph.paragraphLength } ) ); response.json( responseBody ); } ); } diff --git a/packages/yoastseo/src/parse/language/LanguageProcessor.js b/packages/yoastseo/src/parse/language/LanguageProcessor.js index 80d780c9de2..12224a25106 100644 --- a/packages/yoastseo/src/parse/language/LanguageProcessor.js +++ b/packages/yoastseo/src/parse/language/LanguageProcessor.js @@ -8,7 +8,7 @@ const whitespaceRegex = /^\s+$/; /** * Contains language-specific logic for splitting a text into sentences and tokens. */ -class LanguageProcessor { +export default class LanguageProcessor { /** * Creates a new language processor. * @@ -71,4 +71,3 @@ class LanguageProcessor { return tokenTexts.map( tokenText => new Token( tokenText ) ); } } -export default LanguageProcessor; diff --git a/packages/yoastseo/src/parse/language/index.js b/packages/yoastseo/src/parse/language/index.js new file mode 100644 index 00000000000..8ada2d20aa7 --- /dev/null +++ b/packages/yoastseo/src/parse/language/index.js @@ -0,0 +1,5 @@ +import LanguageProcessor from "./LanguageProcessor"; + +export { + LanguageProcessor, +};