Home > Software engineering >  Printing out all words which are not included in blacklist
Printing out all words which are not included in blacklist

Time:11-30

I am trying to parse my steam inventory. I have several service medals, operation coins and etc which i want exclude from final result. Medals in api have following namings:

  • 2017 Service Medal
  • Loyalty Badge
  • 2019 Service Medal
  • Gold Operation Shattered Web Coin
  • Storage Unit
  • 5 Year Veteran Coin
  • 10 Year Birthday Coin
  • Antwerp 2022 Gold Coin
  • Stockholm 2021 Gold Coin

I am included all this keywords(not full names) in the hashset

HashSet<string> oybek = new()
{
"Service Medal",
"Loyalty Badge",
"Gold Operation",
"Silver Operation",
"Diamond Operation",
"Coin",
"Storage Unit"
};

below i am reading text in which i in advance write api response(cuz not hit rate limit)

string _ = string.Empty;
string path = @"C:\Users\Oybek\source\repos\ConsoleApp8\myfile.txt";
using (StreamReader reader = new(path))
{
    string text = await reader.ReadToEndAsync();
    _ = text;
}
var obj = JsonConvert.DeserializeObject(_) as JObject;
var descriptions = obj!["descriptions"]!.ToList();
Console.OutputEncoding = Encoding.UTF8;
foreach (var item in descriptions)
{
    Console.WriteLine(item["market_hash_name"]);
}

descriptions will have the next content:

  • Sticker | FaZe Clan (Glitter) | Antwerp 2022
  • Sticker | Cloud9 | Antwerp 2022
  • Sticker | Heroic | Antwerp 2022
  • Sticker | FaZe Clan | Antwerp 2022
  • Sticker | Copenhagen Flames | Antwerp 2022
  • Sticker | FURIA | Antwerp 2022
  • Sticker | FaZe Clan (Holo) | Antwerp 2022
  • Sticker | Ninjas in Pyjamas | Antwerp 2022
  • Sticker | G2 Esports | Antwerp 2022
  • Sticker | PGL | Antwerp 2022
  • M4A1-S | Decimator (Field-Tested)
  • MP9 | Food Chain (Field-Tested)
  • Antwerp 2022 Contenders Autograph Capsule
  • Antwerp 2022 Legends Autograph Capsule
  • Antwerp 2022 Challengers Autograph Capsule
  • Bayonet | Scorched (Field-Tested)
  • USP-S | Cortex (Field-Tested)
  • 10 Year Birthday Coin
  • UMP-45 | Primal Saber (Field-Tested)
  • P90 | Teardown (Minimal Wear)
  • Sticker | TYLOO (Foil) | 2020 RMR
  • Sticker | exit (Gold) | Antwerp 2022
  • Chef d'Escadron Rouchard | Gendarmerie Nationale
  • P90 | Freight (Battle-Scarred)
  • AWP | POP AWP (Factory New)
  • Sticker | huNter (Glitter) | Antwerp 2022
  • Sticker | m0NESY (Glitter) | Antwerp 2022
  • Antwerp 2022 Champions Autograph Capsule
  • Antwerp 2022 Legends Sticker Capsule
  • SSG 08 | Dragonfire (Minimal Wear)
  • P250 | Franklin (Minimal Wear)
  • USP-S | Orange Anolis (Factory New)
  • Desert Eagle | Mecha Industries (Field-Tested)
  • Antwerp 2022 Contenders Sticker Capsule
  • Antwerp 2022 Contenders Autograph Capsule
  • Antwerp 2022 Legends Autograph Capsule
  • Antwerp 2022 Challengers Sticker Capsule
  • Antwerp 2022 Challengers Autograph Capsule
  • Sticker | Gambit Gaming (Gold) | Stockholm 2021
  • Sticker | Virtus.Pro (Gold) | Stockholm 2021
  • Souvenir P250 | Drought (Factory New)
  • Antwerp 2022 Gold Coin
  • Stockholm 2021 Contenders Sticker Capsule
  • Sticker | Gambit Gaming (Holo) | Stockholm 2021
  • Sticker | Virtus.Pro (Holo) | Stockholm 2021
  • Stockholm 2021 Champions Autograph Capsule
  • AK-47 | The Empress (Field-Tested)
  • USP-S | Purple DDPAT (Factory New)
  • Galil AR | Amber Fade (Factory New)
  • FAMAS | Mecha Industries (Minimal Wear)
  • Five-SeveN | Berries And Cherries (Factory New)
  • 'Medium Rare' Crasswater | Guerrilla Warfare
  • Galil AR | Chromatic Aberration (Minimal Wear)
  • MAG-7 | Justice (Field-Tested)
  • XM1014 | Seasons (Factory New)
  • MAC-10 | Propaganda (Minimal Wear)
  • Glock-18 | Snack Attack (Field-Tested)
  • Dual Berettas | Cobalt Quartz (Minimal Wear)
  • Sticker | Fire Serpent Surf K (Foil)
  • Sticker | Toxic Flow (Holo)
  • Sticker | Sticker Bomb Surf K (Foil)
  • Sticker | Akihabara Accept Surf Ava (Foil)
  • Sticker | Dark Water Surf Ava (Foil)
  • Sticker | Dragon Lore Surf Ava (Foil)
  • Sticker | Hypnotic Surf K (Foil)
  • Sticker | Sticker Bomb Surf Ava (Foil)
  • Sticker | Blaze Surf K (Foil)
  • 2022 Service Medal
  • Sticker | Liquid Fire (Holo)
  • StatTrak™ SG 553 | Darkwing (Field-Tested)
  • AUG | Stymphalian (Minimal Wear)
  • Sticker | Kill Count (Holo)
  • Sticker | Copenhagen Flames (Holo) | Stockholm 2021
  • R8 Revolver | Blaze (Factory New)
  • Sticker | Entropiq (Holo) | Stockholm 2021
  • Stockholm 2021 Finalists Autograph Capsule
  • Stockholm 2021 Champions Autograph Capsule
  • Stockholm 2021 Contenders Sticker Capsule
  • Stockholm 2021 Legends Sticker Capsule
  • Sticker | Watermelon Strafe (Holo)
  • M4A1-S | Fizzy POP (Field-Tested)
  • Negev | Infrastructure (Field-Tested)
  • Stockholm 2021 Gold Coin
  • AWP | Pit Viper (Field-Tested)
  • Sticker | Neon Opal Strafe (Holo)
  • Sticker | Great Wave (Holo)
  • Diamond Operation Riptide Coin
  • 2020 RMR Challengers
  • 2020 RMR Legends
  • 2020 RMR Contenders
  • 5 Year Veteran Coin
  • 2021 Service Medal
  • Diamond Operation Broken Fang Coin
  • Sticker | Chief (Foil)
  • Storage Unit
  • Sticker | Web Stuck (Holo)
  • Sticker | Mastermind (Holo)
  • 2020 Service Medal
  • Gold Operation Shattered Web Coin
  • 2019 Service Medal
  • Loyalty Badge
  • 2017 Service Medal

and i need exclude item names which names contains words from my hashset named oybek.

I am expecting output without Medals names.

I am tried next: i am created list List<string> list = new() for saving inside item names. and then

string _ = string.Empty;
string path = @"C:\Users\Oybek\source\repos\ConsoleApp8\myfile.txt";
using (StreamReader reader = new(path))
{
    string text = await reader.ReadToEndAsync();
    _ = text;
}
var obj = JsonConvert.DeserializeObject(_) as JObject;
var descriptions = obj!["descriptions"]!.ToList();
Console.OutputEncoding = Encoding.UTF8;
List<string> list = new();
foreach (var item in descriptions)
{
    list.Add(item["market_hash_name"]!.ToString());
}

and then following i do :

var ms = list.Select(x => x.ToString()).Where(s => !oybek.Contains(s)).ToList();

foreach(var item in ms)
{
    Console.WriteLine(item);
}

in the result i exlude only Loyalty Badge, but other badges and medals not deleted from my list. Can you help me exclude badges medals and etc from final result based on only keywords which stored in hashset oybek?

CodePudding user response:

Loyalty Badge is the only one excluded because it's the only exact match.

Your approach does not work because you are only checking if strings in your list match any of the strings oybek exactly.

Instead you'd need to do something like this

bool Valid(string item)
{
    foreach(var invalidWord in oybek)
    {
       if(item.Contains(invalidWord))
           return false;
    }
    return true;
}

List<string> allItems = //get your list here
var validItems = allItems.Where(x => Valid(x));

This checks for each string in your list if any of the strings in oybek is contained in it.

A faster approach (computationally) is probably for oybek to only contain single words and to split the strings in list into their words and check against it.

  •  Tags:  
  • c#
  • Related