-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21866 from Yoast/html-parser/paragraph-length
Convert Sentence length and paragraph length to use HTML parser and enable AI button for both assessments
- Loading branch information
Showing
30 changed files
with
775 additions
and
649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
packages/yoastseo/spec/languageProcessing/helpers/html/matchParagraphsSpec.js
This file was deleted.
Oops, something went wrong.
80 changes: 60 additions & 20 deletions
80
packages/yoastseo/spec/languageProcessing/helpers/sentence/sentencesLengthSpec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,81 @@ | ||
import sentencesLength from "../../../../src/languageProcessing/helpers/sentence/sentencesLength"; | ||
import getSentencesFromTree from "../../../../src/languageProcessing/helpers/sentence/getSentencesFromTree"; | ||
import JapaneseResearcher from "../../../../src/languageProcessing/languages/ja/Researcher"; | ||
import EnglishResearcher from "../../../../src/languageProcessing/languages/en/Researcher"; | ||
import Paper from "../../../../src/values/Paper"; | ||
import buildTree from "../../../specHelpers/parse/buildTree"; | ||
|
||
describe( "A test to count sentence lengths.", function() { | ||
it( "should not return a length for an empty sentence", function() { | ||
const sentences = [ "", "A sentence" ]; | ||
const mockResearcher = new EnglishResearcher( new Paper( "" ) ); | ||
const mockPaper = new Paper( "<p></p><p>A sentence</p>" ); | ||
const mockResearcher = new EnglishResearcher( mockPaper ); | ||
buildTree( mockPaper, mockResearcher ); | ||
|
||
const lengths = sentencesLength( sentences, mockResearcher ); | ||
const sentenceLengths = sentencesLength( getSentencesFromTree( mockPaper ), mockResearcher ); | ||
|
||
expect( lengths ).toEqual( [ | ||
{ sentence: "A sentence", sentenceLength: 2 }, | ||
] ); | ||
expect( sentenceLengths.length ).toEqual( 1 ); | ||
expect( sentenceLengths[ 0 ].sentenceLength ).toEqual( 2 ); | ||
expect( sentenceLengths[ 0 ].sentence.text ).toEqual( "A sentence" ); | ||
} ); | ||
|
||
it( "should return the sentences and their length (the HTML tags should not be counted if present)", function() { | ||
const sentences = [ "A <strong>good</strong> text", "this is a <span style='color: blue;'> textstring </span>" ]; | ||
const mockResearcher = new EnglishResearcher( new Paper( "" ) ); | ||
const mockPaper = new Paper( "<p>A <strong>good</strong> text</p>" + | ||
"<p>this is a <span style='color: blue;'>string</span></p>" ); | ||
const mockResearcher = new EnglishResearcher( mockPaper ); | ||
buildTree( mockPaper, mockResearcher ); | ||
|
||
const lengths = sentencesLength( sentences, mockResearcher ); | ||
const sentenceLengths = sentencesLength( getSentencesFromTree( mockPaper ), mockResearcher ); | ||
|
||
expect( lengths ).toEqual( [ | ||
{ sentence: "A <strong>good</strong> text", sentenceLength: 3 }, | ||
{ sentence: "this is a <span style='color: blue;'> textstring </span>", sentenceLength: 4 }, | ||
] ); | ||
expect( sentenceLengths.length ).toEqual( 2 ); | ||
expect( sentenceLengths[ 0 ].sentenceLength ).toEqual( 3 ); | ||
expect( sentenceLengths[ 0 ].sentence.text ).toEqual( "A good text" ); | ||
expect( sentenceLengths[ 1 ].sentenceLength ).toEqual( 4 ); | ||
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 correct length for sentences containing leading and trailing spaces including the first and last token that is not spaces", function() { | ||
const mockPaper = new Paper( | ||
"<p> The first sentence.</p><p>The second sentence. </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( 3 ); | ||
expect( sentenceLengths[ 0 ].firstToken ).toEqual( { sourceCodeRange: { endOffset: 7, startOffset: 4 }, text: "The" } ); | ||
expect( sentenceLengths[ 0 ].lastToken ).toEqual( { sourceCodeRange: { endOffset: 23, startOffset: 22 }, text: "." } ); | ||
expect( sentenceLengths[ 1 ].sentenceLength ).toEqual( 3 ); | ||
expect( sentenceLengths[ 1 ].firstToken ).toEqual( { sourceCodeRange: { endOffset: 33, startOffset: 30 }, text: "The" } ); | ||
expect( sentenceLengths[ 1 ].lastToken ).toEqual( { sourceCodeRange: { endOffset: 50, startOffset: 49 }, text: "." } ); | ||
} ); | ||
|
||
it( "should return the sentences and their length for Japanese (so counting characters)", function() { | ||
const sentences = [ "自然おのずから存在しているもの", "歩くさわやかな森 <span style='color: red;'> 自然 </span>" ]; | ||
const mockJapaneseResearcher = new JapaneseResearcher( new Paper( "" ) ); | ||
const mockPaper = new Paper( "<p>自然おのずから存在しているもの</p>" + | ||
"<p>歩くさわやかな森 <span style='color: red;'> 自然 </span></p>" ); | ||
const mockJapaneseResearcher = new JapaneseResearcher( mockPaper ); | ||
buildTree( mockPaper, mockJapaneseResearcher ); | ||
|
||
const lengths = sentencesLength( sentences, mockJapaneseResearcher ); | ||
const sentenceLengths = sentencesLength( getSentencesFromTree( mockPaper ), mockJapaneseResearcher ); | ||
|
||
expect( lengths ).toEqual( [ | ||
{ sentence: "自然おのずから存在しているもの", sentenceLength: 15 }, | ||
{ sentence: "歩くさわやかな森 <span style='color: red;'> 自然 </span>", sentenceLength: 10 }, | ||
] ); | ||
expect( sentenceLengths.length ).toEqual( 2 ); | ||
expect( sentenceLengths[ 0 ].sentenceLength ).toEqual( 15 ); | ||
expect( sentenceLengths[ 0 ].sentence.text ).toEqual( "自然おのずから存在しているもの" ); | ||
expect( sentenceLengths[ 1 ].sentenceLength ).toEqual( 10 ); | ||
expect( sentenceLengths[ 1 ].sentence.text ).toEqual( "歩くさわやかな森 自然 " ); | ||
} ); | ||
} ); |
Oops, something went wrong.