-
Notifications
You must be signed in to change notification settings - Fork 885
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 #20381 from Yoast/158-tokenize-sentences-in-japanese
158 tokenize sentences in japanese
- Loading branch information
Showing
8 changed files
with
253 additions
and
4 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/yoastseo/spec/languageProcessing/languages/ja/helpers/splitIntoTokensCustomSpec.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import splitIntoTokensCustom from "../../../../../src/languageProcessing/languages/ja/helpers/splitIntoTokensCustom"; | ||
|
||
const testcases = [ | ||
{ | ||
description: "should return an empty result sentence is empty", | ||
sentence: { text: "", sourceCodeRange: { startOffset: 0, endOffset: 0 } }, | ||
expected: [], | ||
}, | ||
{ | ||
description: "should correctly tokenize a simple Japanese sentence wouthout punctuations", | ||
sentence: { | ||
text: "犬が大好き", | ||
sourceCodeRange: { startOffset: 0, endOffset: 5 }, | ||
}, | ||
expected: [ "犬", "が", "大好き" ], | ||
}, | ||
{ | ||
description: "should correctly tokenize a Japanese sentence with japanese punctuations", | ||
sentence: { | ||
text: "犬が大好き\u3002", | ||
sourceCodeRange: { startOffset: 0, endOffset: 6 }, | ||
}, | ||
expected: [ "犬", "が", "大好き", "。" ], | ||
|
||
}, | ||
{ | ||
description: "should correctly tokenize a Japanese sentence with english punctuations", | ||
sentence: { | ||
text: "犬が大好き.", | ||
sourceCodeRange: { startOffset: 0, endOffset: 6 }, | ||
}, | ||
expected: [ "犬", "が", "大好き", "." ], | ||
}, | ||
{ | ||
description: "should correctly tokenize a Japanese sentence with quotation marks inside", | ||
sentence: { | ||
text: "犬「が」大好き\u3002", | ||
sourceCodeRange: { startOffset: 0, endOffset: 8 }, | ||
}, | ||
expected: [ "犬", "「", "が", "」", "大好き", "。" ], | ||
}, | ||
{ | ||
description: "should correctly tokenize a Japanese sentence with quotation marks around", | ||
sentence: { | ||
text: "『犬が大好き\u3002』", | ||
sourceCodeRange: { startOffset: 0, endOffset: 8 }, | ||
}, | ||
expected: [ "『", "犬", "が", "大好き", "。", "』" ], | ||
}, | ||
]; | ||
|
||
describe.each( testcases )( "splitIntoTokensCustom for Japanese: %p", ( { description, sentence, expected } ) => { | ||
it( description, () => { | ||
const tokens = splitIntoTokensCustom( sentence ); | ||
expect( tokens ).toEqual( expected ); | ||
} ); | ||
} ); |
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
22 changes: 22 additions & 0 deletions
22
packages/yoastseo/src/languageProcessing/languages/ja/helpers/splitIntoTokensCustom.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { map } from "lodash-es"; | ||
import TinySegmenter from "tiny-segmenter"; | ||
|
||
/** | ||
* Split sentence into tokens. | ||
* | ||
* @param {Sentence} sentence The sentence to split. | ||
* | ||
* @returns {Token[]} The tokens. | ||
*/ | ||
function splitIntoTokensCustom( sentence ) { | ||
// Retrieve sentence from sentence class | ||
const sentenceText = sentence.text; | ||
// Return empty string if sentence is empty | ||
if ( sentenceText === "" ) { | ||
return []; | ||
} | ||
// Split sentences into words that are also tokens | ||
const words = new TinySegmenter().segment( sentenceText ); | ||
return map( words ); | ||
} | ||
export default splitIntoTokensCustom; |
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