Skip to main content
Liphou
Inspiring
October 27, 2016
Question

[JS] Dictionary

  • October 27, 2016
  • 2 replies
  • 347 views

Bonjour,

Est-il possible de détecté, une citation anglaise ou autre dans un paragraphe exemple Français (dictionnaire Fr) sans dictionnaire appliqué à la citation à l'aide d'un script ou autre?

Merci   

Hello,


Is it possible to detected, an English quotation or another example in a paragraph French (Fr dictionary) without a dictionary applied to the citation with a script or another?

Thank you

This topic has been closed for replies.

2 replies

Liphou
LiphouAuthor
Inspiring
October 28, 2016

Thank you Trevor,

Beautiful example and a good approach.

After some research and testing, I will look to test the resulat the "Dynamic spelling"

If the spelling is incorrect, test with another dictionary, to have a reponce "True".

This now I recheche the order and method to do it tested.

to be continued ...

-------------

Merci Trevor,

Belle exemple et une bonne approche.

Après quelque recherche et test, je vais regarde pour tester avec le resulat du "Dynamic spelling"

Si l'orthographe n'est pas exacte, tester avec un autre dictionnaire, pour avoir une reponce "Vrai".

Maitenant, je recheche le commande et methode pour y faire les testes.

à  suivre ...

Trevor:
Legend
October 27, 2016

Hi Lipou,

This is a start.

There are probably much better ways.

I've used an object dictionary which you obviously would not want to do. You would probably import from a Hunspell dictionary or the likes.

They would need some interpretation.

Here's 3 words from that dictionary

abundance/SM

abundant/Y

abused/E

The /Y means that abundantly is valid one would have to look up there other codes

HTH

Trevor

// by Trevor www.creative-scripts.com

var doc, dic, c, l, n, i, stories, words, englishWords, englishWord, minPhraseLength, nonEnglishReg;

doc = app.activeDocument;

stories = doc.stories.everyItem().getElements().slice();

englishWords = [];

minPhraseLength = 2;

nonEnglishReg = /[^-A-za-z0-9]/g;

dic = getDictionary();

l = stories.length;

for (c = 0; c < l; c++) {

    words = stories.words.everyItem().getElements().slice();

    i = words.length;

    for (n = 0; n < i; n++) {

        if (words.contents.replace(nonEnglishReg, '') in dic) { // You might want to add .toLowerCase()

            englishWords.push(words);

        } else {

            englishPhrase(words.contents);

        }

    }

    englishPhrase(); // for English phrase at end of story

}

function englishPhrase() {

    if (englishWords.length >= minPhraseLength) {

        while (englishWord = englishWords.pop()) {

            englishWord.fillColor = doc.colors.itemByName('Magenta');

        }

        return true;

    }

    if (englishWords.length) {

        englishWords.length = 0;

    }

}

function getDictionary() {

    // you would normally fetch the dictionary from a dictionary file

    // for simplicities sake we are not

    var myDic = {

        'added': true,

        'English': true,

        'here': true,

        'some': true,

        'words': true,

    };

    return myDic;

}