Reputation: 15416
I'm stuck conceptually on this problem. I have some data:
const measurements = [
{
value: 162000,
properties: [
{
name: "Selection Status",
value: "Most recent value chosen",
},
{
name: "Measurement Method",
value: "Method of Disks, Single Plane",
},
{
name: "Image Mode",
value: "2D mode",
},
{
name: "Image View",
value: "Apical two chamber",
},
],
},
{
value: 171000,
properties: [
{
name: "Selection Status",
value: "Most recent value chosen",
},
{
name: "Measurement Method",
value: "Method of Disks, Single Plane",
},
{
name: "Image Mode",
value: "2D mode",
},
{
name: "Image View",
value: "Apical four chamber",
},
],
},
{
value: 173000,
properties: [
{
name: "Measurement Method",
value: "Method of Disks, Biplane",
},
{
name: "Image Mode",
value: "2D mode",
},
],
},
{
value: 157000,
properties: [
{
name: "Measurement Method",
value: "Cube Method",
},
{
name: "Image Mode",
value: "2D mode",
},
],
},
{
value: 141000,
properties: [
{
name: "Measurement Method",
value: "Teichholz",
},
{
name: "Image Mode",
value: "2D mode",
},
],
},
];
My goal is to choose the measurement according to some greedy rules. They might look a little like this:
function findMeasurement(
measurements: Measurement[],
filter: MeasurementFilter,
): Measurement | undefined {
return undefined
}
where a MeasurementFilter might look a little like this:
const MATCH_ANYTHING = /.+/;
{
'Image Mode': [/2D mode/, MATCH_ANYTHING],
'Image View': [/Apical four chamber/, /Apical two chamber/, MATCH_ANYTHING],
'Measurement Method': [
/Method of Disks, Biplane/,
/Method of Disks, Single Plane/,
/Teichholz/,
MATCH_ANYTHING,
],
'Selection Status': [/Mean value chosen/, MATCH_ANYTHING],
}
This filter should look for measurements that match from most specific to least specific. So I want to iterate through the data array, and find the measurement that matches most closely. So it would be measurement 2 with value 171000, as it has Apical four chamber
, 2D mode
. I want to match top to bottom in my filter.
Does this make sense? Are there any flaws I am missing? Can anyone get me started on a function implementation/pseudo code?
Upvotes: 1
Views: 77
Reputation:
I dont know a trivial way to achieve this although there should be implementations for this usecase, I dont know them. Two solution approaches:
1) You could try to use existing fuzzy search libraries/solutions like elastic search (available for i.e. MongoDB), store your searchable fields in a search string and try to teach fuzzy search how to split the parts as you need them. Probably feasible if you want fuzzy matching in your query too and dont care too much about exact results.
2) Implement your own matching. Start by implementing a similarity measure and iteratively calculate that measure over the dataset. A very simple measure would be to simply add one to the similarity score for every matching field. Assign the score to the document and sort by score after iterating over the whole collection. Now you have a sorted collection by relevance to your query.
I dont see why it should make any difference whether you match top to bottom vs bottom to top.
Upvotes: 1