Search code examples
rdictionaryspell-checkingfrench

Hunspell package : spell checking for French language


As I will be working on a french text with the mission to analyse it, I am unable to add a french dictionary to the hunspell package. I tried the links suggested by the package as follows :

To manually install dictionaries, copy the corresponding .aff and .dic file to ~/Library/Spelling or a custom directory specified in DICPATH. Alternatively you can pass the entire path to the .dic file as the dict parameter. Some popular sources of dictionaries are SCOWL, OpenOffice, debian, github/titoBouzout or github/wooorm.

However, nothing works out. I would like some suggestions into how to add a french dictionary to this package.

Thanks.


Solution

  • This solution should work on any system (windows, linux, macOS):

    First, run this code to download a lot of wonderful dictionaries:

    git clone https://github.com/titoBouzout/Dictionaries.git
    

    Take note of where you just downloaded the new files (since you'll use that location in the next step).

    Inside R, run this line to load in the French dictionary you just downloaded:

    library(hunspell) 
    french <- dictionary("~/Dictionaries/French.dic")
    

    Now that you have the French dictionary loaded, you can immediately use it to check spelling of French words:

    words_to_check <- c( 
      "poule", 
      "coq", 
      "canard", 
      "cochon", 
      "âne", 
      "sldkfjsldkjf" # <- incorrect spelling should return false
      )
    
    hunspell_check(words_to_check, dict = french)
    
    # [1]  TRUE  TRUE  TRUE  TRUE  TRUE  FALSE
    

    Note: this works for any language, not just French.