Getting a count of unique strings from a List<string[]> into a dictionary

ccsv

I want to input a List<string[]> and

The output is a dictionary where the keys are unique strings used for an index and the values is an array of floats with each position in the array representing the count of the key for a string[] in the List<string[]>

So far here is what I attempted

static class CT
{
    //Counts all terms in array
    public static Dictionary<string, float[]> Termfreq(List<string[]> text)
    {
        List<string> unique = new List<string>();

        foreach (string[] s in text)
        {
            List<string> groups = s.Distinct().ToList();
            unique.AddRange(groups);
        }

        string[] index = unique.Distinct().ToArray();

        Dictionary<string, float[]> countset = new Dictionary<string, float[]>();


         return countset;
    }

}



 static void Main()
    {
        /* local variable definition */


        List<string[]> doc = new List<string[]>();
        string[] a = { "That", "is", "a", "cat" };
        string[] b = { "That", "bat", "flew","over","the", "cat" };
        doc.Add(a);
        doc.Add(b);

       // Console.WriteLine(doc);


        Dictionary<string, float[]> ret = CT.Termfreq(doc);

        foreach (KeyValuePair<string, float[]> kvp in ret)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);

        }


        Console.ReadLine();

    }

I got stuck on the dictionary part. What is the most effective way to implement this?

Jon Skeet

It sounds like you could use something like:

var dictionary = doc
    .SelectMany(array => array)
    .Distinct()
    .ToDictionary(word => word,
                  word => doc.Select(array => array.Count(x => x == word))
                             .ToArray());

In other words, first find the distinct set of words, then for each word, create a mapping.

To create a mapping, look at each array in the original document, and find the count of the occurrences of the word in that array. (So each array maps to an int.) Use LINQ to perform that mapping over the whole document, with ToArray creating an int[] for a particular word... and that's the value for that word's dictionary entry.

Note that this creates a Dictionary<string, int[]> rather than a Dictionary<string, float[]> - it seems more sensible to me, but you could always cast the result of Count to float if you really wanted to.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count equal strings in a list of string and make them unique

From Dev

Difficulty getting the item count for the combinations of list of items from python dictionary

From Dev

Unique values with frequency count from List<List<String>>

From Dev

List of strings in a dictionary getting erroneously assigned a value

From Dev

Replace strings in list from dictionary

From Dev

How to string count unique values in data strings

From Dev

Getting total count from the string

From Dev

Clearing a List<string> removes all values from Dictionary List of Strings, C#

From Dev

Applying a dictionary of string replacements to a list of strings

From Dev

Applying a dictionary of string replacements to a list of strings

From Dev

Getting a list of list from a string

From Dev

List<String> get count of all elements ending with one of strings from another list

From Dev

Regex: creating a dictionary from a list of strings

From Dev

From List<string> to Dictionary<string,string>

From Dev

Get a list of unique strings from duplicate entries

From Dev

Getting a list of strings after a start string

From Dev

Create dictionary from splitted strings from list of strings

From Dev

Return count of unique values from list of dictionaries

From Dev

getting one value from a list of values in dictionary

From Dev

Get unique string from a vector of similar strings

From Dev

Extract unique strings from a factor string variable

From Dev

Using Python to count unique list elements of two strings separated by a space

From Java

Unique count of words from text string

From Dev

Getting count of unique values in pandas Dataframe when there is a list object in a column

From Dev

Create dictionary with count of each unique item from an excel column

From Dev

Getting the unique names of a specific value from several keys of a dictionary in python

From Dev

Getting the unique names of a specific value from several keys of a dictionary in python

From Dev

Getting a random String from a List

From Dev

Getting a random String from a List

Related Related

  1. 1

    Count equal strings in a list of string and make them unique

  2. 2

    Difficulty getting the item count for the combinations of list of items from python dictionary

  3. 3

    Unique values with frequency count from List<List<String>>

  4. 4

    List of strings in a dictionary getting erroneously assigned a value

  5. 5

    Replace strings in list from dictionary

  6. 6

    How to string count unique values in data strings

  7. 7

    Getting total count from the string

  8. 8

    Clearing a List<string> removes all values from Dictionary List of Strings, C#

  9. 9

    Applying a dictionary of string replacements to a list of strings

  10. 10

    Applying a dictionary of string replacements to a list of strings

  11. 11

    Getting a list of list from a string

  12. 12

    List<String> get count of all elements ending with one of strings from another list

  13. 13

    Regex: creating a dictionary from a list of strings

  14. 14

    From List<string> to Dictionary<string,string>

  15. 15

    Get a list of unique strings from duplicate entries

  16. 16

    Getting a list of strings after a start string

  17. 17

    Create dictionary from splitted strings from list of strings

  18. 18

    Return count of unique values from list of dictionaries

  19. 19

    getting one value from a list of values in dictionary

  20. 20

    Get unique string from a vector of similar strings

  21. 21

    Extract unique strings from a factor string variable

  22. 22

    Using Python to count unique list elements of two strings separated by a space

  23. 23

    Unique count of words from text string

  24. 24

    Getting count of unique values in pandas Dataframe when there is a list object in a column

  25. 25

    Create dictionary with count of each unique item from an excel column

  26. 26

    Getting the unique names of a specific value from several keys of a dictionary in python

  27. 27

    Getting the unique names of a specific value from several keys of a dictionary in python

  28. 28

    Getting a random String from a List

  29. 29

    Getting a random String from a List

HotTag

Archive