@@ -16,13 +16,18 @@ public TagSearch(String input){
1616 }
1717 public Map <String , Set <String >> buildDisctonary (String input ){
1818 Map <String , Set <String >> map = new HashMap <>();
19- String [] strings = input .split (" " );
19+ for (String s : input .split ("\n " )){
20+ String [] feature = s .split (":" );
21+ String _feature = feature [0 ].strip ();
22+ map .putIfAbsent (_feature ,new HashSet <>());
2023
21- for (String feature : strings ){
22- String [] featureValue = feature .split (":" );
23- map .put (featureValue [0 ],new HashSet <>());
24- map .get (featureValue [0 ]).addAll (List .of (featureValue [1 ].split ("," )));
24+ for (String t : feature [1 ].split (" " )){
25+ if (t .isBlank ())
26+ continue ;
27+ map .get (_feature ).add (t .strip ());
28+ }
2529 }
30+ this .dict = map ;
2631 return map ;
2732 }
2833
@@ -37,29 +42,56 @@ public Set<String> getFeatureContains(String tag){
3742 return features ;
3843 }
3944
40- public Set <String > getTwoCommon (String t1 ,String t2 ){
45+ public Set <String > getEitherCommon (String t1 ,String t2 ){
46+ Set <String > s1 = getFeatureContains (t1 );
47+ Set <String > s2 = getFeatureContains (t2 );
48+ Set <String > result = new HashSet <>();
49+ s1 .addAll (s2 );
50+ return s1 ;
51+ }
52+
53+ public Set <String > getContainsAll (String t1 ,String t2 ){
4154 Set <String > s1 = getFeatureContains (t1 );
4255 Set <String > s2 = getFeatureContains (t2 );
4356 Set <String > result = new HashSet <>();
4457 for (String s : s1 ){
4558 if (s2 .contains (s ))
4659 result .add (s );
4760 }
48-
4961 return result ;
5062 }
5163
64+ public Set <List <String >> getWhichHaveCommon () {
65+ Set <List <String >> pairs = new HashSet <>();
66+ for (String outer : dict .keySet ()) {
67+ for (String inner : dict .keySet ()) {
68+ if (outer .compareTo (inner ) < 0 ) {
69+ if (haveCommon (dict .get (outer ), dict .get (inner ))) {
70+ pairs .add (List .of (outer , inner ));
71+ }
72+ }
73+ }
74+ }
75+ return pairs ;
76+ }
77+
78+
79+ public boolean haveCommon (Set <String > s1 ,Set <String > s2 ){
80+ for (String s : s1 )
81+ if (s2 .contains (s ))
82+ return true ;
83+ return false ;
84+ }
85+
5286 public static void main (String [] args ){
53- TagSearch search = new TagSearch ("FeatureA:tag1,tag2 FeatureB:tag3,tag4 FeatureC:tag3,tag4 FeatureD:tag1,tag3 FeatureE:tag1" );
87+ String test = "feature1 : string categorical\n feature2 : numeric\n feature3 : numeric categorical\n feature4 : string splittable\n feature5 : string" ;
88+ TagSearch search = new TagSearch (test );
89+
90+
5491 System .out .println (
55- search .getTwoCommon ( "tag1" , "tag3" )
92+ search .getWhichHaveCommon ( )
5693 );
5794
58- search .tagSet .stream ().forEach (
59- s -> System .out .println (
60- search .getFeatureContains (s )
61- )
62- );
6395 }
6496}
6597
0 commit comments