2
$\begingroup$

Given

inventory={
{"https://lex.uz/en/docs/3815506", {"plant protection", "land plot", "agricultural production"}},
{"https://lex.uz/en/docs/6906672", {"environmental protection", "land plot"}}
};

I want to link the set of tags, for example, {"plant protection", "land plot", "agricultural production"} to the associated URL link https://lex.uz/en/docs/3815506 and filter the inventory using the tags to retrieve the associated URL links?

Example: when I filter the inventory using the tag land plot, I should get both URL links in the inventory list because land plot is common among the URL links.

$\endgroup$

2 Answers 2

2
$\begingroup$

Edit 1: Added url hyperlinks

inventory={
  {"https://lex.uz/en/docs/3815506", {"plant protection", "land plot", "agricultural production"}},
  {"https://lex.uz/en/docs/6906672", {"environmental protection", "land plot"}}};

ds = Query[All, {"url" -> URL}]@
       Dataset[AssociationThread[{"url", "tags"} -> #] & /@ inventory]

lookup[words__] :=
 Query[
   Select[
    Or @@ 
      Map[word |-> StringContainsQ[StringRiffle[#tags, "|"], word], words] &]
   , {"url", "tags"}]@ds

words = {"prot", "agri"};
lookup[words] [All, "url"] // Normal

{URL["https://lex.uz/en/docs/3815506"], URL["https://lex.uz/en/docs/6906672"]}

words = {"envi"};
lookup[words] [All, "url"] // Normal

{URL["https://lex.uz/en/docs/6906672"]}

$\endgroup$
2
  • $\begingroup$ Outputs from your code, URL links, should be clickable so that I can just click on it and go to the actual document. URLs in ds should also be clickable. $\endgroup$ Commented Oct 13 at 12:06
  • 1
    $\begingroup$ @TugrulTemel Thank you for the accepted answer. Please take a look at the updated version. $\endgroup$
    – vindobona
    Commented Oct 13 at 13:37
3
$\begingroup$

You haven't provided much detail about the actual structure of the things you want to work with, but here's a stab at a solution. This creates an association that maps tags to urls. You can just query this association.

urlsByTag = Merge[Thread@*Apply[Rule]@*Reverse /@ inventory, Identity]
(* <|"plant protection" -> {"https://lex.uz/en/docs/3815506"}, 
     "land plot" -> {"https://lex.uz/en/docs/3815506", "https://lex.uz/en/docs/6906672"}, 
     "agricultural production" -> {"https://lex.uz/en/docs/3815506"}, 
     "environmental protection" -> {"https://lex.uz/en/docs/6906672"}|> *)

urlsByTag["plant protection"]
(* {"https://lex.uz/en/docs/3815506"} *)

urlsByTag["land plot"]
(* {"https://lex.uz/en/docs/3815506", "https://lex.uz/en/docs/6906672"} *)
$\endgroup$
1
  • $\begingroup$ Actually, I have a total of 40,000 URL links and each one has a few tags to be attached like you did. My ultimate goal is to use a selected number of tags at once to retrieve the associated set of URL links. From there on, I will go into each one of the URL links to assess using some criteria. Is there a way to use a single word tag like land to retrieve the links, tags of which have multiple words like land plot? In a way, I shorten the two-word tag to a single word. Often, I need to collect the links with a single word tag while the actual tags have two words. $\endgroup$ Commented Oct 12 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.