smart-word-generator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +1 -0
  2. package/index.js +159 -0
  3. package/package.json +28 -0
  4. package/utils.js +33 -0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # smart-word-generator
package/index.js ADDED
@@ -0,0 +1,159 @@
1
+ const axios = require("axios");
2
+ const { wordOptionalParams, decorateWords } = require("./utils");
3
+
4
+ /**
5
+ * Generate words that have similar meaning of input word
6
+ * @param {String} word The input word
7
+ * @param {Boolean} transformToArray Specify if true then return type array of words otherwise array of objects include extra details
8
+ * @param {String} partOfSpeech Optional param to specify the part-Of-Speech of returned words 'n' for nouns, 'v' for verbs, 'adj' for adjectives and 'adv' for adverbs
9
+ * @param {String} topics Optional param to specify which category of the returned words belong to
10
+ * @param {String} startWith Optional param to specify what returned words should start with
11
+ * @param {String} endWith Optional param to specify what returned words should end with
12
+ * @param {String} max Optional param to specify number of returned words
13
+ */
14
+ const generateWordMeansLike = async (
15
+ word,
16
+ transformToArray = false,
17
+ partOfSpeech,
18
+ topics,
19
+ startWith,
20
+ endWith,
21
+ max
22
+ ) => {
23
+ try {
24
+ const optionalParams = wordOptionalParams(topics, startWith, endWith, max);
25
+ let response = await axios.get(
26
+ `https://api.datamuse.com/words?ml=${word}&${optionalParams}`
27
+ );
28
+ return decorateWords(partOfSpeech, transformToArray, response);
29
+ } catch (err) {
30
+ console.error(err);
31
+ }
32
+ };
33
+
34
+ /**
35
+ * Generate words that sounds like input word
36
+ * @param {String} word The input word
37
+ * @param {Boolean} transformToArray Specify if true then return type array of words otherwise array of objects include extra details
38
+ * @param {String} partOfSpeech Optional param to specify the part-Of-Speech of returned words 'n' for nouns, 'v' for verbs, 'adj' for adjectives and 'adv' for adverbs
39
+ * @param {String} topics Optional param to specify which category of the returned words belong to
40
+ * @param {String} startWith Optional param to specify what returned words should start with
41
+ * @param {String} endWith Optional param to specify what returned words should end with
42
+ * @param {String} max Optional param to specify number of returned words
43
+ */
44
+ const generateWordSoundsLike = async (
45
+ word,
46
+ transformToArray = false,
47
+ partOfSpeech,
48
+ topics,
49
+ startWith,
50
+ endWith,
51
+ max
52
+ ) => {
53
+ try {
54
+ const optionalParams = wordOptionalParams(topics, startWith, endWith, max);
55
+ let response = await axios.get(
56
+ `https://api.datamuse.com/words?sl=${word}&${optionalParams}`
57
+ );
58
+ return decorateWords(partOfSpeech, transformToArray, response);
59
+ } catch (err) {
60
+ console.error(err);
61
+ }
62
+ };
63
+
64
+ /**
65
+ * Generate words that spelled like input word
66
+ * @param {String} word The input word
67
+ * @param {Boolean} transformToArray Specify if true then return type array of words otherwise array of objects include extra details
68
+ * @param {String} partOfSpeech Optional param to specify the part-Of-Speech of returned words 'n' for nouns, 'v' for verbs, 'adj' for adjectives and 'adv' for adverbs
69
+ * @param {String} topics Optional param to specify which category of the returned words belong to
70
+ * @param {String} startWith Optional param to specify what returned words should start with
71
+ * @param {String} endWith Optional param to specify what returned words should end with
72
+ * @param {String} max Optional param to specify number of returned words
73
+ */
74
+ const generateWordSpelledLike = async (
75
+ word,
76
+ transformToArray = false,
77
+ partOfSpeech,
78
+ topics,
79
+ startWith,
80
+ endWith,
81
+ max
82
+ ) => {
83
+ try {
84
+ const optionalParams = wordOptionalParams(topics, startWith, endWith, max);
85
+ let response = await axios.get(
86
+ `https://api.datamuse.com/words?sp=${word}&${optionalParams}`
87
+ );
88
+ return decorateWords(partOfSpeech, transformToArray, response);
89
+ } catch (err) {
90
+ console.error(err);
91
+ }
92
+ };
93
+
94
+ /**
95
+ * Generate words that have left context of input word
96
+ * @param {String} word The input word
97
+ * @param {Boolean} transformToArray Specify if true then return type array of words otherwise array of objects include extra details
98
+ * @param {String} partOfSpeech Optional param to specify the part-Of-Speech of returned words 'n' for nouns, 'v' for verbs, 'adj' for adjectives and 'adv' for adverbs
99
+ * @param {String} topics Optional param to specify which category of the returned words belong to
100
+ * @param {String} startWith Optional param to specify what returned words should start with
101
+ * @param {String} endWith Optional param to specify what returned words should end with
102
+ * @param {String} max Optional param to specify number of returned words
103
+ */
104
+ const generateWordHasLeftContext = async (
105
+ word,
106
+ transformToArray = false,
107
+ partOfSpeech,
108
+ topics,
109
+ startWith,
110
+ endWith,
111
+ max
112
+ ) => {
113
+ try {
114
+ const optionalParams = wordOptionalParams(topics, startWith, endWith, max);
115
+ let response = await axios.get(
116
+ `https://api.datamuse.com/words?lc=${word}&${optionalParams}`
117
+ );
118
+ return decorateWords(partOfSpeech, transformToArray, response);
119
+ } catch (err) {
120
+ console.error(err);
121
+ }
122
+ };
123
+
124
+ /**
125
+ * Generate words that have right context of input word
126
+ * @param {String} word The input word
127
+ * @param {Boolean} transformToArray Specify if true then return type array of words otherwise array of objects include extra details
128
+ * @param {String} partOfSpeech Optional param to specify the part-Of-Speech of returned words 'n' for nouns, 'v' for verbs, 'adj' for adjectives and 'adv' for adverbs
129
+ * @param {String} topics Optional param to specify which category of the returned words belong to
130
+ * @param {String} startWith Optional param to specify what returned words should start with
131
+ * @param {String} endWith Optional param to specify what returned words should end with
132
+ * @param {String} max Optional param to specify number of returned words
133
+ */
134
+ const generateWordHasRightContext = async (
135
+ word,
136
+ transformToArray = false,
137
+ partOfSpeech,
138
+ topics,
139
+ startWith,
140
+ endWith,
141
+ max
142
+ ) => {
143
+ try {
144
+ const optionalParams = wordOptionalParams(topics, startWith, endWith, max);
145
+ let response = await axios.get(
146
+ `https://api.datamuse.com/words?rc=${word}&${optionalParams}`
147
+ );
148
+ return decorateWords(partOfSpeech, transformToArray, response);
149
+ } catch (err) {
150
+ console.error(err);
151
+ }
152
+ };
153
+ module.exports = {
154
+ generateWordMeansLike,
155
+ generateWordSoundsLike,
156
+ generateWordSpelledLike,
157
+ generateWordHasLeftContext,
158
+ generateWordHasRightContext,
159
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "smart-word-generator",
3
+ "version": "1.0.0",
4
+ "description": "Generate specific words depending on your needs",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Mahmoud-Magdy-deeplearning/smart-word-generator.git"
12
+ },
13
+ "keywords": [
14
+ "word",
15
+ "generator",
16
+ "AI",
17
+ "NLP"
18
+ ],
19
+ "author": "Mahmoud Magdy",
20
+ "license": "ISC",
21
+ "bugs": {
22
+ "url": "https://github.com/Mahmoud-Magdy-deeplearning/smart-word-generator/issues"
23
+ },
24
+ "homepage": "https://github.com/Mahmoud-Magdy-deeplearning/smart-word-generator#readme",
25
+ "dependencies": {
26
+ "axios": "^1.2.1"
27
+ }
28
+ }
package/utils.js ADDED
@@ -0,0 +1,33 @@
1
+ const wordOptionalParams = (topics, startWith, endWith, max) => {
2
+ max = max ? max : "";
3
+ topics = topics ? topics : "";
4
+ endWith = endWith ? endWith : "";
5
+ startWith = startWith ? startWith : "";
6
+ return `sp=${startWith + "*" + endWith}&max=${max}&topics=${topics}&md=p`;
7
+ };
8
+ const filterWords = (words, type) => {
9
+ const filteredWords = words.data.filter((word) => {
10
+ const tags = word.tags.filter((tag) => tag === type);
11
+ return tags.length;
12
+ });
13
+ return filteredWords;
14
+ };
15
+ const arrayOfWordsTransformer = (words) => {
16
+ const arr = [];
17
+ words.map((wordObject) => {
18
+ arr.push(wordObject.word);
19
+ });
20
+ return arr;
21
+ };
22
+
23
+ const decorateWords = (partOfSpeech, transformToArray, words) => {
24
+ if (partOfSpeech) words = partOfSpeech && filterWords(words, partOfSpeech);
25
+ else
26
+ words = words.data.map((word) => {
27
+ return word;
28
+ });
29
+ if (transformToArray) words = arrayOfWordsTransformer(words);
30
+ return words;
31
+ };
32
+
33
+ module.exports = { wordOptionalParams, decorateWords };