Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mauberti-bc committed Mar 5, 2024
1 parent 2571f2d commit 7410677
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
4 changes: 2 additions & 2 deletions api/src/repositories/taxonomy-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class TaxonomyRepository extends BaseRepository {
async addItisTaxonRecord(
itisTsn: number,
itisScientificName: string,
commonNames: string | null,
commonName: string | null,
itisData: Record<string, unknown>,
itisUpdateDate: string
): Promise<TaxonRecord> {
Expand All @@ -82,7 +82,7 @@ export class TaxonomyRepository extends BaseRepository {
VALUES (
${itisTsn},
${itisScientificName},
${commonNames},
${commonName},
${itisData},
${itisUpdateDate}
)
Expand Down
11 changes: 3 additions & 8 deletions api/src/services/itis-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,19 @@ export class ItisService {
}

/**
* Sorts by exact matches within, ie. Keywords of "Black" and "Bear" would match on "Black Willow"
*
*/

/**
* Cleans up the ITIS search response data.
* Cleans up the ITIS search response data
*
* @param {ItisSolrSearchResponse[]} data
* @memberof ItisService
*/
_sanitizeItisData = (data: ItisSolrSearchResponse[]): TaxonSearchResult[] => {
return data.map((item: ItisSolrSearchResponse) => {
const englishNames = item.commonNames?.filter((name) => name.split('$')[2] === 'English');
const commonNames = englishNames ? englishNames.map((name) => name.split('$')[1]) : null;
const commonNames = englishNames && englishNames.map((name) => name.split('$')[1])

return {
tsn: Number(item.tsn),
commonNames: commonNames || [],
commonNames: commonNames,
scientificName: item.scientificName,
rank: item.rank,
kingdom: item.kingdom
Expand Down
77 changes: 38 additions & 39 deletions api/src/utils/itis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const sortExactMatches = (data: TaxonSearchResult[], searchTerms: string[
const contains = customSortContainsAnyMatchingSearchTerm(data, searchTermsLower);

// Prioritize records where any word in the scientific or common name matches the JOINED search terms
// const someEquals = customSortContainsSearchTermsJoined(contains, searchTermsLower);
const someEquals = customSortContainsSearchTermsJoined(contains, searchTermsLower);

// Prioritize taxa where either the scientific name or any common name CONTAINS the search terms joined
// eg. ['Black', 'bear'] -> "Black bear" matches on "American black bear"
const exactEquals = customSortContainsSearchTermsJoined(contains, searchTermsLower);
const exactEquals = customSortContainsSearchTermsJoined(someEquals, searchTermsLower);

return exactEquals;
};
Expand Down Expand Up @@ -61,43 +61,42 @@ export const customSortContainsAnyMatchingSearchTerm = (
return data.sort(customSort);
};

// /**
// * Sorts the ITIS response such that exact matches with search terms are first
// *
// * @param {ItisSolrSearchResponse[]} data
// * @memberof ItisService
// */
// export const customSortContainsSearchTermsJoined = (
// data: TaxonSearchResult[],
// searchTerms: string[]
// ): TaxonSearchResult[] => {
// // Custom sorting function
// const customSort = (a: TaxonSearchResult, b: TaxonSearchResult) => {
// const aInReference = checkForMatch(a, searchTerms);
// const bInReference = checkForMatch(b, searchTerms);

// if (aInReference && !bInReference) {
// return -1; // Place items from searchTerms before other items
// } else if (!aInReference && bInReference) {
// return 1; // Place other items after items from searchTerms
// } else {
// return 0; // Maintain the original order if both are from searchTerms or both are not
// }
// };

// // Function to check if an item is a match with search terms
// const checkForMatch = (item: TaxonSearchResult, searchTerms: string[]) => {
// // Lowercase commonNames and split into individual words
// const commonNameWords = item.commonNames && item.commonNames.map((name) => name.toLowerCase());

// // Lowercase scientificName and split into individual words
// const scientificNameWords = item.scientificName.toLowerCase().split(/\s+/);

// return commonNameWords?.includes(searchTerms.join(' ')) || scientificNameWords.includes(searchTerms.join(' '));
// };

// return data.sort(customSort);
// };
/**
* Sorts the ITIS response such that exact matches with search terms are first
*
* @param {ItisSolrSearchResponse[]} data
* @memberof ItisService
*/
export const customSortContainsSearchTermsJoined = (
data: TaxonSearchResult[],
searchTerms: string[]
): TaxonSearchResult[] => {
// Custom sorting function
const customSort = (a: TaxonSearchResult, b: TaxonSearchResult) => {
const aInReference = checkForMatch(a, searchTerms);
const bInReference = checkForMatch(b, searchTerms);

if (aInReference && !bInReference) {
return -1; // Place items from searchTerms before other items
} else if (!aInReference && bInReference) {
return 1; // Place other items after items from searchTerms
} else {
return 0; // Maintain the original order if both are from searchTerms or both are not
}
};

// Function to check if an item is a match with search terms
const checkForMatch = (item: TaxonSearchResult, searchTerms: string[]) => {
const commonNameWords = item.commonNames?.map((name) => name.toLowerCase());

// Lowercase scientificName and split into individual words
const scientificNameWords = item.scientificName.toLowerCase().split(/\s+/);

return commonNameWords?.includes(searchTerms.join(' ')) || scientificNameWords.includes(searchTerms.join(' '));
};

return data.sort(customSort);
};

/**
* Sorts the ITIS response such that exact matches with search terms are first
Expand Down

0 comments on commit 7410677

Please sign in to comment.