Skip to content

Commit 26f2e4e

Browse files
Add category determination guidelines and refactor report category logic
- Introduced a new section in general.mdc outlining guidelines for determining categories based on keywords. - Refactored the determineCategory function in reportService.ts to utilize a constant mapping object for improved readability and maintainability. - Replaced multiple if-else statements with a more efficient approach using Array methods for keyword matching.
1 parent 6629769 commit 26f2e4e

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

.cursor/rules/general.mdc

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
description: Follow this rules for every request
33
globs:
4+
alwaysApply: false
45
---
56

67
- Project Proposal Overview: This project proposes an AI-powered medical report translator that simplifies complex medical documents for patients and caregivers. By leveraging AI-driven text extraction and natural language processing (NLP), the system translates medical jargon into plain language, helping users understand their health conditions, diagnoses, and test results without relying on unreliable online searches.
@@ -124,10 +125,37 @@ AWS architecture: [aws architecture.pdf](mdc:docs/assets/aws architecture.pdf)
124125
```
125126
```
126127

128+
# General Code Guidelines
129+
130+
## Category Determination Pattern
131+
When determining categories based on keywords in filenames or text:
132+
133+
1. Define a constant mapping object at module level that maps categories to their identifying keywords
134+
2. Use TypeScript's Record type to ensure type safety
135+
3. Convert input to lowercase once at the start
136+
4. Use Array methods like `find` and `some` for clean keyword matching
137+
5. Provide a default/fallback category
138+
6. Include JSDoc with clear parameter and return descriptions
139+
140+
Example:
141+
```typescript
142+
const CATEGORY_KEYWORDS: Record<Category, string[]> = {
143+
[Category.TYPE_A]: ['keyword1', 'keyword2'],
144+
[Category.TYPE_B]: ['keyword3', 'keyword4'],
145+
[Category.DEFAULT]: []
146+
};
147+
148+
const determineCategory = (input: string): Category => {
149+
const lowerInput = input.toLowerCase();
150+
const matchedCategory = Object.entries(CATEGORY_KEYWORDS)
151+
.find(([_, keywords]) => keywords.some(k => lowerInput.includes(k)));
152+
return matchedCategory ? (matchedCategory[0] as Category) : Category.DEFAULT;
153+
};
154+
```
155+
127156
# Typescript rules
128157

129158
- Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
130159

131160
This rule provides clear guidelines on what units to use, how to convert between units, and why it's important for your project. You can add this to your general rules to ensure consistency across the codebase.
132161

133-
Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.

frontend/src/common/api/reportService.ts

+20-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ export interface UploadProgressCallback {
3232
(progress: number): void;
3333
}
3434

35+
/**
36+
* Maps categories to their identifying keywords for report classification
37+
*/
38+
const CATEGORY_KEYWORDS = {
39+
[ReportCategory.HEART]: ['heart', 'cardiac', 'stress'],
40+
[ReportCategory.NEUROLOGICAL]: ['brain', 'neuro'],
41+
[ReportCategory.OFTALMOLOGICAL]: ['eye', 'vision', 'optic'],
42+
[ReportCategory.GASTRO]: ['stomach', 'gastro', 'digestive'],
43+
[ReportCategory.ORTHOPEDIC]: ['bone', 'joint', 'skeletal'],
44+
[ReportCategory.GENERAL]: []
45+
} as const;
46+
3547
/**
3648
* Uploads a medical report file
3749
* @param file - The file to upload
@@ -121,25 +133,18 @@ export const uploadReport = async (
121133
};
122134

123135
/**
124-
* Determines a report category based on filename
125-
* This is just for mock demonstration
136+
* Determines a report category based on filename keywords
137+
* @param filename - Name of the file to categorize
138+
* @returns The determined report category
126139
*/
127140
const determineCategory = (filename: string): ReportCategory => {
128141
const lowerFilename = filename.toLowerCase();
129142

130-
if (lowerFilename.includes('heart') || lowerFilename.includes('cardiac') || lowerFilename.includes('stress')) {
131-
return ReportCategory.HEART;
132-
} else if (lowerFilename.includes('brain') || lowerFilename.includes('neuro')) {
133-
return ReportCategory.NEUROLOGICAL;
134-
} else if (lowerFilename.includes('eye') || lowerFilename.includes('vision')) {
135-
return ReportCategory.OFTALMOLOGICAL;
136-
} else if (lowerFilename.includes('stomach') || lowerFilename.includes('gastro')) {
137-
return ReportCategory.GASTRO;
138-
} else if (lowerFilename.includes('bone') || lowerFilename.includes('joint')) {
139-
return ReportCategory.ORTHOPEDIC;
140-
}
141-
142-
return ReportCategory.GENERAL;
143+
const matchedCategory = Object.entries(CATEGORY_KEYWORDS).find(([_, keywords]) =>
144+
keywords.some(keyword => lowerFilename.includes(keyword))
145+
);
146+
147+
return matchedCategory ? (matchedCategory[0] as ReportCategory) : ReportCategory.GENERAL;
143148
};
144149

145150
/**

0 commit comments

Comments
 (0)