only_ever_generator 8.4.6 → 8.4.7

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 (119) hide show
  1. package/dist/bootstrap/app.d.ts +6 -3
  2. package/dist/bootstrap/app.d.ts.map +1 -1
  3. package/dist/bootstrap/app.js +11 -17
  4. package/dist/bootstrap/app.js.map +1 -1
  5. package/dist/card_gen/generate_cards.d.ts +2 -3
  6. package/dist/card_gen/generate_cards.d.ts.map +1 -1
  7. package/dist/card_gen/generate_cards.js +22 -15
  8. package/dist/card_gen/generate_cards.js.map +1 -1
  9. package/dist/constants/prompt_data.d.ts +4 -4
  10. package/dist/constants/prompt_data.js +302 -302
  11. package/dist/constants/prompts/card_gen_prompt.js +160 -160
  12. package/dist/constants/prompts/typology_prompt.js +131 -131
  13. package/dist/constants/source_data.d.ts +171 -171
  14. package/dist/constants/source_data.js +973 -973
  15. package/dist/embedding_generation/local_consolidation.js +104 -104
  16. package/dist/helper/build_concept_facts_schema.d.ts +42 -42
  17. package/dist/helper/build_concept_facts_schema.js +44 -44
  18. package/dist/helper/qdrant_db_methods.d.ts.map +1 -1
  19. package/dist/helper/schema_helper/build_card_schema.d.ts +1 -9
  20. package/dist/helper/schema_helper/build_card_schema.d.ts.map +1 -1
  21. package/dist/helper/schema_helper/build_card_schema.js +47 -50
  22. package/dist/helper/schema_helper/build_card_schema.js.map +1 -1
  23. package/dist/helper/schema_helper/build_concept_facts_schema.d.ts +1 -1
  24. package/dist/helper/schema_helper/build_concept_facts_schema.d.ts.map +1 -1
  25. package/dist/helper/schema_helper/build_concept_facts_schema.js +20 -5
  26. package/dist/helper/schema_helper/build_concept_facts_schema.js.map +1 -1
  27. package/dist/helper/schema_helper/build_summary_schema.d.ts +1 -1
  28. package/dist/helper/schema_helper/build_summary_schema.d.ts.map +1 -1
  29. package/dist/helper/schema_helper/build_summary_schema.js +18 -7
  30. package/dist/helper/schema_helper/build_summary_schema.js.map +1 -1
  31. package/dist/index.d.ts +0 -2
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +10 -9
  34. package/dist/index.js.map +1 -1
  35. package/dist/parse/response_format_card.d.ts +176 -176
  36. package/dist/parse/response_format_card.js +371 -371
  37. package/dist/parse/response_format_typology.d.ts +1 -1
  38. package/dist/parse/response_format_typology.js +46 -46
  39. package/dist/services/get_prompts.d.ts +8 -7
  40. package/dist/services/get_prompts.d.ts.map +1 -1
  41. package/dist/services/get_prompts.js +69 -21
  42. package/dist/services/get_prompts.js.map +1 -1
  43. package/dist/typology_gen/generate_concept_facts.d.ts +2 -3
  44. package/dist/typology_gen/generate_concept_facts.d.ts.map +1 -1
  45. package/dist/typology_gen/generate_concept_facts.js +25 -15
  46. package/dist/typology_gen/generate_concept_facts.js.map +1 -1
  47. package/dist/typology_gen/generate_typology.d.ts +2 -1
  48. package/dist/typology_gen/generate_typology.d.ts.map +1 -1
  49. package/dist/typology_gen/generate_typology.js +24 -13
  50. package/dist/typology_gen/generate_typology.js.map +1 -1
  51. package/dist/typology_gen/summarize.d.ts +2 -3
  52. package/dist/typology_gen/summarize.d.ts.map +1 -1
  53. package/dist/typology_gen/summarize.js +24 -13
  54. package/dist/typology_gen/summarize.js.map +1 -1
  55. package/package.json +39 -38
  56. package/src/bootstrap/app.ts +418 -416
  57. package/src/card_gen/generate_cards.ts +347 -345
  58. package/src/config.ts +11 -11
  59. package/src/constants/api_constants.ts +7 -7
  60. package/src/constants/prompts/card_gen_prompt.ts +164 -164
  61. package/src/constants/prompts/typology_prompt.ts +139 -139
  62. package/src/embedding_generation/consolidation/global_consolidation.ts +96 -96
  63. package/src/embedding_generation/consolidation/local_consolidation.ts +141 -141
  64. package/src/embedding_generation/consolidation/write_consolidated_data.ts +98 -98
  65. package/src/embedding_generation/generate_embeddings.ts +42 -42
  66. package/src/embedding_generation/parse_embedding_response.ts +31 -31
  67. package/src/enums/card_type_enum.ts +6 -6
  68. package/src/gap_fill/calculate_gap_fill.ts +50 -50
  69. package/src/helper/get_id_from_title.ts +33 -33
  70. package/src/helper/mongo_helper.ts +29 -29
  71. package/src/helper/openai_helper.ts +20 -20
  72. package/src/helper/qdrant_db_methods.ts +77 -77
  73. package/src/helper/schema_helper/build_card_schema.ts +74 -98
  74. package/src/helper/schema_helper/build_classify_summarize_schema.ts +43 -43
  75. package/src/helper/schema_helper/build_concept_facts_schema.ts +45 -31
  76. package/src/helper/schema_helper/build_summary_schema.ts +43 -32
  77. package/src/index.ts +71 -73
  78. package/src/logger.ts +65 -65
  79. package/src/parse/parse_card/parse_cloze_card.ts +146 -146
  80. package/src/parse/parse_card/parse_flash_cards.ts +42 -42
  81. package/src/parse/parse_card/parse_match_card.ts +104 -104
  82. package/src/parse/parse_card/parse_mcq_card.ts +114 -114
  83. package/src/parse/parse_card_response.ts +197 -197
  84. package/src/parse/parse_source_content.ts +212 -212
  85. package/src/services/get_prompts.ts +164 -112
  86. package/src/services/open_ai_service.ts +89 -89
  87. package/src/services/qdrant_service.ts +10 -10
  88. package/src/types/base_param_type.ts +13 -13
  89. package/src/types/mongo_concept_fact_type.ts +12 -12
  90. package/src/types/parsed_card_type.ts +39 -39
  91. package/src/types/raw_card_response_types/generated_card_response_type.ts +59 -59
  92. package/src/types/source_taxonomy_type.ts +24 -24
  93. package/src/typology-parsed-response.ts +1932 -1932
  94. package/src/typology_gen/generate_concept_facts.ts +180 -169
  95. package/src/typology_gen/generate_typology.ts +203 -189
  96. package/src/typology_gen/summarize.ts +176 -164
  97. package/src/utils/distributed_quote_restoration.ts +80 -80
  98. package/src/utils/generate_args.ts +29 -29
  99. package/src/utils/parse_openai_response.ts +19 -19
  100. package/src/utils/sanitize_strings.ts +65 -65
  101. package/tsconfig.json +16 -16
  102. package/dist/constants/default_generation_variables.d.ts +0 -3
  103. package/dist/constants/default_generation_variables.d.ts.map +0 -1
  104. package/dist/constants/default_generation_variables.js +0 -580
  105. package/dist/constants/default_generation_variables.js.map +0 -1
  106. package/dist/services/prompts_test.d.ts +0 -10
  107. package/dist/services/prompts_test.d.ts.map +0 -1
  108. package/dist/services/prompts_test.js +0 -227
  109. package/dist/services/prompts_test.js.map +0 -1
  110. package/dist/types/generation_variables_schema.d.ts +0 -14
  111. package/dist/types/generation_variables_schema.d.ts.map +0 -1
  112. package/dist/types/generation_variables_schema.js +0 -3
  113. package/dist/types/generation_variables_schema.js.map +0 -1
  114. package/dist/utils/test.d.ts +0 -2
  115. package/dist/utils/test.d.ts.map +0 -1
  116. package/dist/utils/test.js +0 -5
  117. package/dist/utils/test.js.map +0 -1
  118. package/src/constants/default_generation_variables.ts +0 -624
  119. package/src/types/generation_variables_schema.ts +0 -16
@@ -1,146 +1,146 @@
1
- import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
2
- import { RawClozeCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
-
4
- export class ParseClozeCard {
5
- parse(data: {
6
- card_content: RawClozeCardResponseType;
7
- type: string;
8
- concepts_facts: MongoConceptFactCards[];
9
- bloom_level: number;
10
- explanation: string;
11
- }) {
12
- try {
13
- const content = data.card_content;
14
- let correctOptions = content.correct_options;
15
- let incorrectOptions = content.incorrect_options;
16
- let allOptions = [...correctOptions, ...incorrectOptions];
17
- let displayTitle = this._generateClozeCardDisplayTitle(
18
- data.card_content.prompt,
19
- allOptions
20
- );
21
- let preparedData = this._prepareQuestionAndCorrectAnswers(
22
- content.prompt,
23
- correctOptions
24
- );
25
- let finalQuestion = preparedData.prompt;
26
- let parsedCorrectOptions = preparedData.options;
27
- let parsedIncorrectoptions = incorrectOptions.map((e: any) => {
28
- return {
29
- option: e,
30
- cloze: "null",
31
- };
32
- });
33
- const finalParsedOptions = [
34
- ...parsedCorrectOptions,
35
- ...parsedIncorrectoptions,
36
- ];
37
- let clozeCardData = {
38
- type: {
39
- category: "learning",
40
- sub_type: data.type,
41
- },
42
- heading: "",
43
- displayTitle: displayTitle,
44
- content: {
45
- question: finalQuestion,
46
- options: finalParsedOptions,
47
- },
48
- concepts_facts: data.concepts_facts,
49
- bloom_level: data.bloom_level,
50
- explanation: data.explanation,
51
- };
52
-
53
- return this._validateCloze(clozeCardData);
54
- // return clozeCardData;
55
- } catch (e) {
56
- return null;
57
- }
58
- }
59
-
60
- _generateClozeCardDisplayTitle(question: string, answers: Array<any>) {
61
- try {
62
- let optionsString = "";
63
- if (answers.length !== 0) {
64
- optionsString = answers.join(", ");
65
- }
66
-
67
- return `${question} ---- ${optionsString}`;
68
- } catch (e) {
69
- throw Error("Error in generating display title");
70
- }
71
- }
72
-
73
- /// validate the cloze card
74
- // 1. Has Empty cloze
75
- // 2. has Duplicate Clozes
76
- // 3. doesnt have any valid option,
77
- // 4. Question length 320
78
- // 5. More than 6 options
79
- // 6. Less than 2 options
80
- // 7. Max character for individual cloze: 90
81
-
82
- _prepareQuestionAndCorrectAnswers(
83
- rawPrompt: String,
84
- correctOptions: Array<any>
85
- ) {
86
- try {
87
- var finalCorrectOptions = <any>[];
88
- const regex = /{{(.*?)}}/g;
89
-
90
- const transformed = rawPrompt.replace(regex, (match, p1) => {
91
- // p1 is the captured group inside {{ }} (e.g., "fruit", "green")
92
- const idx = correctOptions.indexOf(p1);
93
- if (idx !== -1) {
94
- let cloze = `c${idx}`;
95
- finalCorrectOptions.push({
96
- option: p1,
97
- cloze: cloze,
98
- });
99
- return `{{c${idx}:${p1}}}`;
100
- }
101
- return match; // If not found in correct_options, leave as is or handle accordingly
102
- });
103
- return {
104
- prompt: transformed,
105
- options: finalCorrectOptions,
106
- };
107
- } catch (e) {
108
- throw Error("Error in preparing question and correct answers");
109
- }
110
- }
111
-
112
- _validateCloze(clozeCard: any) {
113
- let clozeRegex = /\{\{c(\d+):([^}]+)\}\}/g;
114
-
115
- try {
116
- /// validate emptu cloze
117
- let options = clozeCard.content.options ?? [];
118
- let question = clozeCard.content.question;
119
- if (options.length < 2 || options.length > 6) {
120
- throw Error("Number of cloze options are invalid");
121
- }
122
-
123
- /// There are no correct clozes// or null cloze or empty cloze
124
- let correctOptions = options.find(
125
- (e: any) => e.cloze != "null" && e.cloze != null && e.cloze.trim() != ""
126
- );
127
- if (correctOptions) {
128
- } else {
129
- throw Error(" No valid clozes exists");
130
- }
131
-
132
- let rightClozes = options.filter((e: any) => e.cloze.startsWith("c"));
133
-
134
- /// matches our cloze syntax
135
- let clozeMatches = [...question.matchAll(clozeRegex)];
136
- if (clozeMatches.length == 0) {
137
- throw Error("Question Invalid");
138
- } else if (clozeMatches.length != rightClozes.length) {
139
- throw Error(" Clozes in question doesnt match to clozes in options");
140
- }
141
- return clozeCard;
142
- } catch (e: any) {
143
- throw Error(`Error in validating cloze card ${e.message}`);
144
- }
145
- }
146
- }
1
+ import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
2
+ import { RawClozeCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
+
4
+ export class ParseClozeCard {
5
+ parse(data: {
6
+ card_content: RawClozeCardResponseType;
7
+ type: string;
8
+ concepts_facts: MongoConceptFactCards[];
9
+ bloom_level: number;
10
+ explanation: string;
11
+ }) {
12
+ try {
13
+ const content = data.card_content;
14
+ let correctOptions = content.correct_options;
15
+ let incorrectOptions = content.incorrect_options;
16
+ let allOptions = [...correctOptions, ...incorrectOptions];
17
+ let displayTitle = this._generateClozeCardDisplayTitle(
18
+ data.card_content.prompt,
19
+ allOptions
20
+ );
21
+ let preparedData = this._prepareQuestionAndCorrectAnswers(
22
+ content.prompt,
23
+ correctOptions
24
+ );
25
+ let finalQuestion = preparedData.prompt;
26
+ let parsedCorrectOptions = preparedData.options;
27
+ let parsedIncorrectoptions = incorrectOptions.map((e: any) => {
28
+ return {
29
+ option: e,
30
+ cloze: "null",
31
+ };
32
+ });
33
+ const finalParsedOptions = [
34
+ ...parsedCorrectOptions,
35
+ ...parsedIncorrectoptions,
36
+ ];
37
+ let clozeCardData = {
38
+ type: {
39
+ category: "learning",
40
+ sub_type: data.type,
41
+ },
42
+ heading: "",
43
+ displayTitle: displayTitle,
44
+ content: {
45
+ question: finalQuestion,
46
+ options: finalParsedOptions,
47
+ },
48
+ concepts_facts: data.concepts_facts,
49
+ bloom_level: data.bloom_level,
50
+ explanation: data.explanation,
51
+ };
52
+
53
+ return this._validateCloze(clozeCardData);
54
+ // return clozeCardData;
55
+ } catch (e) {
56
+ return null;
57
+ }
58
+ }
59
+
60
+ _generateClozeCardDisplayTitle(question: string, answers: Array<any>) {
61
+ try {
62
+ let optionsString = "";
63
+ if (answers.length !== 0) {
64
+ optionsString = answers.join(", ");
65
+ }
66
+
67
+ return `${question} ---- ${optionsString}`;
68
+ } catch (e) {
69
+ throw Error("Error in generating display title");
70
+ }
71
+ }
72
+
73
+ /// validate the cloze card
74
+ // 1. Has Empty cloze
75
+ // 2. has Duplicate Clozes
76
+ // 3. doesnt have any valid option,
77
+ // 4. Question length 320
78
+ // 5. More than 6 options
79
+ // 6. Less than 2 options
80
+ // 7. Max character for individual cloze: 90
81
+
82
+ _prepareQuestionAndCorrectAnswers(
83
+ rawPrompt: String,
84
+ correctOptions: Array<any>
85
+ ) {
86
+ try {
87
+ var finalCorrectOptions = <any>[];
88
+ const regex = /{{(.*?)}}/g;
89
+
90
+ const transformed = rawPrompt.replace(regex, (match, p1) => {
91
+ // p1 is the captured group inside {{ }} (e.g., "fruit", "green")
92
+ const idx = correctOptions.indexOf(p1);
93
+ if (idx !== -1) {
94
+ let cloze = `c${idx}`;
95
+ finalCorrectOptions.push({
96
+ option: p1,
97
+ cloze: cloze,
98
+ });
99
+ return `{{c${idx}:${p1}}}`;
100
+ }
101
+ return match; // If not found in correct_options, leave as is or handle accordingly
102
+ });
103
+ return {
104
+ prompt: transformed,
105
+ options: finalCorrectOptions,
106
+ };
107
+ } catch (e) {
108
+ throw Error("Error in preparing question and correct answers");
109
+ }
110
+ }
111
+
112
+ _validateCloze(clozeCard: any) {
113
+ let clozeRegex = /\{\{c(\d+):([^}]+)\}\}/g;
114
+
115
+ try {
116
+ /// validate emptu cloze
117
+ let options = clozeCard.content.options ?? [];
118
+ let question = clozeCard.content.question;
119
+ if (options.length < 2 || options.length > 6) {
120
+ throw Error("Number of cloze options are invalid");
121
+ }
122
+
123
+ /// There are no correct clozes// or null cloze or empty cloze
124
+ let correctOptions = options.find(
125
+ (e: any) => e.cloze != "null" && e.cloze != null && e.cloze.trim() != ""
126
+ );
127
+ if (correctOptions) {
128
+ } else {
129
+ throw Error(" No valid clozes exists");
130
+ }
131
+
132
+ let rightClozes = options.filter((e: any) => e.cloze.startsWith("c"));
133
+
134
+ /// matches our cloze syntax
135
+ let clozeMatches = [...question.matchAll(clozeRegex)];
136
+ if (clozeMatches.length == 0) {
137
+ throw Error("Question Invalid");
138
+ } else if (clozeMatches.length != rightClozes.length) {
139
+ throw Error(" Clozes in question doesnt match to clozes in options");
140
+ }
141
+ return clozeCard;
142
+ } catch (e: any) {
143
+ throw Error(`Error in validating cloze card ${e.message}`);
144
+ }
145
+ }
146
+ }
@@ -1,42 +1,42 @@
1
- import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
2
- import { RawFlashCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
-
4
- export class ParseFlashCard {
5
- parse(data: {
6
- card_content: RawFlashCardResponseType;
7
- type: string;
8
- concepts_facts: MongoConceptFactCards[];
9
- bloom_level: number;
10
- explanation: string;
11
- }) {
12
- try {
13
- let displayTitle = this.generateFlashCardDisplayTitle(
14
- data.card_content.front,
15
- data.card_content.back
16
- );
17
- let flashCardData = {
18
- type: {
19
- category: "learning",
20
- sub_type: "flash",
21
- },
22
- heading: "",
23
- displayTitle: displayTitle,
24
- content: {
25
- front_content: data.card_content.front,
26
- back_content: data.card_content.back,
27
- },
28
- concepts_facts: data.concepts_facts,
29
- explanation: data.explanation,
30
- bloom_level: data.bloom_level,
31
- };
32
-
33
- return flashCardData;
34
- } catch (e) {
35
- return null;
36
- }
37
- }
38
-
39
- generateFlashCardDisplayTitle(front: string, back: string) {
40
- return `${front} ---- ${back}`;
41
- }
42
- }
1
+ import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
2
+ import { RawFlashCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
+
4
+ export class ParseFlashCard {
5
+ parse(data: {
6
+ card_content: RawFlashCardResponseType;
7
+ type: string;
8
+ concepts_facts: MongoConceptFactCards[];
9
+ bloom_level: number;
10
+ explanation: string;
11
+ }) {
12
+ try {
13
+ let displayTitle = this.generateFlashCardDisplayTitle(
14
+ data.card_content.front,
15
+ data.card_content.back
16
+ );
17
+ let flashCardData = {
18
+ type: {
19
+ category: "learning",
20
+ sub_type: "flash",
21
+ },
22
+ heading: "",
23
+ displayTitle: displayTitle,
24
+ content: {
25
+ front_content: data.card_content.front,
26
+ back_content: data.card_content.back,
27
+ },
28
+ concepts_facts: data.concepts_facts,
29
+ explanation: data.explanation,
30
+ bloom_level: data.bloom_level,
31
+ };
32
+
33
+ return flashCardData;
34
+ } catch (e) {
35
+ return null;
36
+ }
37
+ }
38
+
39
+ generateFlashCardDisplayTitle(front: string, back: string) {
40
+ return `${front} ---- ${back}`;
41
+ }
42
+ }
@@ -1,104 +1,104 @@
1
- import { match } from "assert";
2
- import { RawMatchCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
- import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
4
-
5
- type InputItem = {
6
- left_item: string;
7
- right_item: string;
8
- };
9
-
10
- type OutputItem = {
11
- left_item: string;
12
- right_item: string[];
13
- };
14
-
15
- export class ParseMatchCard {
16
- parse(cardData: {
17
- card_content: RawMatchCardResponseType;
18
- type: string;
19
- concepts_facts: MongoConceptFactCards[];
20
- bloom_level: number;
21
- explanation: string;
22
- }) {
23
- try {
24
- let content = cardData.card_content;
25
- const finalContent = this._parseMatchContent(content);
26
-
27
- let displayTitle = this._generateMatchCardDisplayTitle(content);
28
- let matchCard = {
29
- type: {
30
- category: "learning",
31
- sub_type: cardData.type,
32
- },
33
- heading: "",
34
- content: finalContent,
35
- // content: cardData.card_content,
36
- displayTitle: displayTitle,
37
- concepts_facts: cardData.concepts_facts,
38
- bloom_level: cardData.bloom_level,
39
- explanation: cardData.explanation,
40
- };
41
-
42
- return this._validateMatch(matchCard);
43
- return matchCard;
44
- } catch (e) {
45
- return null;
46
- }
47
- }
48
-
49
- _generateMatchCardDisplayTitle(answers: any) {
50
- let titles: string[] = [];
51
- let counter = 65;
52
- for (let data of answers) {
53
- let value = data.right_item;
54
- let leftData = data.left_item;
55
- let letter = String.fromCharCode(counter);
56
- titles.push(`${letter}. ${leftData} -- ${value}`);
57
- counter++;
58
- }
59
- let displayTitle = titles.join(",");
60
- return displayTitle;
61
- }
62
-
63
- _parseMatchContent = (input: InputItem[]): OutputItem[] => {
64
- const grouped = input.reduce<Record<string, OutputItem>>(
65
- (acc, { left_item, right_item }) => {
66
- if (!acc[left_item]) {
67
- acc[left_item] = { left_item, right_item: [] };
68
- }
69
- acc[left_item].right_item.push(right_item);
70
- return acc;
71
- },
72
- {}
73
- );
74
-
75
- return Object.values(grouped);
76
- };
77
-
78
- _validateMatch(matchCard: any) {
79
- let matches = matchCard.content;
80
- let content = [];
81
- try {
82
- if (matches.length < 1 || matches.length > 4) {
83
- throw Error("Invalid number of matches");
84
- }
85
-
86
- for (let elem of matches) {
87
- if (
88
- (elem.left_item ?? "").length != 0 &&
89
- (elem.right_item ?? "").length != 0
90
- ) {
91
- content.push(elem);
92
- }
93
- }
94
- if (content.length >= 2) {
95
- matchCard.content = content;
96
- } else {
97
- throw Error("Invalid content");
98
- }
99
- return matchCard;
100
- } catch (e) {
101
- return null;
102
- }
103
- }
104
- }
1
+ import { match } from "assert";
2
+ import { RawMatchCardResponseType } from "../../types/raw_card_response_types/generated_card_response_type";
3
+ import { MongoConceptFactCards } from "../../types/mongo_concept_fact_type";
4
+
5
+ type InputItem = {
6
+ left_item: string;
7
+ right_item: string;
8
+ };
9
+
10
+ type OutputItem = {
11
+ left_item: string;
12
+ right_item: string[];
13
+ };
14
+
15
+ export class ParseMatchCard {
16
+ parse(cardData: {
17
+ card_content: RawMatchCardResponseType;
18
+ type: string;
19
+ concepts_facts: MongoConceptFactCards[];
20
+ bloom_level: number;
21
+ explanation: string;
22
+ }) {
23
+ try {
24
+ let content = cardData.card_content;
25
+ const finalContent = this._parseMatchContent(content);
26
+
27
+ let displayTitle = this._generateMatchCardDisplayTitle(content);
28
+ let matchCard = {
29
+ type: {
30
+ category: "learning",
31
+ sub_type: cardData.type,
32
+ },
33
+ heading: "",
34
+ content: finalContent,
35
+ // content: cardData.card_content,
36
+ displayTitle: displayTitle,
37
+ concepts_facts: cardData.concepts_facts,
38
+ bloom_level: cardData.bloom_level,
39
+ explanation: cardData.explanation,
40
+ };
41
+
42
+ return this._validateMatch(matchCard);
43
+ return matchCard;
44
+ } catch (e) {
45
+ return null;
46
+ }
47
+ }
48
+
49
+ _generateMatchCardDisplayTitle(answers: any) {
50
+ let titles: string[] = [];
51
+ let counter = 65;
52
+ for (let data of answers) {
53
+ let value = data.right_item;
54
+ let leftData = data.left_item;
55
+ let letter = String.fromCharCode(counter);
56
+ titles.push(`${letter}. ${leftData} -- ${value}`);
57
+ counter++;
58
+ }
59
+ let displayTitle = titles.join(",");
60
+ return displayTitle;
61
+ }
62
+
63
+ _parseMatchContent = (input: InputItem[]): OutputItem[] => {
64
+ const grouped = input.reduce<Record<string, OutputItem>>(
65
+ (acc, { left_item, right_item }) => {
66
+ if (!acc[left_item]) {
67
+ acc[left_item] = { left_item, right_item: [] };
68
+ }
69
+ acc[left_item].right_item.push(right_item);
70
+ return acc;
71
+ },
72
+ {}
73
+ );
74
+
75
+ return Object.values(grouped);
76
+ };
77
+
78
+ _validateMatch(matchCard: any) {
79
+ let matches = matchCard.content;
80
+ let content = [];
81
+ try {
82
+ if (matches.length < 1 || matches.length > 4) {
83
+ throw Error("Invalid number of matches");
84
+ }
85
+
86
+ for (let elem of matches) {
87
+ if (
88
+ (elem.left_item ?? "").length != 0 &&
89
+ (elem.right_item ?? "").length != 0
90
+ ) {
91
+ content.push(elem);
92
+ }
93
+ }
94
+ if (content.length >= 2) {
95
+ matchCard.content = content;
96
+ } else {
97
+ throw Error("Invalid content");
98
+ }
99
+ return matchCard;
100
+ } catch (e) {
101
+ return null;
102
+ }
103
+ }
104
+ }