-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AI Assistant: Add spelling mistake detection to Breve (#38923)
* add nspell to jetpack * add spelling mistake feature * hide suggestion button for typos * changelog * fix for js test
- Loading branch information
Showing
8 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/update-jetpack-ai-breve-typo-local
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,4 @@ | ||
Significance: patch | ||
Type: other | ||
|
||
AI Assistant: Add spelling mistake detection to Breve |
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
80 changes: 80 additions & 0 deletions
80
...tensions/plugins/ai-assistant-plugin/components/breve/features/spelling-mistakes/index.ts
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,80 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import nspell from 'nspell'; | ||
/** | ||
* Types | ||
*/ | ||
import type { BreveFeatureConfig, HighlightedText, SpellChecker } from '../../types'; | ||
|
||
export const SPELLING_MISTAKES: BreveFeatureConfig = { | ||
name: 'spelling-mistakes', | ||
title: __( 'Spelling mistakes', 'jetpack' ), | ||
tagName: 'span', | ||
className: 'jetpack-ai-breve__has-proofread-highlight--spelling-mistakes', | ||
defaultEnabled: false, | ||
}; | ||
|
||
const spellcheckers: { [ key: string ]: SpellChecker } = {}; | ||
const spellingContexts: { | ||
[ key: string ]: { | ||
affix: string; | ||
dictionary: string; | ||
}; | ||
} = {}; | ||
|
||
const loadContext = ( language: string ) => { | ||
// TODO: Load dictionaries dynamically and save on localStorage | ||
return spellingContexts[ language ]; | ||
}; | ||
|
||
const getSpellchecker = ( { language = 'en' }: { language?: string } = {} ) => { | ||
if ( spellcheckers[ language ] ) { | ||
return spellcheckers[ language ]; | ||
} | ||
|
||
// Cannot await here as the Rich Text function needs to be synchronous. | ||
// Load of the dictionary in the background if necessary and re-trigger the highlights later. | ||
const spellingContext = loadContext( language ); | ||
|
||
if ( ! spellingContext ) { | ||
return null; | ||
} | ||
|
||
const { affix, dictionary } = spellingContext; | ||
spellcheckers[ language ] = nspell( affix, dictionary ); | ||
|
||
return spellcheckers[ language ]; | ||
}; | ||
|
||
export default function longSentences( text: string ): Array< HighlightedText > { | ||
const highlightedTexts: Array< HighlightedText > = []; | ||
// Regex to match words, including contractions and hyphenated words | ||
// \p{L} is a Unicode property that matches any letter in any language | ||
// \p{M} is a Unicode property that matches any character intended to be combined with another character | ||
const wordRegex = new RegExp( /[\p{L}\p{M}'-]+/, 'gu' ); | ||
const words = text.match( wordRegex ) || []; | ||
const spellchecker = getSpellchecker(); | ||
|
||
if ( ! spellchecker ) { | ||
return highlightedTexts; | ||
} | ||
|
||
words.forEach( ( word: string, index ) => { | ||
if ( ! spellchecker.correct( word ) ) { | ||
const suggestions = spellchecker.suggest( word ); | ||
|
||
if ( suggestions.length > 0 ) { | ||
highlightedTexts.push( { | ||
text: word, | ||
startIndex: text.indexOf( word, index ), | ||
endIndex: text.indexOf( word, index ) + word.length, | ||
suggestions, | ||
} ); | ||
} | ||
} | ||
} ); | ||
|
||
return highlightedTexts; | ||
} |
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