Skip to content

Commit

Permalink
Fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmaraev committed Sep 11, 2024
1 parent fe117ff commit 9bd1b55
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 63 deletions.
46 changes: 31 additions & 15 deletions src/is.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import InformationState from "./types";
import { objectsEqual, WHQ, findout, consultDB, getFactArgument } from "./utils";
import { InformationState } from "./types";
import {
objectsEqual,
WHQ,
findout,
consultDB,
getFactArgument,
} from "./utils";

export const initialIS = (): InformationState => {
const predicates = { // Mapping from predicate to sort
const predicates: { [index: string]: string } = {
// Mapping from predicate to sort
favorite_food: "food",
booking_course: "course",
};
const individuals = { // Mapping from individual to sort
const individuals: { [index: string]: string } = {
// Mapping from individual to sort
pizza: "food",
LT2319: "course",
};
Expand All @@ -15,7 +23,10 @@ export const initialIS = (): InformationState => {
predicates: predicates,
individuals: individuals,
relevant: (a, q) => {
if (typeof a === "string" && predicates[q.predicate] === individuals[a]) {
if (
typeof a === "string" &&
predicates[q.predicate] === individuals[a]
) {
return true;
}
if (typeof a === "object" && q.predicate === a.predicate) {
Expand All @@ -30,33 +41,38 @@ export const initialIS = (): InformationState => {
return false;
},
combine: (q, a) => {
if (typeof a === "string" && predicates[q.predicate] === individuals[a]) {
return {predicate: q.predicate, argument: a};
if (
typeof a === "string" &&
predicates[q.predicate] === individuals[a]
) {
return { predicate: q.predicate, argument: a };
}
if (typeof a === "object" && q.predicate === a.predicate) {
return a;
}
throw new Error("Combine failed.");
},
plans: [
{
"type": "issue",
"content": WHQ("booking_room"),
"plan": [
type: "issue",
content: WHQ("booking_room"),
plan: [
findout(WHQ("booking_course")),
consultDB(WHQ("booking_room")),
],
}
},
],
},
database: {
consultDB: (question, facts) => {
if (objectsEqual(question, WHQ("booking_room"))) {
const course = getFactArgument(facts, "booking_course");
if (course == "LT2319") {
return {"predicate": "booking_room", "argument": "G212"};
return { predicate: "booking_room", argument: "G212" };
}
}
}
return null;
},
},
next_move: null,
private: {
Expand All @@ -67,8 +83,8 @@ export const initialIS = (): InformationState => {
content: null,
},
],
bel: [{"predicate": "favorite_food", "argument": "pizza"}],
bel: [{ predicate: "favorite_food", argument: "pizza" }],
},
shared: { lu: undefined, qud: [], com: [] },
}
};
};
1 change: 0 additions & 1 deletion src/isu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { DMContext, DMEvent, NextMoveEvent } from "./types";
import { nlg, nlu } from "./nlug";
import { dme } from "./dme";
import { initialIS } from "./is";
import { WHQ } from "./utils";

const inspector = createBrowserInspector();

Expand Down
26 changes: 12 additions & 14 deletions src/nlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const nluMapping: NLUMapping = {
type: "ask",
content: WHQ("favorite_food"),
},
"pizza": {
pizza: {
type: "answer",
content: "pizza",
},
Expand All @@ -29,29 +29,27 @@ const nluMapping: NLUMapping = {
},
};
const nlgMapping: NLGMapping = [
[{ type: "ask", content: WHQ("booking_course") }, "Which course?"],
[{ type: "greet", content: null }, "Hello! You can ask me anything!"],
[
{ "type": "ask", "content": WHQ("booking_course") },
"Which course?",
],
[
{ type: "greet", content: null },
"Hello! You can ask me anything!",
],
[
{ type: "answer", content: {"predicate": "favorite_food", "argument": "pizza"} },
{
type: "answer",
content: { predicate: "favorite_food", argument: "pizza" },
},
"Pizza.",
],
[
{ type: "answer", content: {"predicate": "booking_room", "argument": "G212"} },
{
type: "answer",
content: { predicate: "booking_room", argument: "G212" },
},
"The lecture is in G212.",
],
];

export function nlg(move: Move | null): string {
console.log("generating...", move);
const mapping = nlgMapping.find(
(x) => objectsEqual(x[0], move),
);
const mapping = nlgMapping.find((x) => objectsEqual(x[0], move));
if (mapping) {
return mapping[1];
}
Expand Down
23 changes: 14 additions & 9 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,20 @@ export const rules: Rules = {
const action = is.private.plan[0];
if (action.type === "consultDB") {
const question = action.content as Question;
const propositionFromDB = is.database.consultDB(question, is.shared.com);
return () => ({
...is,
private: {
...is.private,
plan: [...is.private.plan.slice(1)],
bel: [...is.private.bel, propositionFromDB],
},
});
const propositionFromDB = is.database.consultDB(
question,
is.shared.com,
);
if (propositionFromDB) {
return () => ({
...is,
private: {
...is.private,
plan: [...is.private.plan.slice(1)],
bel: [...is.private.bel, propositionFromDB],
},
});
}
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { SpeechStateExternalEvent } from "speechstate";

type Individuals = Predicates;
type Predicates = { [index: string]: string };
export type Domain = {
combine: (q: Question, y: ShortAnswer | Proposition) => Proposition;
relevant: (x: ShortAnswer | Proposition, q: Question) => boolean;
resolves: (x: ShortAnswer | Proposition, q: Question) => boolean;
plans: PlanInfo[];
predicates: Predicates;
individuals: Individuals;
};

export type PlanInfo = {
Expand Down
55 changes: 31 additions & 24 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
export function objectsEqual(obj1, obj2) {
if (obj1 === obj2) {
return true; // same reference or both are null/undefined
}
import { Move, Proposition, Question } from "./types";

if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) {
return false; // primitive values or one of them is null
}
export function objectsEqual(obj1: any, obj2: any) {
if (obj1 === obj2) {
return true; // same reference or both are null/undefined
}

const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (
typeof obj1 !== "object" ||
typeof obj2 !== "object" ||
obj1 === null ||
obj2 === null
) {
return false; // primitive values or one of them is null
}

if (keys1.length !== keys2.length) {
return false; // different number of properties
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);

for (let key of keys1) {
if (!keys2.includes(key) || !objectsEqual(obj1[key], obj2[key])) {
return false; // different properties or values
}
if (keys1.length !== keys2.length) {
return false; // different number of properties
}

for (let key of keys1) {
if (!keys2.includes(key) || !objectsEqual(obj1[key], obj2[key])) {
return false; // different properties or values
}
}

return true;
return true;
}

export function WHQ(predicate) {
export function WHQ(predicate: string): Question {
return {
type: "whq",
predicate: predicate,
}
};
}

export function findout(q) {
export function findout(q: Question): Move {
return {
type: "findout",
content: q,
}
};
}

export function consultDB(q) {
export function consultDB(q: Question): Move {
return {
type: "consultDB",
content: q,
}
};
}

export function getFactArgument(facts, predicate) {
export function getFactArgument(facts: Proposition[], predicate: string) {
for (let fact of facts) {
if (fact.predicate == predicate) {
return fact.argument;
Expand Down

0 comments on commit 9bd1b55

Please sign in to comment.