skip to Main Content

I am trying to build an app that can find specific ingredients in a food item’s list of ingredients.
I have a list of food ingredients called filtered results

filtered results: [{key: enriched corn meal corn meal, vegetable oil (corn, canola, and/or sunflower oil

I want to find everything in this list that matches my list called desired strings

desired Strings: [ sesame oil, sesamin,  vegetable oil, canola oil]

Currently I am using this method to compare them

  Set<String> lowercaseSet1 =
      cleanedUpStrings.map((e) => e.toLowerCase().trim()).toSet();
  Set<String> lowercaseSet2 =
      desiredStrings.map((e) => e.toLowerCase().trim()).toSet();
  Set<String> commonElements = lowercaseSet1.intersection(lowercaseSet2);

However my commonElements is returning {}
I was able to split the words on spaces in order to compare single word for word and that worked. But the commonElements was returning "vegetable" instead of vegetable oil.

How can I find exact matches with more than one word?

Here are the full lists

filtered results [{key: enriched corn meal (corn meal, ferrous sulfate, niacin, thiamin mononitrate, riboflavin, folic acid), vegetable oil (corn, canola, and/or sunflower 0il), cheese seasoning (whey, cheddar cheese [milk, cheese cultures, salt, enzymes], canola oil, maltodextrin [made from corn], natural and artificial flavors, salt, whey protein concentrate, monosodium glutamate, lactic acid, citric acid. artificial color [yellow 6]), and salt.}, {key: enriched corn meal (corn meal, ferrous sulfate, niacin, thiamin mononitrate, riboflavin, folic acid), vegetable oil (corn, canola, and/or sunflower 0il), cheese seasoning (whey, cheddar cheese [milk, cheese cultures, salt, enzymes], canola oil, maltodextrin [made from corn], natural and artificial flavors, salt, whey protein concentrate, monosodium glutamate, lactic acid, citric acid. artificial color [yellow 6]), and salt.}]

Desired Strings:

I/flutter (14679): desired Strings: [value from home page, seed oils, sunflower oil, sunflower lecithin, sesame oil, sesamin, safflower oil, safflower oleosomes, grapeseed oil, grapeseed extract, flaxseed oil, flaxseed meal, chia seed oil, chia seed gel, pumpkin seed oil, pumpkin seed protein, hemp seed oil, hemp seed protein, poppy seed oil, poppy seed paste, mustard seed oil, mustard seed powder, cottonseed oil, cottonseed meal, vegetable oil, canola oil]

And I am cleaning my api results with this code:

List<String> cleanedUpStrings = filteredResults.toList()
      .map((entry) => entry["key"].toString().toLowerCase())
      .map((s) => s.replaceAll(RegExp(r'[^ws,]'),
          ' ')) // Replace characters other than word characters, spaces, and commas
      .toList();

I am using the cleanedUpStrings for the comparison to remove any case issues.

2

Answers


  1. List list1 = ["AA", "BB", "CC, "DD"]
    List list2 = ["CC", "DD", "EE", "FF"]
    commons = []
    list1.map((word)=>if(list2.contains(word){
    commons.add(word);}))
    
    Login or Signup to reply.
  2. Please use below code for find exact matches with more than one word

      List<String> uniqueMatches = [];
      // Convert to lowercase for case-insensitive matching
      List<String> lowercasedFilterResult = filterResult
          .map((item) => item['key']!.toLowerCase())
          .toList();
    
      for (String desiredString in desiredStrings) {
        // Check if desiredString exists in any item of filterResult
        bool matchFound = lowercasedFilterResult.any((item) =>
            item.contains(desiredString.toLowerCase()));
    
        // If match found and not already in uniqueMatches, add it
        if (matchFound && !uniqueMatches.contains(desiredString)) {
          uniqueMatches.add(desiredString);
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search