Results 1 to 10 of 10
  1. #1
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Mapping cluster arrays into original data source

    Hello,
    I'm writing a program that clusters strings based on a defined string edit distance. As an output, my program generates a list that consist of cluster arrays. What is in the cluster arrays is the indexes of the original strings that was stored in an array. Now I want to go back and map the indexes in the arrays to the original values that are stored in the input array. I know I have to use a map for that purpose, but not sure where to start. Any advice will be appreciated.

    This is how the input array looks like: [This, is, an, example, text, to, test, the, QT, clustering, algorithm.]
    Those are the array clusters that are stored in the output list:
    cluster 0:[8, 1, 2, 5, 7]
    cluster 1:[0, 4, 6]
    cluster 2:[9, 10]
    cluster 3:[3]

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Mapping cluster arrays into original data source

    I'm still uncertain as to what you want to end up with. You have the initial text array and you have four cluster arrays, which in total point to all the original strings. Do you want something like:

    Cluster0 -> "QT", "is", "an", "to","the"
    Cluster1 -> ...
    Cluster2 -> ...

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping cluster arrays into original data source

    Quote Originally Posted by jim829 View Post
    I'm still uncertain as to what you want to end up with. You have the initial text array and you have four cluster arrays, which in total point to all the original strings. Do you want something like:

    Cluster0 -> "QT", "is", "an", "to","the"
    Cluster1 -> ...
    Cluster2 -> ...

    Regards,
    Jim

    Hey Jim!
    Thanks for your promoted response!
    Exactly, this is what i'm trying to do. Map the indexes of the arrays back to the actual values of in the input array I used to obtain the values.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Mapping cluster arrays into original data source

    Then you can use a Map<String, List<String>>.

    The String will be the cluster name and the list for each cluster can contain the individual words for that cluster. Check out how to use them at
    Lesson: Introduction to Collections (The Java™ Tutorials > Collections)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping cluster arrays into original data source

    Jim,

    I tried this piece of code, can u let me know what is wrong in here!



    for(int i = 0; i < clusters.size(); i++) {
    map.put(Arrays.toString(words),clusters<Arrays.toS tring(clusters.get(i)>));
    System.out.println("cluster " + i + ":" + Arrays.toString(clusters.get(i)));
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Mapping cluster arrays into original data source

    Your syntax is messed up. ArraysLists regardless of type are indexed by numbers. Map entries are retrieved based on keys which may be a variety of types.

    Java Code:
    List<String> list = new ArrayList<>();
    list.add("foo");
    System.out.println(list.get(0));
    Java Code:
    Map<String,String> map = new HashMap<>():
    map.put("foo", "bar");
    System.out.println(map.get("foo"));
    Here's a demo of how to add to a map of lists:

    Java Code:
    public class Snippet {
       public static void main(String[] args) {
    
          /**
           * Adding to map
           */
          Map<String, List<String>> clusters = new HashMap<>();
          String[] c1 = { "A", "B", "C"
          };
          clusters.put("Cluster1", new ArrayList<>(Arrays.asList(c1)));
    
          /**
           * Now retrieve them
           */
    
          for (String cluster : clusters.keySet()) {
             System.out.println(cluster + " = " + clusters.get(cluster));
          }
       }
    
    }
    Now all you have to do is build the array. And you don't need to do it like I did. You can also add the strings to the ArrayList on the fly as you discover them.

    Regards,
    JIm
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping cluster arrays into original data source

    Thanx again Jim!
    I could figure out how messed my syntax was, but the example u gave me will let each individual cluster equal to the whole elements of the array. Where I want only the strings with the indexes included each cluster to point to their actual values:
    Example: cluster containing [3] = [example]
    Which is carries index num 3 in the array!

  8. #8
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping cluster arrays into original data source

    Hey Jim!
    I think I have to explain my point a little more! The clusters that I have are obtained using some library that implements the QT clustering algorithm. The values that I have in the clusters are not Strings. They are integers that reflect the indexes of the strings that are stored in the input array. Now, I want to get the String values of the integer indexes that are stored in clusters arrays. By following your example, I get the whole input array printed as a value of every array cluster. This is not exactly what I want to get. Looking forward you comment!

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Mapping cluster arrays into original data source

    My example can be altered. You don't have to print out the arrays as a single unit. You can also simply read in the array of indices and then use them to access the appropriate element in the original String array.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    rima is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Default Re: Mapping cluster arrays into original data source

    Quote Originally Posted by jim829 View Post
    My example can be altered. You don't have to print out the arrays as a single unit. You can also simply read in the array of indices and then use them to access the appropriate element in the original String array.

    Regards,
    Jim
    Thanx so much! I'll try to figure out how to do that!

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2013, 10:54 AM
  2. Replies: 0
    Last Post: 02-13-2013, 07:48 AM
  3. to log the very fast data source
    By jerry_nf in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-26-2011, 05:21 AM
  4. ODBC Data Source Administrator
    By dewitrydan in forum New To Java
    Replies: 2
    Last Post: 11-08-2010, 11:55 AM
  5. about Arrays.asList source
    By geniusxiayi in forum Advanced Java
    Replies: 2
    Last Post: 04-13-2009, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •