acronym-generator
Smartly generates acronyms from a words database you can fill.
This module allows you generate acronyms and add or remove words in the database.
This project is inpired from another project of mine generating bullshit acronyms : BAG. It uses almost the same logic to generate acronyms, except that this module use a database to store and retreive words.
Index
Install
Full Example
Full Example
Usage and Documentation
getAcronym(text)
addWord(word,details)
removeWord(word)
hasWord(word)
findWordsBeginningWith(letter)
positions
types
WordNotFoundError
Install
$ npm install --save acronym-generator
The modules uses a mongoDB database, and mongoose. So before using this module you must install MongoDB.
The document path by the module to store words is ag_Word
.
A mongoose connection must be established before using the module. To do so install mongoose at first :
$ npm install --save mongoose
Full example
This is an example of how to use the acronym-generator
module.
It add three words in the database, ask for an acronym, remove one word from the database.
'use strict'const mongoose = const AG = mongoose // connect your databasemongoosePromise = globalPromise // override the deprecated mongoose Promise mongooseconnection
Note that if we chain again with AG.getAcronym("BTA")
the Promise would be rejected and given an WordNotFoundError
object.
Usage and Documentation
Once you've installed everything, you can open the connection. You should override mongoose.Promise
because they are deprecated, prefer the global.Promise
model from ES6.
'use strict'const mongoose = const AG = mongoosemongoosePromise = globalPromise mongooseconnection
For now on the examples are assuming you connected correctly to the database, and that the mongoose.Promise
are set.
Get an Acronym
You can get an acronym made of the words from your database with :
// return Promise
The parameter is :
text
{string} The word you want to find an acronym for.
This function return a Promise
.
The resolve
callback gets the acronym.
The reject
callback gets a database error, or an instance of WordNotFoundError when the database has not enough word beginning by a specific letter needed in the algorithm.
Note : The algorithm doens't use the same word twice in the same acronym. The acronym for "AA"
can't be "Algorithm Algorithm"
.
Example
const AG = let text = "BTA"// assuming the words "Binary", "Tree" and "Algorithm" are in the database AG // => "Binary Tree Algorithm"
Add a word to the database
You can add a word to the database so that it can be picked to form the future acronyms.
Note : You can't insert two word with the same value, even if the details are different.
// return Promise
The parameters are :
word
{String} The actual word to add to the databasedetails
{Object} Option to add details to the word, to get a better acronym generation
The different values for details
are all optional. If one is not provided, the default value will prevail.
Here are the default values :
preposition : '' // The preposition which fit the word if necessary : "de", "d'", ... type: AGtypesOTHER // The Word's type : takes values from position: AGpositionsANYWHERE // The Word's prefered position in an acronym : takes values from AcronymGenerator.positions
The value for type
and position
must be from the object AG.types and AG.positions.
This function return a Promise
.
The resolve
callback gets the created word.
The reject
callback gets a database error, or a duplication error, or an error saying that the values in parameters some incorrect values.
Example
const AG = let word = "Algorithm"let details = preposition: '' type: AGtypesNOUN position: AGpositionENDAG // db error or duplication error or an error because the input values were not correct
Remove a word from the database
You can remove a word from the database.
// return Promise
The parameter is :
word
{string|Word} The word you want to delete from the database. It can be either the string which value is the word you are looking for, or directly the word Object (that you could have get fromaddWord(word, details)
for example)
This function return a Promise
.
The resolve
callback gets the deleted word.
The reject
callback gets a database error.
Example
const AG = let word = "Algorithm" AG // db error, or the word wasn't found in database
Check the existence of a word
You can check if a word already exists in the database.
// return Promise
The parameter is :
word
{string} The string value of the word you want to check in the database.
This function return a Promise
.
The resolve
callback gets a boolean to true
if the word exists, else false
.
The reject
callback gets a database error.
Example
const AG = let word = "Algorithm" AG // true / false // db error
Find words beginning with some string
You can get all the words stored beginning with a given string :
// return Promise
The parameter is :
letter
{string} The string (mostly letter) that you want to find words beginning with.
This function return a Promise
.
The resolve
callback gets an array of words beginning with th given letter. The length of the array can be 0 if there is no result.
The reject
callback gets a database error.
Example
const AG = const letter = "A"// assuming the words "Algorithm" and "Acronym" are in the database AG/* => GOT WORDS : [{value: 'Algorithm', preposition: '', type: 'NOUN', position: 'END'}, {value: 'Acronym', preposition: '', type: 'NOUN', position: 'END'}]*/
Positions
Contains the available positions for a word.
Use these values in the details.position
parameter of the function addWord(word, details)
to set the preferred position of the word in an acronym.
const AG = console/* =>{ 'ANYWHERE':'ANY', 'START':'START', 'MIDDLE':'MID', 'END':'END'} */
Use these values as following :
- Use
AG.positions.ANYWHERE
when the word can fit anywhere in an acronym (default value) - Use
AG.positions.START
when the word can only be placed at the beginning of the acronym - Use
AG.positions.MIDDLE
when the word can't be placed at the beginning, nor the end of an acronym - Use
AG.positions.END
when the word can only be placed at the end of the word
Types
Contains the available types for a word.
Use these values in the details.type
parameter of the function addWord(word, details)
to set the type of the word.
const AG = console/*=> { 'NOUN':'NOUN', 'PROPER_NOUN':'PR_NOUN', 'ADJECTIVE':'ADJ', 'ADVERB':'ADVB', 'VERB_CONJUGATED':'VB_CJGT', 'VERB_INFINITIVE':'VB_INF', 'OTHER':'OTHER'} */
Use thes values as following :
AG.types.NOUN
when the word's type is nounAG.types.PROPER_NOUN
: when the word's type is a proper nounAG.types.ADJECTIVE
when the word's type is adjectiveAG.types.ADVERB
when the word's type is adverbAG.types.VERB_CONJUGATED
when the word's type is a verb conjugated to the 3rd person singularAG.types.VERB_INFINITIVE
when the word's type is a verb in infinitive formAG.types.OTHER
when the word's type doesn't fit any of these
WordNotFoundError class
WordNotFoundError is returned in the rejected Promise
of the getAcronym(text)
function.
const AG = AGWordNotFoundError // class
Source
It is a simple class containing some information :
{ thismessage = message // the error message thisname = "WordNotFoundError" // the name of the exception thisletter = letter // the letter that triggered the error }
Lasts words...
Feel free to use it !