Skip to content

Commit

Permalink
refac: test removing lodash && remove old as hell difflib library
Browse files Browse the repository at this point in the history
  • Loading branch information
Joabesv committed Nov 1, 2023
1 parent d395d96 commit 0011775
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 34 deletions.
2 changes: 2 additions & 0 deletions apps/queue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@next/common": "workspace:^",
"bullmq": "^4.12.5",
"close-with-grace": "^1.2.0",
"diff": "^5.1.0",
"dotenv": "^16.3.1",
"lodash-es": "^4.17.21",
"ofetch": "^1.1.1",
Expand All @@ -33,6 +34,7 @@
"@next/constants": "workspace:*",
"@next/models": "workspace:*",
"@next/tsconfig": "workspace:^",
"@types/diff": "^5.0.7",
"@types/lodash-es": "^4.17.10",
"tsup": "^7.2.0"
}
Expand Down
37 changes: 21 additions & 16 deletions apps/queue/src/helpers/identifier.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import { createHash } from 'node:crypto';
import { logger } from '@next/common';
import { camelCase, chain } from 'lodash-es';
import { camelCase } from 'lodash-es';
import type { Disciplina } from '@/types/models.js';

/**
* Generates a unique identifier for a given disciplina
* */
export function generateIdentifier(
disciplina: Record<string, unknown>,
keys: string[],
disciplina: Record<string, Disciplina>,
keys: string[] = ['disciplina', 'turno', 'campus', 'turma'],
silent = true,
) {
keys = keys || ['disciplina', 'turno', 'campus', 'turma'];

//TODO: Find a way of doing this without lodash
const disc = chain(disciplina)
.pick(keys)
.mapValues(String)
.mapValues((value: string) => {
camelCase(value);
})
.toPairs()
.sortBy(0)
.fromPairs()
.values()
.value()
// const disc = chain(disciplina)
// .pick(keys)
// .mapValues(String)
// .mapValues((value: string) => {
// camelCase(value);
// })
// .toPairs()
// .sortBy(0)
// .fromPairs()
// .values()
// .value()
// .join('');

// TODO2: See if it behaves the same
const disc = keys
.map((key) => String(disciplina[key]))
.map((value) => camelCase(value)) // Use camelCase
.join('');

if (!silent) {
Expand Down
42 changes: 25 additions & 17 deletions apps/queue/src/helpers/resolveProfessors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SequenceMatcher, getCloseMatches } from 'difflib';
import { diffChars } from 'diff';
import { camelCase, startCase } from 'lodash-es';
import type { Teacher } from '@next/models';
import type { Teacher } from '@/types/models.js';

export function resolveProfessors(
name: string,
Expand All @@ -12,32 +12,40 @@ export function resolveProfessors(
}

const normalizedName = startCase(camelCase(name));
const isNameInvalid =
!normalizedName || ['N D', 'Falso'].includes(normalizedName);

if (
!normalizedName ||
normalizedName === 'N D' ||
normalizedName === 'Falso'
) {
if (isNameInvalid) {
return null;
}

const foundTeacher =
teachers.find((t) => t.name === normalizedName) ||
teachers.find((t) => (t.alias || []).includes(normalizedName));
const isTeacherPresent = (t: Teacher) =>
t.name === normalizedName ?? (t.alias || []).includes(normalizedName);
const foundTeacher = teachers.find((teacher) => isTeacherPresent(teacher));

if (foundTeacher) {
return foundTeacher;
}

const bestMatch = getCloseMatches(
normalizedName,
teachers.map((t) => t.name),
)[0];
let bestMatch: string;
let bestMatchScore = 0;

for (const teacher of teachers) {
const diff = diffChars(normalizedName, teacher.name);
const score = diff.reduce(
(acc, part) => (part.added ? acc : acc + part.count!),
0,
);

const sequenceMatcher = new SequenceMatcher(null, bestMatch, normalizedName);
if (score > bestMatchScore) {
bestMatch = teacher.name;
bestMatchScore = score;
}
}

if (sequenceMatcher.ratio() > 0.8)
const similarityThreshold = 0.8;
if (bestMatchScore > similarityThreshold) {
return teachers.find((t) => t.name === bestMatch);
}

return { error: `Missing Teacher: ${normalizedName}` };
}
27 changes: 27 additions & 0 deletions apps/queue/src/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ export type HistoryDiscipline = {
disciplina: string;
identifier: string;
};

export type Disciplina = {
identifier: string;
obrigatorias: number[];
alunos_matriculados: number[];
before_kick: number[];
after_kick: number[];
year?: number;
quad?: number;
turno?: string;
disciplina?: string;
season?: string;
teoria?: ObjectId;
pratica?: ObjectId;
campus?: string;
turma?: string;
subject?: ObjectId;
codigo?: string;
disciplina_id?: number;
vagas?: number;
ideal_quad?: boolean;
};

export type Teacher = {
alias: string[];
name: string;
};
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0011775

Please sign in to comment.