0

my 2D array has sentences, their tokens and their tags. I want to remove all the other tags Except nouns and verbs. I want to remove the whole index which is not verb/noun. Right now my code is just placing a null on that index. but i want that index to be removed from the array. my code is:

HashSet<String> tagsToRemove = new HashSet<String>() { "'NN'", "'NNS'", "'VBD'", "'VBG'", "'VBZ'", "'VB'" };

for (int i = 0; i < array2D_of_tags.GetLength(0); ++i)
{
    if (!tagsToRemove.Contains(array2D_of_tags[i, 2]))
        array2D_of_tags[i, 2] = null;

    //Console.WriteLine(array2D_of_tags[i, 2]);
}

this is the data in the array2D_of_tags:

 A supplier supplies many parts. | 'A' | 'DT' | 
 A supplier supplies many parts. | 'supplier' | 'NN' | 
 A supplier supplies many parts. | 'supplies' | 'NNS' | 
 A supplier supplies many parts. | 'many' | 'JJ' | 
 A supplier supplies many parts. | 'parts' | 'NNS' | 
 A supplier supplies many parts. | '.' | '.' | 
spinodal
  • 4,007
  • 3
  • 29
  • 38
  • it will be helpful to add an example of data in `array2D_of_tags`. Please show the input data and the expected output. It will make question more clear. And please add this data to the question itself :) – Gilad Green Oct 11 '16 at 09:20
  • if you remove one item from a 2x2 array, what would be the size of the new array? – Slai Oct 11 '16 at 09:21
  • array looks like: A supplier supplies many parts. | 'A' | 'DT' | A supplier supplies many parts. | 'supplier' | 'NN' | A supplier supplies many parts. | 'supplies' | 'NNS' | A supplier supplies many parts. | 'many' | 'JJ' | A supplier supplies many parts. | 'parts' | 'NNS' | A supplier supplies many parts. | '.' | '.' | – Rida Akhtar Oct 11 '16 at 09:23
  • According to [How to delete a row from a 2d array in c#?](http://stackoverflow.com/a/7992832/6741868) and [Delete row of 2D string array in C#](http://stackoverflow.com/a/5376395/6741868), the only way is to create a new one to replace the old one. – Keyur PATEL Oct 11 '16 at 09:24
  • If you only need to dynamically remove elements from the _inner_ array then perhaps consider a List(T)[] (if you do need to modify the outer array then maybe use a List(List(T)) ) – Scott Perham Oct 11 '16 at 09:25
  • @RidaAkhtar - can you please edit your question to add it. And please add the actual definition of your data structure – Gilad Green Oct 11 '16 at 09:25
  • size of the new array would be one less than the original one. – Rida Akhtar Oct 11 '16 at 09:25
  • @GiladGreen done editing – Rida Akhtar Oct 11 '16 at 09:28

1 Answers1

1

It's not possible to delete the index cause array are fixed in size. If you want to accomplish this by keeping your data structure, you must create a new array with the desired data inside. You can write something like this:

string[,] array2D_of_tags = new string[,]
    {
        {"A", "DT"}, {"supplier", "NN"}, {"supplies", "NNS"}, {"many", "JJ" }
    };

    HashSet <string> tagsToRemove = new HashSet<string> { "'NN'", "'NNS'", "'VBD'" };
    int rowsToKeep = array2D_of_tags.GetLength(0) - tagsToRemove.Count;

    string[,] newArr2d = new string[rowsToKeep, array2D_of_tags.GetLength(1)];
    int currentRow = 0;
    for (int i = 0; i < array2D_of_tags.GetLength(0); i++)
    {
        if (!tagsToRemove.Contains(array2D_of_tags[i, 2])) //I didn't understand this, i take it from your code
        {
            for (int j = 0; j < array2D_of_tags.GetLength(1); j++)
            {
                newArr2d[currentRow, j] = array2D_of_tags[i, j];
            }
            currentRow++;
        }
    }
Alex
  • 599
  • 1
  • 4
  • 16
  • However, I really recommend you to change your data structure. Take a look at Dictionaries. They have this methods already implemented. Take a look [in this website:](https://www.dotnetperls.com/dictionary) – Alex Oct 11 '16 at 10:27