Skip to content

Commit

Permalink
feat: start knn
Browse files Browse the repository at this point in the history
calculate distances between N
  • Loading branch information
c0utin committed Sep 29, 2024
1 parent 30c239c commit fe21fba
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
66 changes: 57 additions & 9 deletions src/parsekoto_backend/main.mo
Original file line number Diff line number Diff line change
@@ -1,39 +1,87 @@
import Principal "mo:base/Principal";
import Text "mo:base/Text";
import Int "mo:base/Int";
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Debug "mo:base/Debug";
import Float "mo:base/Float";

import Types "types";

actor {

type networks_interface = actor {
get_raw_data: (Text) -> async Text;
get_stable_raw_text: () -> async Text;
get_stable_raw_json: () -> async Text;
};

var networks : ?networks_interface = null;
var networks: ?networks_interface = null;

public func setup_networks(c : Principal) {
public func setup_networks(c: Principal) {
networks := ?(actor (Principal.toText(c)) : networks_interface);
};

public func fetch_raw_data(url: Text) : async Text {
switch (networks) {
case (null) {
return "Networks not set up"; // Retorna uma mensagem se networks não foi inicializado
return "Networks not set up";
};
case (?n) {
return await n.get_raw_data(url); // Chama a função do canister de redes
return await n.get_raw_data(url);
};
}
};

public func fetch_stable_raw_text() : async Text {
public func fetch_stable_raw_json() : async Text {
switch (networks) {
case (null) {
return "Networks not set up"; // Retorna uma mensagem se networks não foi inicializado
return "Networks not set up";
};
case (?n) {
return await n.get_stable_raw_text(); // Chama a função do canister de redes
return await n.get_stable_raw_json();
};
}
};
}

let mock: [Types.StudentRecord] = [
{
studytime = 10;
higher = "Yes";
absences = 2;
failures = "No";
},
{
studytime = 15;
higher = "No";
absences = 5;
failures = "Yes";
},
{
studytime = 8;
higher = "Yes";
absences = 1;
failures = "No";
}
];

public func calculateDistance(a: Types.StudentRecord, b: Types.StudentRecord): async Float {
let studytimeDiff = Float.abs(Float.fromInt(a.studytime) - Float.fromInt(b.studytime));
let absencesDiff = Float.abs(Float.fromInt(a.absences) - Float.fromInt(b.absences));
return studytimeDiff + absencesDiff;
};

// Função para calcular todas as distâncias
public func calculateAllDistances(newRecord: Types.StudentRecord): async [(Types.StudentRecord, Float)] {
var distances: [(Types.StudentRecord, Float)] = [];

// Calcula a distância entre o novo registro e cada registro do array
for (mockRecord in mock.vals()) { // Corrigido para usar mock.vals()
let distance = await calculateDistance(newRecord, mockRecord); // Use await para chamar a função assíncrona
distances := Array.append(distances, [(mockRecord, distance)]);
};

return distances;
};

};

17 changes: 17 additions & 0 deletions src/parsekoto_backend/types.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module Types {

public type ParseError = {
message : Text;
};

public type Result<T, E> = {
#ok : T;
#err : E;
};

public type Timestamp = Nat64;

public type HttpRequestArgs = {
Expand Down Expand Up @@ -53,5 +62,13 @@ module Types {
http_request : HttpRequestArgs -> async HttpResponsePayload;
};

// Type Example:

public type StudentRecord = {
studytime: Nat;
higher: Text;
absences: Nat;
failures: Text;
};
};

0 comments on commit fe21fba

Please sign in to comment.