-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
115 lines (88 loc) · 3.57 KB
/
api.js
File metadata and controls
115 lines (88 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
async function processText() {
const apiKey = 'DrxJ3ruskXWuMpmF';
const inputText = document.getElementById('inputText').value;
// const sentnum = document.getElementById("sentnum").value;
const multilevel = document.querySelector(".multilevel");
const radios = multilevel.querySelectorAll('input[name="grp1"]');
const selectedRadio = Array.from(radios).find(radio => radio.checked);
const maxsentence = selectedRadio ? selectedRadio.value : '3'; // Default to 6
const formnotetext = document.querySelector(".notetext");
if (!inputText) {
alert('Please enter some text.');
return;
}
// Step 1: Correct Text
const correctedText = await correctText(inputText, apiKey);
console.log(correctedText);
// Step 2: Summarize and Extract Keywords
const { summary, keywords } = await summarizeAndExtract(correctedText, apiKey,maxsentence);
console.log("summary",summary);
console.log("keywords",keywords);
console.log(inputText);
const summaryString = summary.join(' ');
formnotetext.value = "";
formnotetext.value =summaryString;
// console.log("input text", inputText);
// console.log("summarystring", summaryString);
// console.log("keyword", keywords);
return [summaryString, keywords ];
}
async function processText2(inputText) {
const apiKey = "DrxJ3ruskXWuMpmF";
const maxsentence = "5";
if (!inputText) {
throw new Error("Input text is empty.");
}
// Step 1: Correct Text
const correctedText = await correctText(inputText, apiKey);
// Step 2: Summarize and Extract Keywords
const { summary, keywords } = await summarizeAndExtract(correctedText, apiKey, maxsentence);
return summary.join(" ")
}
async function correctText(text, apiKey) {
const url = `https://api.textgears.com/correct?text=${encodeURIComponent(text)}&language=en-GB&key=${apiKey}`;
const response = await fetch(url);
const data = await response.json();
console.log(data);
if (data.status && data.response && data.response.corrected) {
return data.response.corrected; // Use the corrected text from the response
} else {
console.error("Error in correction API:", data);
return text; // Fallback to the original text
}
}
async function summarizeAndExtract(text, apiKey,maxsentence = '6') {
const sanitizedText = text.replace(/[\r\n\t]+/g, ' ').trim();
const url = `https://api.textgears.com/summarize?text=${encodeURIComponent(sanitizedText)}&max_sentences=${maxsentence}&language=en-GB&key=${apiKey}`;
const response = await fetch(url);
try {
const response = await fetch(url);
if (!response.ok) {
console.error(`Error: HTTP ${response.status} ${response.statusText}`);
return { summary: [], keywords: [] };
}
const data = await response.json();
console.log("API Response:", data);
if (data.status === false) {
console.error(`API Error: ${data.description}`);
return { summary: [], keywords: [] }; // Return fallback values
}
if (data.response) {
return {
summary: data.response.summary || [],
keywords: data.response.keywords || []
};
}
} catch (error) {
console.error("Network or Fetch Error:", error);
}
return { summary: [], keywords: [] }; // Default fallback
}
function highlightKeywords(text, keywords) {
let highlightedText = text;
keywords.forEach(keyword => {
const regex = new RegExp(`\\b(${keyword})\\b`, 'gi'); // Match whole words
highlightedText = highlightedText.replace(regex, `<span class="highlight">$1</span>`);
});
return highlightedText;
}