@scrabble-solver/word-definitions 2.9.1 → 2.9.3

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 (37) hide show
  1. package/LICENSE +1 -1
  2. package/build/getWordDefinition.d.ts +1 -1
  3. package/build/getWordDefinition.js +3 -3
  4. package/build/lib/normalizeDefinition.js +3 -1
  5. package/build/parse/parse.js +2 -2
  6. package/build/parse/parseEnglish.js +7 -7
  7. package/build/parse/parseFrench.js +3 -6
  8. package/build/parse/parseGerman.js +54 -16
  9. package/build/parse/parsePolish.js +3 -6
  10. package/build/parse/parseSpanish.js +3 -6
  11. package/build/types.d.ts +1 -1
  12. package/package.json +5 -5
  13. package/src/getWordDefinition.ts +3 -3
  14. package/src/lib/normalizeDefinition.ts +4 -1
  15. package/src/parse/__tests__/expected/de-DE.hm.json +4 -0
  16. package/src/parse/__tests__/expected/de-DE.ho.json +9 -0
  17. package/src/parse/__tests__/expected/de-DE.kolla.json +4 -0
  18. package/src/parse/__tests__/expected/de-DE.vom.json +16 -0
  19. package/src/parse/__tests__/expected/en-US.awe.json +1 -1
  20. package/src/parse/__tests__/expected/en-US.pawn.json +1 -1
  21. package/src/parse/__tests__/expected/en-US.pawnee.json +1 -1
  22. package/src/parse/__tests__/expected/en-US.pean.json +1 -1
  23. package/src/parse/__tests__/expected/en-US.wiz.json +1 -1
  24. package/src/parse/__tests__/expected/es-ES.corma.json +1 -1
  25. package/src/parse/__tests__/expected/es-ES.portero.json +1 -1
  26. package/src/parse/__tests__/input/de-DE.hm.html +873 -0
  27. package/src/parse/__tests__/input/de-DE.ho.html +1144 -0
  28. package/src/parse/__tests__/input/de-DE.kolla.html +980 -0
  29. package/src/parse/__tests__/input/de-DE.vom.html +738 -0
  30. package/src/parse/parse.test.ts +4 -0
  31. package/src/parse/parse.ts +2 -2
  32. package/src/parse/parseEnglish.ts +8 -4
  33. package/src/parse/parseFrench.ts +3 -3
  34. package/src/parse/parseGerman.ts +71 -12
  35. package/src/parse/parsePolish.ts +3 -3
  36. package/src/parse/parseSpanish.ts +3 -3
  37. package/src/types.ts +1 -1
@@ -10,6 +10,10 @@ export const readTestFile = (filepath: string): string => {
10
10
  };
11
11
 
12
12
  const tests = [
13
+ { locale: Locale.DE_DE, word: 'hm' },
14
+ { locale: Locale.DE_DE, word: 'ho' },
15
+ { locale: Locale.DE_DE, word: 'kolla' },
16
+ { locale: Locale.DE_DE, word: 'vom' },
13
17
  { locale: Locale.EN_US, word: 'awe' },
14
18
  { locale: Locale.EN_US, word: 'pawn' },
15
19
  { locale: Locale.EN_US, word: 'pawnee' },
@@ -19,11 +19,11 @@ const parsePerLocale: Record<Locale, (html: string) => ParseResult> = {
19
19
  };
20
20
 
21
21
  const parse = (locale: Locale, html: string): ParseResult => {
22
- const { definitions, isAllowed } = parsePerLocale[locale](html);
22
+ const { definitions, exists } = parsePerLocale[locale](html);
23
23
 
24
24
  return {
25
25
  definitions: unique(definitions.map(normalizeDefinition).filter(Boolean)),
26
- isAllowed,
26
+ exists,
27
27
  };
28
28
  };
29
29
 
@@ -1,17 +1,21 @@
1
- import cheerio from 'cheerio';
1
+ import { load } from 'cheerio';
2
2
 
3
3
  import { ParseResult } from '../types';
4
4
 
5
+ const DOES_NOT_EXIST_MESSAGE =
6
+ // eslint-disable-next-line max-len
7
+ "The word you've entered isn't in the dictionary. Click on a spelling suggestion below or try again using the search bar above.";
8
+
5
9
  const parseEnglish = (html: string): ParseResult => {
6
- const $ = cheerio.load(html);
10
+ const $ = load(html);
7
11
  $('strong.mw_t_bc').replaceWith(', ');
8
12
  $('.text-lowercase').remove();
9
13
  $('[id^=dictionary-entry]').find('.dtText > *:not(a)').remove();
10
14
  const $definitions = $('[id^=dictionary-entry]').find('.dtText, .cxl-ref');
11
15
 
12
16
  return {
13
- definitions: Array.from($definitions).map((definition) => $(definition).text()),
14
- isAllowed: $definitions.length > 0,
17
+ definitions: Array.from($definitions).map((definition) => $(definition).text().replace(/\n/g, '')),
18
+ exists: $('.spelling-suggestion-text').text().trim() !== DOES_NOT_EXIST_MESSAGE,
15
19
  };
16
20
  };
17
21
 
@@ -1,14 +1,14 @@
1
- import cheerio from 'cheerio';
1
+ import { load } from 'cheerio';
2
2
 
3
3
  import { ParseResult } from '../types';
4
4
 
5
5
  const parseFrench = (html: string): ParseResult => {
6
- const $ = cheerio.load(html);
6
+ const $ = load(html);
7
7
  const $definitions = $('.tlf_cdefinition');
8
8
 
9
9
  return {
10
10
  definitions: Array.from($definitions).map((definition) => $(definition).text()),
11
- isAllowed: $('#vitemselected span').length > 0,
11
+ exists: $('#vitemselected span').length > 0,
12
12
  };
13
13
  };
14
14
 
@@ -1,21 +1,80 @@
1
- import cheerio from 'cheerio';
1
+ import { Cheerio, CheerioAPI, Element, load } from 'cheerio';
2
2
 
3
3
  import { ParseResult } from '../types';
4
4
 
5
5
  const parseGerman = (html: string): ParseResult => {
6
- const $ = cheerio.load(html);
7
- const $meaningOverview = $('.bedeutungsuebersicht');
8
- let $definitions;
9
- if ($meaningOverview.length > 0) {
10
- $definitions = $('.bedeutungsuebersicht > ol > li > a');
11
- } else {
12
- $definitions = $('.dwdswb-lesart .dwdswb-definition');
6
+ const $ = load(html);
7
+
8
+ const definitions = [parseBedeutungsubersicht, parseBedeutungen, parseBedeutung].reduce<string[]>(
9
+ (results, parse) => (results.length === 0 ? parse($) : results),
10
+ [],
11
+ );
12
+ const exists = Array.from($('.label-danger')).every((label) => $(label).text() !== 'Hinweis');
13
+
14
+ return { definitions, exists };
15
+ };
16
+
17
+ const parseBedeutungsubersicht = ($: CheerioAPI): string[] => {
18
+ Array.from($('.bedeutungsuebersicht ol > li > a')).forEach((item) => {
19
+ $(item).text($(item).text().replace(/\n/g, ''));
20
+ });
21
+
22
+ Array.from($('.bedeutungsuebersicht ol > li > ol > li')).forEach((item) => {
23
+ const text = `\n${$(item).text().replace(/\n/g, '')}`;
24
+ const $text = $(`<div>${text}</div>`);
25
+ $(item).replaceWith($text);
26
+ });
27
+
28
+ Array.from($('.bedeutungsuebersicht ol > li > ol')).forEach((list) => {
29
+ const $list = $(list);
30
+ const html = $list.html() || '';
31
+ const $html = $(`<div>${html}</div>`);
32
+ const $prev = $list.prev('a');
33
+
34
+ if ($prev) {
35
+ $prev.append($html);
36
+ $(list).remove();
37
+ } else {
38
+ $(list).replaceWith($html);
39
+ }
40
+ });
41
+
42
+ return parseDefinitions($, $('.bedeutungsuebersicht ol > li'));
43
+ };
44
+
45
+ const parseBedeutung = ($: CheerioAPI): string[] => {
46
+ return parseDefinitions($, $('.dwdswb-lesart .dwdswb-definition-spezifizierung'));
47
+ };
48
+
49
+ const parseBedeutungen = ($: CheerioAPI): string[] => {
50
+ const definitions = parseDefinitions($, $('.dwdswb-lesart .dwdswb-definition'));
51
+
52
+ if (definitions.length > 0) {
53
+ return definitions;
13
54
  }
14
55
 
15
- return {
16
- definitions: Array.from($definitions).map((definition) => $(definition).text().replace('/\n/', '')),
17
- isAllowed: $definitions.length > 0,
18
- };
56
+ const $references = $('.dwdswb-lesart .dwdswb-verweis');
57
+ const references = Array.from($references).reduce<string[]>((result, reference) => {
58
+ const html = reference.attribs['data-content'] || '<span />';
59
+ const values = $(html)
60
+ .text()
61
+ .split(';')
62
+ .map((value) => value.trim());
63
+
64
+ return result.concat(values);
65
+ }, []);
66
+
67
+ return references;
68
+ };
69
+
70
+ const parseDefinitions = ($: CheerioAPI, $definitions: Cheerio<Element>) => {
71
+ return Array.from($definitions).map((definition) =>
72
+ $(definition)
73
+ .text()
74
+ .replace(/[ ]+/g, ' ')
75
+ .replace(/[ ]\n/g, '\n')
76
+ .replace(/^[0-9]+\.\s/g, ''),
77
+ );
19
78
  };
20
79
 
21
80
  export default parseGerman;
@@ -1,16 +1,16 @@
1
- import cheerio from 'cheerio';
1
+ import { load } from 'cheerio';
2
2
 
3
3
  import { ParseResult } from '../types';
4
4
 
5
5
  const parsePolish = (html: string): ParseResult => {
6
- const $ = cheerio.load(html);
6
+ const $ = load(html);
7
7
  const $header = $($('h1')[0]);
8
8
  const $isAllowed = $header.next();
9
9
  const $definitions = $header.next().next().next().next();
10
10
 
11
11
  return {
12
12
  definitions: $definitions.text().trim().split(/\d+\./),
13
- isAllowed: $isAllowed.text().trim().indexOf('dopuszczalne w grach') >= 0,
13
+ exists: $isAllowed.text().trim().indexOf('dopuszczalne w grach') >= 0,
14
14
  };
15
15
  };
16
16
 
@@ -1,9 +1,9 @@
1
- import cheerio from 'cheerio';
1
+ import { load } from 'cheerio';
2
2
 
3
3
  import { ParseResult } from '../types';
4
4
 
5
5
  const parseSpanish = (html: string): ParseResult => {
6
- const $ = cheerio.load(html);
6
+ const $ = load(html);
7
7
  $('.verdBold14 + .gris11 + .gris13').remove();
8
8
  $('br + .gris13').remove();
9
9
  $('.grisItalic13 + .gris13').remove();
@@ -28,7 +28,7 @@ const parseSpanish = (html: string): ParseResult => {
28
28
  .filter(Boolean)
29
29
  .map((definition) => definition.replace(/\s+\.$/g, ''))
30
30
  .map((definition) => (definition.endsWith('.') ? definition : `${definition}.`)),
31
- isAllowed: definitions.length > 0,
31
+ exists: $('.wrapper > p > strong').text() !== 'No se ha encontrado la palabra exacta',
32
32
  };
33
33
  };
34
34
 
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export interface ParseResult {
2
2
  definitions: string[];
3
- isAllowed: boolean;
3
+ exists: boolean;
4
4
  }