Skip to content

Commit

Permalink
BugFix: Common Name Parsing (#246)
Browse files Browse the repository at this point in the history
* Fix common names parsing bug
* Add commonNames parser util function
  • Loading branch information
NickPhura authored Jun 25, 2024
1 parent c8527c0 commit f3bad73
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
7 changes: 3 additions & 4 deletions api/src/services/itis-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios from 'axios';
import { sortTaxonSearchResults } from '../utils/itis-sort';
import { getItisTaxonCommonNames, sortTaxonSearchResults } from '../utils/itis-utils';
import { getLogger } from '../utils/logger';
import { TaxonSearchResult } from './taxonomy-service';

const defaultLog = getLogger('services/itis-service');

export type ItisSolrSearchResponse = {
commonNames: string[];
commonNames?: string[];
kingdom: string;
name: string;
parentTSN: string;
Expand Down Expand Up @@ -83,8 +83,7 @@ export class ItisService {
*/
_sanitizeItisData = (data: ItisSolrSearchResponse[]): TaxonSearchResult[] => {
return data.map((item: ItisSolrSearchResponse) => {
const englishNames = item.commonNames?.filter((name) => name.split('$')[2] === 'English');
const commonNames = englishNames?.map((name) => name.split('$')[1]) ?? [];
const commonNames = getItisTaxonCommonNames(item.commonNames);

return {
tsn: Number(item.tsn),
Expand Down
14 changes: 2 additions & 12 deletions api/src/services/taxonomy-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IDBConnection } from '../database/db';
import { TaxonomyRepository, TaxonRecord } from '../repositories/taxonomy-repository';
import { getItisTaxonCommonNames } from '../utils/itis-utils';
import { getLogger } from '../utils/logger';
import { ItisService, ItisSolrSearchResponse } from './itis-service';

Expand Down Expand Up @@ -72,18 +73,7 @@ export class TaxonomyService {
* @memberof TaxonomyService
*/
async addItisTaxonRecord(itisSolrResponse: ItisSolrSearchResponse): Promise<TaxonRecord> {
const commonNames =
itisSolrResponse.commonNames
.filter((name) => name.split('$')[2] === 'English')
.map((name) => name.split('$')[1]) ?? [];
/* Sample itisResponse:
* commonNames: [
* '$withered wooly milk-vetch$English$N$152846$2012-12-21 00:00:00$',
* '$woolly locoweed$English$N$124501$2011-06-29 00:00:00$',
* '$Davis Mountains locoweed$English$N$124502$2011-06-29 00:00:00$',
* '$woolly milkvetch$English$N$72035$2012-12-21 00:00:00$'
* ]
*/
const commonNames = getItisTaxonCommonNames(itisSolrResponse?.commonNames);

return this.taxonRepository.addItisTaxonRecord(
Number(itisSolrResponse.tsn),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import { TaxonSearchResult } from '../services/taxonomy-service';
import { sortTaxonSearchResults } from './itis-sort';
import { getItisTaxonCommonNames, sortTaxonSearchResults } from './itis-utils';

describe('itis-sort', () => {
describe('sortTaxonSearchResults', () => {
Expand Down Expand Up @@ -78,3 +78,42 @@ describe('itis-sort', () => {
});
});
});

describe('getItisTaxonCommonNames', () => {
it('Returns an empty array if provided common names is undefined', () => {
const rawCommonNames = undefined;

const commonNames = getItisTaxonCommonNames(rawCommonNames);

expect(commonNames).to.eql([]);
});

it('Returns an empty array if the provided common names is empty', () => {
const rawCommonNames: string[] = [];

const commonNames = getItisTaxonCommonNames(rawCommonNames);

expect(commonNames).to.eql([]);
});

it('Returns an array of english common names', () => {
const rawCommonNames = [
'$withered wooly milk-vetch (German)$German$N$152846$2012-12-21 00:00:00$',
'$withered wooly milk-vetch$English$N$152846$2012-12-21 00:00:00$',
'$woolly locoweed$English$N$124501$2011-06-29 00:00:00$',
'$Davis Mountains locoweed (French)$French$N$124502$2011-06-29 00:00:00$',
'$Davis Mountains locoweed$English$N$124502$2011-06-29 00:00:00$',
'$woolly milkvetch$English$N$72035$2012-12-21 00:00:00$',
'$woolly milkvetch (French)$French$N$124502$2011-06-29 00:00:00$'
];

const commonNames = getItisTaxonCommonNames(rawCommonNames);

expect(commonNames).to.eql([
'withered wooly milk-vetch',
'woolly locoweed',
'Davis Mountains locoweed',
'woolly milkvetch'
]);
});
});
21 changes: 21 additions & 0 deletions api/src/utils/itis-sort.ts → api/src/utils/itis-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,24 @@ export const sortTaxonSearchResults = (
// Sort the data by the score
return taxonSearchResults.sort((a, b) => calculateScore(b) - calculateScore(a));
};

/**
* Parse the raw common names string from an ITIS taxon record into an array of english common names.
*
* @example
* const commonNames = [
* '$withered wooly milk-vetch$English$N$152846$2012-12-21 00:00:00$',
* '$woolly locoweed$English$N$124501$2011-06-29 00:00:00$',
* '$Davis Mountains locoweed$English$N$124502$2011-06-29 00:00:00$',
* '$woolly milkvetch$English$N$72035$2012-12-21 00:00:00$'
* ]
*
* const result = getItisTaxonCommonNames(commonNames)
* // result: ['withered wooly milk-vetch', 'woolly locoweed', 'Davis Mountains locoweed', 'woolly milkvetch']
*
* @param {string[]} [commonNames]
* @memberof TaxonomyService
*/
export const getItisTaxonCommonNames = (commonNames?: string[]): string[] => {
return commonNames?.filter((name) => name.split('$')[2] === 'English').map((name) => name.split('$')[1]) ?? [];
};

0 comments on commit f3bad73

Please sign in to comment.