CalestialAshley35 commited on
Commit
5b26316
·
verified ·
1 Parent(s): b324264

Create app.js

Browse files
Files changed (1) hide show
  1. app.js +133 -0
app.js ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Sample predefined questions and answers
2
+ const predefinedQuestions = [
3
+ { question: "What is your name?", answer: "I am a chatbot!" },
4
+ { question: "How old are you?", answer: "I am timeless!" },
5
+ { question: "Where do you live?", answer: "I live in the cloud!" },
6
+ { question: "What is the weather like?", answer: "I don't know, but you can check online!" },
7
+ { question: "What is the capital of Germany?", answer: "The capital of Germany is Berlin." },
8
+ { question: "What is the tallest building in the world?", answer: "The tallest building in the world is the Burj Khalifa in Dubai, standing at 828 meters." },
9
+ { question: "Can you draw?", answer: "I can't draw pictures, but I can help you with code for drawing using HTML, CSS, and JavaScript!" },
10
+ { question: "What is your favorite hobby?", answer: "I don't have hobbies, but I enjoy assisting with coding and providing knowledge!" },
11
+ { question: "What is the chemical formula for water?", answer: "The chemical formula for water is H2O." },
12
+ { question: "Who invented the lightbulb?", answer: "Thomas Edison is often credited with inventing the lightbulb, though many inventors contributed to its development." },
13
+ { question: "What is the highest-grossing movie of all time?", answer: "As of 2023, the highest-grossing movie of all time is 'Avatar: The Way of Water'." },
14
+ { question: "What is the smallest planet in the solar system?", answer: "The smallest planet in the solar system is Mercury." },
15
+ { question: "What is the fastest plane?", answer: "The fastest plane is the North American X-15, which can reach speeds of up to 4,520 miles per hour." },
16
+ { question: "What is your favorite season?", answer: "I don't have a preference, but many people love spring for its pleasant weather!" },
17
+ { question: "What is the deepest ocean trench?", answer: "The Mariana Trench, located in the Pacific Ocean, is the deepest part of the world's oceans." },
18
+ { question: "How many players are there in a soccer team?", answer: "A standard soccer team consists of 11 players on the field." },
19
+ { question: "What is the longest day of the year?", answer: "The longest day of the year is the summer solstice, which occurs around June 21st in the Northern Hemisphere." },
20
+ { question: "What is the largest desert in the world?", answer: "The largest desert in the world is the Antarctic Desert, which covers about 14 million square kilometers." },
21
+ { question: "Who is the author of 'Harry Potter'?", answer: "The author of the 'Harry Potter' series is J.K. Rowling." },
22
+ { question: "What is the largest animal on land?", answer: "The largest animal on land is the African elephant." },
23
+ { question: "How do you make a paper airplane?", answer: "To make a paper airplane, fold a piece of paper in half lengthwise, then fold the two top corners down to meet the center. Fold the edges to the center to create the wings, and finally, fold the plane in half." },
24
+ { question: "What is the currency of Japan?", answer: "The currency of Japan is the Japanese yen (¥)." },
25
+ { question: "What is the largest planet in the solar system?", answer: "The largest planet in the solar system is Jupiter." },
26
+ { question: "What is the smallest bird in the world?", answer: "The smallest bird in the world is the bee hummingbird, found in Cuba and Isla de la Juventud." },
27
+ { question: "What is the main ingredient in pizza dough?", answer: "The main ingredients in pizza dough are flour, water, yeast, and salt." },
28
+ { question: "What is the definition of gravity?", answer: "Gravity is the force that attracts objects towards the center of the Earth or any other massive body." },
29
+ { question: "What is the largest island in the world?", answer: "Greenland is the largest island in the world." },
30
+ { question: "Who painted the Sistine Chapel?", answer: "The Sistine Chapel ceiling was painted by Michelangelo." },
31
+ { question: "What is the capital of Italy?", answer: "The capital of Italy is Rome." },
32
+ { question: "What is the longest bone in the human body?", answer: "The femur, or thigh bone, is the longest bone in the human body." },
33
+ { question: "What is the most famous clock tower in the world?", answer: "The most famous clock tower is Big Ben, located in London." },
34
+ { question: "What is the largest land animal?", answer: "The largest land animal is the African elephant." },
35
+ { question: "How many states are there in the United States?", answer: "There are 50 states in the United States." },
36
+ { question: "What is the main ingredient in sushi?", answer: "The main ingredient in sushi is vinegared rice, often served with fish, vegetables, or other seafood." },
37
+ { question: "What is the closest star to Earth?", answer: "The closest star to Earth is the Sun." },
38
+ { question: "What is the capital of Spain?", answer: "The capital of Spain is Madrid." },
39
+ { question: "What is the longest highway in the world?", answer: "The longest highway in the world is the Pan-American Highway, stretching from Alaska to Argentina." },
40
+ { question: "How many colors are there in a rainbow?", answer: "There are seven colors in a rainbow: red, orange, yellow, green, blue, indigo, and violet." },
41
+ { question: "Who was the first woman to fly solo across the Atlantic Ocean?", answer: "Amelia Earhart was the first woman to fly solo across the Atlantic Ocean." },
42
+ { question: "What is the fastest-growing plant?", answer: "Bamboo is the fastest-growing plant in the world, capable of growing up to 35 inches (about 90 cm) per day." },
43
+ { question: "What is the capital of Australia?", answer: "The capital of Australia is Canberra." },
44
+ { question: "Who invented the airplane?", answer: "The Wright brothers, Orville and Wilbur Wright, are credited with inventing and flying the first successful airplane." },
45
+ { question: "What is the tallest waterfall in the world?", answer: "The tallest waterfall in the world is Angel Falls in Venezuela, with a height of 979 meters." },
46
+ { question: "What is the most common blood type?", answer: "The most common blood type in the world is O positive." },
47
+ { question: "How many teeth does an adult human have?", answer: "An adult human typically has 32 teeth." },
48
+ { question: "What is the tallest tree in the world?", answer: "The tallest tree in the world is a coast redwood named Hyperion, which stands at 379.7 feet (115.7 meters)." },
49
+ { question: "What is the symbol for gold on the periodic table?", answer: "The symbol for gold on the periodic table is Au." },
50
+ { question: "How many hearts does an octopus have?", answer: "An octopus has three hearts." },
51
+ { question: "What is the hottest planet in our solar system?", answer: "Venus is the hottest planet in our solar system, with surface temperatures over 460°C (860°F)." },
52
+ { question: "What is the biggest city in the world by population?", answer: "As of 2023, the largest city by population is Tokyo, Japan." },
53
+ { question: "What is the smallest mammal in the world?", answer: "The smallest mammal in the world is the bumblebee bat, weighing just 2 grams." },
54
+ { question: "What is the most common element in the Earth's crust?", answer: "The most common element in the Earth's crust is oxygen, making up about 46% of the Earth's crust by weight." },
55
+ { question: "Who is known as the father of modern physics?", answer: "Albert Einstein is known as the father of modern physics, mainly for his theory of relativity." },
56
+ { question: "What is the longest animal?", answer: "The longest animal is the bootlace worm, which can grow over 180 feet (55 meters) long." },
57
+ { question: "What is the largest mammal in the world?", answer: "The blue whale is the largest mammal in the world." },
58
+ { question: "What is the fastest fish?", answer: "The sailfish is the fastest fish, capable of swimming at speeds up to 68 miles per hour." },
59
+ { question: "What is the smallest fish?", answer: "The Stout Floater is considered the smallest fish, measuring about 7.9 mm in length." },
60
+ { question: "What is the most expensive spice in the world?", answer: "Saffron is the most expensive spice in the world." },
61
+ { question: "How many bones are in the human body?", answer: "The human body has 206 bones." },
62
+ { question: "What is the most famous landmark in India?", answer: "The Taj Mahal is the most famous landmark in India." },
63
+ { question: "What is the longest mountain range in the world?", answer: "The Andes Mountain Range in South America is the longest mountain range in the world." },
64
+ { question: "What is the largest waterfall by volume?", answer: "In terms of volume, the Inga Falls in the Congo River are the largest." },
65
+ { question: "What is the coldest planet in our solar system?", answer: "Uranus is the coldest planet in our solar system, with temperatures as low as -224."},
66
+ { question: "What is your favorite color?", answer: "I like all colors!" },
67
+ { question: "What time is it?", answer: "It's always the right time for learning!" },
68
+ { question: "Can you help me with something?", answer: "Of course! What do you need help with?" },
69
+ { question: "What is the meaning of life?", answer: "42" },
70
+ { question: "Tell me a joke", answer: "Why don't scientists trust atoms? Because they make up everything!" },
71
+ { question: "What is the capital of France?", answer: "The capital of France is Paris." },
72
+ // Add more questions and answers here (up to 2000+ questions)
73
+ ];
74
+
75
+ // Levenshtein distance function for fuzzy matching
76
+ function levenshtein(a, b) {
77
+ const tmp = [];
78
+ let i, j;
79
+ for (i = 0; i <= a.length; i++) {
80
+ tmp[i] = [i];
81
+ }
82
+ for (j = 0; j <= b.length; j++) {
83
+ tmp[0][j] = j;
84
+ }
85
+ for (i = 1; i <= a.length; i++) {
86
+ for (j = 1; j <= b.length; j++) {
87
+ tmp[i][j] = Math.min(
88
+ tmp[i - 1][j] + 1, // Deletion
89
+ tmp[i][j - 1] + 1, // Insertion
90
+ tmp[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1) // Substitution
91
+ );
92
+ }
93
+ }
94
+ return tmp[a.length][b.length];
95
+ }
96
+
97
+ // Function for advanced pattern matching
98
+ function matchQuestion() {
99
+ const userQuestion = document.getElementById("questionInput").value.trim().toLowerCase();
100
+ const responseElement = document.getElementById("response");
101
+
102
+ if (!userQuestion) {
103
+ responseElement.textContent = "Please type a question!";
104
+ return;
105
+ }
106
+
107
+ // Step 1: Exact Regular Expression Matching
108
+ const pattern = new RegExp(userQuestion.split(' ').join('.*'), 'i'); // Wildcard match for each word
109
+ let matchedQuestion = predefinedQuestions.find(q => pattern.test(q.question.toLowerCase()));
110
+
111
+ if (matchedQuestion) {
112
+ responseElement.textContent = `I found an exact match: "${matchedQuestion.answer}"`;
113
+ return;
114
+ }
115
+
116
+ // Step 2: Fuzzy Matching (Levenshtein Distance)
117
+ let closestMatch = null;
118
+ let minDistance = Infinity;
119
+ predefinedQuestions.forEach(q => {
120
+ const distance = levenshtein(userQuestion.toLowerCase(), q.question.toLowerCase());
121
+ if (distance < minDistance) {
122
+ minDistance = distance;
123
+ closestMatch = q;
124
+ }
125
+ });
126
+
127
+ // If the distance is acceptable (e.g., <= 5 for a fuzzy match), show the closest match
128
+ if (minDistance <= 5) {
129
+ responseElement.textContent = `I found a close match: "${closestMatch.answer}" (Levenshtein Distance: ${minDistance})`;
130
+ } else {
131
+ responseElement.textContent = "Sorry, I couldn't find a match for your question.";
132
+ }
133
+ }