Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Sentence length and paragraph length to use HTML parser and enable AI button for both assessments #21866

Open
wants to merge 16 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds a temporary output script and (a currently failing) unit test
  • Loading branch information
mhkuu committed Nov 13, 2024
commit 684baabd9e7f577bb7c518b6c221c51c9cd8b242
29 changes: 29 additions & 0 deletions packages/yoastseo/spec/fullTextTests/runFullTextTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { getLanguagesWithWordComplexity } from "../../src/helpers";

// Import test papers.
import testPapers from "./testTexts";
import fs from "fs";

testPapers.forEach( function( testPaper ) {
// eslint-disable-next-line max-statements
Expand All @@ -65,6 +66,34 @@ testPapers.forEach( function( testPaper ) {

buildTree( paper, researcher );

/**
* Writes the given contents to the given filename in the temporary directory tmp
* @param {string} filename The name of the file.
* @param {string} content The content of the file.
* @returns {void}
*/
const writeToTempFile = ( filename, content ) => {
// Creates a temporary directory in the current working directory to store the data, if it not yet exists.
// (i.e., packages/yoastseo/tmp/ if this function is called from packages/yoastseo/)
const dir = "tmp/";
if ( ! fs.existsSync( dir ) ) {
fs.mkdirSync( dir );
}

// Writes the data to this temporary directory
fs.writeFileSync( dir + filename, content );
};

// Collects the results and the header into list of ;-separated rows
const sentences = researcher.getResearch( "countSentencesFromText" );
const resultLines = sentences.map( sentence => sentence.sentence.trimStart().split( " " )[ 0 ] + ";" + sentence.sentenceLength );

// Set doExport to true to write the results to a temporary file.
const doExport = true;
if ( doExport ) {
writeToTempFile( testPaper.name + ".csv", resultLines.join( "\n" ) );
}

const expectedResults = testPaper.expectedResults;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ describe( "A test to count sentence lengths.", function() {
expect( sentenceLengths[ 1 ].sentence.text ).toEqual( "this is a string" );
} );

it( "should return the correct length for sentences containing hyphens", function() {
const mockPaper = new Paper(
"<p>My know-it-all mother-in-law made a state-of-the-art U-turn.</p>" +
"<p>Her ex-husband found that low-key amazing.</p>" );
const mockResearcher = new EnglishResearcher( mockPaper );
buildTree( mockPaper, mockResearcher );

const sentenceLengths = sentencesLength( getSentencesFromTree( mockPaper ), mockResearcher );

expect( sentenceLengths.length ).toEqual( 2 );
expect( sentenceLengths[ 0 ].sentenceLength ).toEqual( 7 );
expect( sentenceLengths[ 1 ].sentenceLength ).toEqual( 6 );
} );

it( "should return the sentences and their length for Japanese (so counting characters)", function() {
const mockPaper = new Paper( "<p>自然おのずから存在しているもの</p>" +
"<p>歩くさわやかな森 <span style='color: red;'> 自然 </span></p>" );
Expand Down
Loading