网络科技

    今日:1164| 主题:245866
收藏本版
互联网、科技极客的综合动态。

[其他] nspell — Hunspell compatible spell-checker

[复制链接]
▼素顔美死人 发表于 2016-11-30 08:06:46
8 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Hunspell compatible spell-checker in plain-vanilla JavaScript.
  nspellcontains most of the essential core of Hunspell. It does not contain a tokeniser but leaves many details up to implementors. The main difference, conceptually, is that Hunspell is based on the user and their preferences, whereas    nspellis based on explicitly passed in options, thus producing the same results regardless of OS, file-system, or environment.  
  Table of Contents

  Installation

      npm:  
  1. npm install nspell
复制代码
You probably also want to install somedictionaries:  
  1. npm install dictionary-en-us
复制代码
Usage

  1. var dictionary = require('dictionary-en-us');
  2. var nspell = require('nspell');
  3. dictionary(function (err, dict) {
  4.   if (err) {
  5.     throw err;
  6.   }
  7.   var spell = nspell(dict);
  8.   console.log(spell.correct('colour'));
  9.   // false
  10.   console.log(spell.suggest('colour'));
  11.   // [ 'color' ]
  12.   console.log(spell.correct('color'));
  13.   // true
  14.   console.log(spell.correct('npm'));
  15.   // false
  16.   spell.add('npm');
  17.   console.log(spell.correct('npm'));
  18.   // true
  19. });
复制代码
API

      NSpell(aff, dic)  

  Create a new spell checker. Passing an affix document is required, through any of the below mentioned signatures.    nspellis useless without at least one    dicpassed—make sure to pass it either in the constructor or to.  
  Signatures

  
       
  •       NSpell(aff[, dic]);   
  •       NSpell(dictionary).   
  •       NSpell(dictionaries).  
  Parameters

  
       
  •       aff(      Bufferor      string) — Affix document to use. Must be in UTF-8 when buffer;   
  •       dic(      Bufferor      string) — Dictionary document to use. Must be in UTF-8 when buffer;   
  •       dictionary(      Object) — Object with      aff(required) and      dic(optional) properties;   
  •       dictionaries(      Array.<Dictionary>) — List of      dictionaryobjects. The first must have an      affkey, other      affkeys are ignored.  
  Returns

  New instance of    NSpell.  
      NSpell#correct(word)  

  Check if    wordis correctly spelled.  
  Example

  1. spell.correct('color'); // true
  2. spell.correct('html'); // false
  3. spell.correct('abreviation'); // false
复制代码
Parameters

  
       
  •       word(      string) — Word to check for correct spelling.  
  Returns

      boolean— Whether    wordis correctly spelled.  
      NSpell#suggest(word)  

  Suggest correctly spelled words close to    word.  
  Example

  1. spell.suggest('colour'); // [ 'color' ]
  2. spell.suggest('color'); // []
  3. spell.suggest('html'); // [ 'HTML' ]
  4. spell.suggest('alot'); // [ 'allot', 'slot', 'clot', ... ]
复制代码
Parameters

  
       
  •       word(      string) — Word to suggest spelling corrections for.  
  Returns

      Array.<string>— A list with zero or more suggestions.  
      NSpell#spell(word)  

  Get spelling information for    word.  
  Example

  1. spell.spell('colour');
  2. // { correct: false, forbidden: false, warn: false }
  3. spell.spell('color');
  4. // { correct: true, forbidden: false, warn: false }
复制代码
Parameters

  
       
  •       word(      string) — Word to check.  
  Returns

      Object, with the following properties:  
  
       
  •       correct(      boolean) — Whether      wordis correctly spelled;   
  •       forbidden(      boolean) — Whether      wordis actually correct, but forbidden from showing up as such (often by the users wish);   
  •       warn(      boolean) — Whether      wordis correct, but should trigger a warning (rarely used in dictionaries).  
      NSpell#add(word[, model])  

  Add    wordto known words. If no model is given, the word will be marked as correct in the future, and will show up in spelling suggestions. If a model is given,    wordwill be handled the same as    model.  
  Example

  1. spell.correct('npm'); // false
  2. spell.suggest('nnpm'); // [ 'ppm', 'bpm', ... ]
  3. spell.add('npm');
  4. spell.correct('npm'); // true
  5. spell.suggest('nnpm'); // [ 'npm' ]
复制代码
Parameters

  
       
  •       word(      string) — Word to add;   
  •       model(      string, optional) — Known word to model      wordafter.  
  Returns

      NSpell— The operated on instance.  
      NSpell#remove(word)  

  Remove    wordfrom the known words.  
  Example

  1. spell.correct('color'); // true
  2. spell.remove('color');
  3. spell.correct('color'); // false
复制代码
Parameters

  
       
  •       word(      string) — Word to add;  
  Returns

      NSpell— The operated on instance.  
      NSpell#wordCharacters()  

  Get extra word characters defined by the loaded affix file. Most affix files don’t set these, but for example theen-US dictionary sets    0123456789.  
  Example

  1. spell.wordCharacters(); // '0123456789'
复制代码
Returns

      string?— The defined word characters, if any.  
      NSpell#dictionary(dic)  

  Add an extra dictionary to the spellchecker.
  Example

  1. spell.dictionary([
  2.   '5',
  3.   'npm',
  4.   'nully',
  5.   'rebase',
  6.   'SHA',
  7.   'stringification'
  8. ].join('\n'));
复制代码
Parameters

  
       
  •       dic(      Bufferor      string) — Dictionary document to use. Must be in UTF-8 when buffer.  
  Returns

      NSpell— The operated on instance.  
  Note

  The given    dicmust be designed to work with the already loaded affix. It’s not possible to add dictionary files from different languages together (use two    NSpellinstances for that).  
      NSpell#personal(dic)  

  Add a personal dictionary.
  Example

  1. npm install dictionary-en-us0
复制代码
Parameters

  
       
  •       dic(      Bufferor      string) — Dictionary document to use. Must be in UTF-8 when buffer.  
  Returns

      NSpell— The operated on instance.  
  Note

  Lines starting with a    *mark a word as forbidden, which results in them being seen as incorrect, and prevents them from showing up in suggestions. Splitting a line in two with a slash, adds the left side and models it after the already known right word.  
  Dictionaries

  nspellsupports many parts of Hunspell-style dictionaries. Essentially, the concept of a dictionary consists of one “affix” document, and one or more “dictionary” document. The documents are tightly linked, so it’s not possible to use a Dutch affix with an English dictionary document.  
  Below is a short introduction, see    hunspell(5)for more information.  
  Affix documents

  Affix documents define the language, keyboard, flags, and much more. For example, a paraphrased example of aDutch affix document:  
  1. npm install dictionary-en-us1
复制代码
Not every option is supported in    nspell. Seefor a list of all options and which ones are supported.  
  Dictionary documents

  Dictionary documents contain words and flags applying to those words. For example:
  1. npm install dictionary-en-us2
复制代码
The above document contains three words, as the count on the first line shows. Further lines each start with a word. Some lines contain flags, as denoted by the slashes. What those flags do, and the size of flags, is defined by affix documents.
  Personal dictionary documents

  Personal dictionaries are not intertwined with affix document. They define new words and words to forbid. For example:
  1. npm install dictionary-en-us3
复制代码
In the above example,    foois added as a known word;    baris added as well, but modelled after the existing word    baz; finally,    quxis marked as a forbidden word.  
  Affix options

  The following affix options are known to Hunspell. The checked ones are supported by    nspell.  
  General

  
       
  •             SET encoding(UTF-8 is implied)   
  •             FLAG value   
  •             COMPLEXPREFIXES   
  •             LANG langcode   
  •             IGNORE characters   
  •             AF number_of_flag_vector_aliases   
  •             AF flag_vector   
  •             AF definitions in the affix file:   
  •             AF flag_vector  
  Suggestion

  
       
  •             KEY characters_separated_by_vertical_line_optionally   
  •             TRY characters   
  •             NOSUGGEST flag   
  •             MAXCPDSUGS num   
  •             MAXNGRAMSUGS num   
  •             MAXDIFF [0-10]   
  •             ONLYMAXDIFF   
  •             NOSPLITSUGS   
  •             SUGSWITHDOTS   
  •             REP number_of_replacement_definitions   
  •             REP what replacement   
  •             MAP number_of_map_definitions   
  •             MAP string_of_related_chars_or_parenthesized_character_sequences   
  •             PHONE number_of_phone_definitions   
  •             PHONE what replacement   
  •             WARN flag   
  •             FORBIDWARN  
  Compounding

  
       
  •             BREAK number_of_break_definitions   
  •             BREAK character_or_character_sequence   
  •             COMPOUNDRULE number_of_compound_definitions   
  •             COMPOUNDRULE compound_pattern   
  •             COMPOUNDMIN num   
  •             COMPOUNDFLAG flag   
  •             COMPOUNDBEGIN flag   
  •             COMPOUNDLAST flag   
  •             COMPOUNDMIDDLE flag   
  •             ONLYINCOMPOUND flag   
  •             COMPOUNDPERMITFLAG flag   
  •             COMPOUNDFORBIDFLAG flag   
  •             COMPOUNDMORESUFFIXES   
  •             COMPOUNDROOT flag   
  •             COMPOUNDWORDMAX number   
  •             CHECKCOMPOUNDDUP   
  •             CHECKCOMPOUNDREP   
  •             CHECKCOMPOUNDCASE   
  •             CHECKCOMPOUNDTRIPLE   
  •             SIMPLIFIEDTRIPLE   
  •             CHECKCOMPOUNDPATTERN number_of_checkcompoundpattern_definitions   
  •             CHECKCOMPOUNDPATTERN endchars[/flag] beginchars[/flag] [replacement]   
  •             FORCEUCASE flag   
  •             COMPOUNDSYLLABLE max_syllable vowels   
  •             SYLLABLENUM flags  
  Affix creation

  
       
  •             PFX flag cross_product number   
  •             PFX flag stripping prefix [condition [morphological_fields...]]   
  •             SFX flag cross_product number   
  •             SFX flag stripping suffix [condition [morphological_fields...]]  
  Other

  
       
  •       CIRCUMFIX flag   
  •             FORBIDDENWORD flag   
  •             FULLSTRIP   
  •             KEEPCASE flag   
  •             ICONV number_of_ICONV_definitions   
  •             ICONV pattern pattern2   
  •             OCONV number_of_OCONV_definitions   
  •             OCONV pattern pattern2   
  •             LEMMA_PRESENT flag   
  •             NEEDAFFIX flag   
  •             PSEUDOROOT flag   
  •             SUBSTANDARD flag   
  •             WORDCHARS characters   
  •             CHECKSHARPS  
  License

  MIT ©    Titus Wormer
友荐云推荐




上一篇:Daydream产品设计案例:YouTube VR
下一篇:Facebook Wants You To Keep In Touch By Playing Pac-Man
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

凡涛 发表于 2016-11-30 09:44:51
顶顶更健康
回复 支持 反对

使用道具 举报

刘丹 发表于 2016-11-30 09:45:27
谁喷了榴莲味儿的香水?
回复 支持 反对

使用道具 举报

4466111 发表于 7 天前
我本非随便的人,但如果你想随便,那我就随你的便好啦!  
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表