NeuralNexusLab-Nh commited on
Commit
adbe503
·
verified ·
1 Parent(s): 8da6181

Update index.cpp

Browse files
Files changed (1) hide show
  1. index.cpp +211 -44
index.cpp CHANGED
@@ -1,20 +1,184 @@
1
- #include <iostream>
2
  #include <string>
3
  #include <vector>
4
  #include <algorithm>
 
5
  #include <cctype>
6
  #include <sstream>
7
- #include <regex>
 
 
8
 
9
- // Structure to hold the return value
10
  struct ZetaResult {
11
  int token;
12
  std::string model;
13
- std::string returnValue;
14
  };
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  ZetaResult zeta(std::string q) {
17
  const std::vector<std::string> db = {
 
 
18
  "Artificial intelligence, or AI, is a technology that allows computers to learn, think, and make decisions like humans. Today, AI is used in many parts of our daily lives. It helps us find information online, recommends songs we might like, and even drives cars automatically.AI can understand language, recognize faces, and solve problems faster than people in some cases. However, AI still needs humans to guide it and make sure it is used safely. Many scientists believe that AI will continue to grow and improve, making our future smarter and more efficient. The key is to use it responsibly and for the benefit of everyone.",
19
  "Developing AI systems requires a mix of math, programming, and creativity. Engineers begin by collecting data, which helps the AI learn patterns and make predictions. They then design a model, usually using machine-learning frameworks like TensorFlow or PyTorch. After training the model, developers test it to make sure it works correctly and safely.AI development is not only about making machines smart—it also involves careful decision-making. Developers must think about privacy, fairness, and how their technology will be used. Building AI responsibly ensures that it can help people while avoiding harmful effects. As the field grows, AI developers continue to create new tools that push technology forward and open the door to future innovations.",
20
  "Cybersecurity is the practice of protecting computers, networks, and data from unauthorized access or attacks. In today's digital world, people store a lot of personal information online, from bank accounts to social media. Hackers can try to steal this information or cause damage.To stay safe, organizations and individuals use strong passwords, firewalls, and encryption. Regular software updates and careful behavior online are also important. Cybersecurity is not just about technology—it is about awareness and responsibility. By following good practices, we can reduce risks and keep our digital world safe.",
@@ -25,74 +189,77 @@ ZetaResult zeta(std::string q) {
25
  "Artificial intelligence has grown rapidly, and many AI models are helping people in different ways. ChatGPT is a language model that can chat, answer questions, and even write text like essays or code. Gemini is another AI developed for understanding language and assisting with research and creative tasks.DeepSeek focuses on analyzing large amounts of data, helping users find patterns and insights quickly. Gemma3 is designed for generating content, such as text, images, or code, making it useful for creative work. Copilot, developed for programmers, helps write code faster by suggesting lines or functions in real time.These AI models show how technology can assist humans in communication, research, and productivity. They are powerful tools, but users should always consider ethics, privacy, and responsible use when using them."
26
  };
27
  //Put article to db array.
28
- std::string raw = q;
29
-
30
- // Convert to lowercase
31
- std::transform(raw.begin(), raw.end(), raw.begin(),
32
- [](unsigned char c) { return std::tolower(c); });
33
-
34
- // Split by spaces
35
- std::vector<std::string> rawWords;
36
- std::istringstream iss(raw);
37
- std::string word;
38
- while (iss >> word) {
39
- rawWords.push_back(word);
40
- }
41
 
 
 
42
  std::string res = "";
43
  int token = 0;
44
 
45
- // Check if first word is a question word and remove it
46
  if (!rawWords.empty()) {
47
- if (rawWords[0] == "do" || rawWords[0] == "what" || rawWords[0] == "who" ||
48
- rawWords[0] == "which" || rawWords[0] == "where" || rawWords[0] == "when" ||
49
- rawWords[0] == "was" || rawWords[0] == "were" || rawWords[0] == "is" ||
50
- rawWords[0] == "are") {
 
51
  rawWords.erase(rawWords.begin());
52
  token += 1;
53
  }
54
  }
55
 
56
- // Filter out common words
57
- std::vector<std::string> filteredWords;
58
- for (const auto& w : rawWords) {
59
- if (w != "is" && w != "are" && w != "the") {
60
- filteredWords.push_back(w);
61
- }
62
- }
63
- rawWords = filteredWords;
64
 
65
- // Get keyword (first word or "nothing")
66
  std::string keyword = rawWords.empty() ? "nothing" : rawWords[0];
67
 
68
- // Remove non-alphabetic characters
69
- keyword = std::regex_replace(keyword, std::regex("[^a-zA-Z\\s]"), "");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  std::string catc = "";
72
 
73
- // Search through database
74
  for (size_t i = 0; i < db.size(); i++) {
75
- std::string dbLower = db[i];
76
- std::transform(dbLower.begin(), dbLower.end(), dbLower.begin(),
77
- [](unsigned char c) { return std::tolower(c); });
78
-
79
- if (dbLower.find(keyword) != std::string::npos) {
80
  catc += db[i];
81
  token += db[i].length();
82
  }
83
  }
84
 
85
- // Build result string
 
 
 
 
86
  res += q + ", about " + keyword + "...\n";
87
  res += catc + "\n";
88
  res += keyword + ", a good discuss for us.";
89
  token += 5;
90
 
91
- // Return struct
92
  ZetaResult result;
93
  result.token = token;
94
- result.model = "zeta-x1-lite";
95
- result.returnValue = res;
96
 
97
  return result;
98
- }
 
 
1
  #include <string>
2
  #include <vector>
3
  #include <algorithm>
4
+ #include <regex>
5
  #include <cctype>
6
  #include <sstream>
7
+ #include <cmath>
8
+ #include <stdexcept>
9
+ #include <limits>
10
 
11
+ // Return type structure for zeta function
12
  struct ZetaResult {
13
  int token;
14
  std::string model;
15
+ std::string return_value;
16
  };
17
 
18
+ // Helper function to convert string to lowercase
19
+ std::string toLowerCase(const std::string& str) {
20
+ std::string result = str;
21
+ std::transform(result.begin(), result.end(), result.begin(),
22
+ [](unsigned char c) { return std::tolower(c); });
23
+ return result;
24
+ }
25
+
26
+ // Helper function to split string by delimiter
27
+ std::vector<std::string> split(const std::string& str, char delimiter) {
28
+ std::vector<std::string> tokens;
29
+ std::stringstream ss(str);
30
+ std::string token;
31
+ while (std::getline(ss, token, delimiter)) {
32
+ if (!token.empty()) {
33
+ tokens.push_back(token);
34
+ }
35
+ }
36
+ return tokens;
37
+ }
38
+
39
+ // Helper function to evaluate mathematical expressions
40
+ double evalMathExpression(const std::string& expr) {
41
+ std::string cleanExpr = expr;
42
+ // Remove spaces
43
+ cleanExpr.erase(std::remove_if(cleanExpr.begin(), cleanExpr.end(), ::isspace), cleanExpr.end());
44
+
45
+ // Simple recursive descent parser for basic arithmetic
46
+ class ExprParser {
47
+ private:
48
+ std::string expr;
49
+ size_t pos;
50
+
51
+ double parseNumber() {
52
+ size_t start = pos;
53
+ if (pos < expr.length() && (expr[pos] == '-' || expr[pos] == '+')) {
54
+ pos++;
55
+ }
56
+ while (pos < expr.length() && (std::isdigit(expr[pos]) || expr[pos] == '.')) {
57
+ pos++;
58
+ }
59
+ if (start == pos) {
60
+ throw std::runtime_error("Expected number");
61
+ }
62
+ return std::stod(expr.substr(start, pos - start));
63
+ }
64
+
65
+ double parseFactor() {
66
+ if (pos < expr.length() && expr[pos] == '(') {
67
+ pos++; // skip '('
68
+ double result = parseExpression();
69
+ if (pos >= expr.length() || expr[pos] != ')') {
70
+ throw std::runtime_error("Expected ')'");
71
+ }
72
+ pos++; // skip ')'
73
+ return result;
74
+ }
75
+ return parseNumber();
76
+ }
77
+
78
+ double parsePower() {
79
+ double left = parseFactor();
80
+ while (pos < expr.length() && expr[pos] == '^') {
81
+ pos++; // skip '^'
82
+ double right = parseFactor();
83
+ left = std::pow(left, right);
84
+ }
85
+ return left;
86
+ }
87
+
88
+ double parseTerm() {
89
+ double left = parsePower();
90
+ while (pos < expr.length() && (expr[pos] == '*' || expr[pos] == '/')) {
91
+ char op = expr[pos];
92
+ pos++;
93
+ double right = parsePower();
94
+ if (op == '*') {
95
+ left *= right;
96
+ } else {
97
+ if (right == 0) {
98
+ throw std::runtime_error("Division by zero");
99
+ }
100
+ left /= right;
101
+ }
102
+ }
103
+ return left;
104
+ }
105
+
106
+ double parseExpression() {
107
+ double left = parseTerm();
108
+ while (pos < expr.length() && (expr[pos] == '+' || expr[pos] == '-')) {
109
+ char op = expr[pos];
110
+ pos++;
111
+ double right = parseTerm();
112
+ if (op == '+') {
113
+ left += right;
114
+ } else {
115
+ left -= right;
116
+ }
117
+ }
118
+ return left;
119
+ }
120
+
121
+ public:
122
+ ExprParser(const std::string& e) : expr(e), pos(0) {}
123
+
124
+ double parse() {
125
+ double result = parseExpression();
126
+ if (pos < expr.length()) {
127
+ throw std::runtime_error("Unexpected character");
128
+ }
129
+ return result;
130
+ }
131
+ };
132
+
133
+ try {
134
+ ExprParser parser(cleanExpr);
135
+ return parser.parse();
136
+ } catch (...) {
137
+ throw;
138
+ }
139
+ }
140
+
141
+ // evalMath function equivalent
142
+ std::pair<bool, double> evalMath(const std::string& input) {
143
+ std::regex pattern("[\\d+\\-*/()^.]+");
144
+ std::smatch match;
145
+
146
+ if (!std::regex_search(input, match, pattern)) {
147
+ return {false, 0.0};
148
+ }
149
+
150
+ std::string expr = match[0].str();
151
+
152
+ // Replace ^ with ** (handled in parser as ^)
153
+ // expr already contains ^, our parser handles it
154
+
155
+ try {
156
+ // Check if expression contains only valid characters
157
+ std::regex validChars("^[\\d+\\-*/().\\s*]+$");
158
+ std::string checkExpr = expr;
159
+ // Replace ^ with empty for validation (we'll handle it in parser)
160
+ std::string exprForCheck = expr;
161
+ std::replace(exprForCheck.begin(), exprForCheck.end(), '^', '*');
162
+
163
+ if (!std::regex_match(exprForCheck, validChars)) {
164
+ return {false, 0.0};
165
+ }
166
+
167
+ double result = evalMathExpression(expr);
168
+
169
+ if (std::isfinite(result)) {
170
+ return {true, result};
171
+ }
172
+ return {false, 0.0};
173
+ } catch (...) {
174
+ return {false, 0.0};
175
+ }
176
+ }
177
+
178
  ZetaResult zeta(std::string q) {
179
  const std::vector<std::string> db = {
180
+ "Who am I? I'm a model trained by NeuralNexusLab and developed by NeuralNexusLab. I can use article to answer you or computation math.",
181
+ "Who are you? I don't know who are you but I know you are my best friend! I can explain what is AI to you, or computation math.",
182
  "Artificial intelligence, or AI, is a technology that allows computers to learn, think, and make decisions like humans. Today, AI is used in many parts of our daily lives. It helps us find information online, recommends songs we might like, and even drives cars automatically.AI can understand language, recognize faces, and solve problems faster than people in some cases. However, AI still needs humans to guide it and make sure it is used safely. Many scientists believe that AI will continue to grow and improve, making our future smarter and more efficient. The key is to use it responsibly and for the benefit of everyone.",
183
  "Developing AI systems requires a mix of math, programming, and creativity. Engineers begin by collecting data, which helps the AI learn patterns and make predictions. They then design a model, usually using machine-learning frameworks like TensorFlow or PyTorch. After training the model, developers test it to make sure it works correctly and safely.AI development is not only about making machines smart—it also involves careful decision-making. Developers must think about privacy, fairness, and how their technology will be used. Building AI responsibly ensures that it can help people while avoiding harmful effects. As the field grows, AI developers continue to create new tools that push technology forward and open the door to future innovations.",
184
  "Cybersecurity is the practice of protecting computers, networks, and data from unauthorized access or attacks. In today's digital world, people store a lot of personal information online, from bank accounts to social media. Hackers can try to steal this information or cause damage.To stay safe, organizations and individuals use strong passwords, firewalls, and encryption. Regular software updates and careful behavior online are also important. Cybersecurity is not just about technology—it is about awareness and responsibility. By following good practices, we can reduce risks and keep our digital world safe.",
 
189
  "Artificial intelligence has grown rapidly, and many AI models are helping people in different ways. ChatGPT is a language model that can chat, answer questions, and even write text like essays or code. Gemini is another AI developed for understanding language and assisting with research and creative tasks.DeepSeek focuses on analyzing large amounts of data, helping users find patterns and insights quickly. Gemma3 is designed for generating content, such as text, images, or code, making it useful for creative work. Copilot, developed for programmers, helps write code faster by suggesting lines or functions in real time.These AI models show how technology can assist humans in communication, research, and productivity. They are powerful tools, but users should always consider ethics, privacy, and responsible use when using them."
190
  };
191
  //Put article to db array.
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
+ std::string raw = toLowerCase(q);
194
+ std::vector<std::string> rawWords = split(raw, ' ');
195
  std::string res = "";
196
  int token = 0;
197
 
198
+ // Check if first word is one of the specified words
199
  if (!rawWords.empty()) {
200
+ std::string firstWord = rawWords[0];
201
+ if (firstWord == "do" || firstWord == "what" || firstWord == "who" ||
202
+ firstWord == "which" || firstWord == "where" || firstWord == "when" ||
203
+ firstWord == "was" || firstWord == "were" || firstWord == "is" ||
204
+ firstWord == "are") {
205
  rawWords.erase(rawWords.begin());
206
  token += 1;
207
  }
208
  }
209
 
210
+ // Filter out "is", "are", "the"
211
+ rawWords.erase(
212
+ std::remove_if(rawWords.begin(), rawWords.end(),
213
+ [](const std::string& word) {
214
+ return word == "is" || word == "are" || word == "the";
215
+ }),
216
+ rawWords.end()
217
+ );
218
 
 
219
  std::string keyword = rawWords.empty() ? "nothing" : rawWords[0];
220
 
221
+ if (keyword == "you" || keyword == "your") {
222
+ keyword = "i";
223
+ } else {
224
+ if (keyword == "i" || keyword == "me") {
225
+ keyword = "you";
226
+ }
227
+ }
228
+
229
+ // Remove non-alphabetic characters (keep spaces)
230
+ keyword.erase(
231
+ std::remove_if(keyword.begin(), keyword.end(),
232
+ [](char c) {
233
+ return !std::isalpha(c) && !std::isspace(c);
234
+ }),
235
+ keyword.end()
236
+ );
237
 
238
  std::string catc = "";
239
 
 
240
  for (size_t i = 0; i < db.size(); i++) {
241
+ std::string dbLower = toLowerCase(db[i]);
242
+ std::string keywordLower = toLowerCase(keyword);
243
+ if (dbLower.find(keywordLower) != std::string::npos) {
 
 
244
  catc += db[i];
245
  token += db[i].length();
246
  }
247
  }
248
 
249
+ std::pair<bool, double> mathResult = evalMath(q);
250
+ if (mathResult.first != false) {
251
+ catc += "\"" + q + "\", the computation's answer is " + std::to_string(mathResult.second) + ".";
252
+ }
253
+
254
  res += q + ", about " + keyword + "...\n";
255
  res += catc + "\n";
256
  res += keyword + ", a good discuss for us.";
257
  token += 5;
258
 
 
259
  ZetaResult result;
260
  result.token = token;
261
+ result.model = "zeta-xf";
262
+ result.return_value = res;
263
 
264
  return result;
265
+ }