简体   繁体   中英

How to dynamically populate a 3D array through loops in C#?

I want to populate a 3D array dynamically. I am doing NLP and for that I want my sentences and tokens and tags on one point. So I want three of them to be in one 3D array so that I can access them at once and I want to populate it dynamically with this stuff. I have sentences in 1D array and tokens and tags in a 2D array. How could I add them in 3D array? here is what i have done so far:

//getting result
result = reader.ReadToEnd();
richTextBox2.Text = result;
var lines = richTextBox2.Lines;
sentences = lines[0].Split('.'); //getting sentences

String[,] tagged_data = new String[1000, 2];
String input = lines[2];
String first = input.Substring(2, input.Length - 2);
Console.WriteLine(first);
String second = first.Substring(0, first.Length - 2);
Console.WriteLine(second);

Regex regex = new Regex(@"\'([^\']*)\'");
MatchCollection matches = regex.Matches(second);

int count = 0;

for (int j = 0; j < tagged_data.Length; j++)
{
    for (int k = 0; k < 2; k++)
    {
        if (count < matches.Count)
        {
            tagged_data[j, k] = matches[count].Value;
            count++;
            Console.WriteLine(tagged_data[j, k]);
        }
        else
            break;
    }
}

Sentences, tokens and tags do not sound like things to be put in a 3d array for me. You probably don't need a cuboid, but a jagged array, ie an array of arrays of arrays.

For instance you could have an array of sentences, where each sentence could have a varying number of tokens and each token a varying number of tags.

string[] sentences = GetSentences(text);
string[][][] result = new string[sentences.Length][][];
for (int i = 0; i < sentences.Length; i++) {
    string[][] tagsAndTokens = GetTagsAnTokens(sentence);
    result[i] = tagsAndTokens;
}

The problem with this approach, however is, that this result array is only storing one string per position; probably the tags in your case. An n-dimensional array or jagged array only ever stores the things of the deepest level of nesting. Where do you store the other things?

Wouldn't a class structure handle it better?

class Sentence
{
    public string Text { get; set; }
    public Token[] Tokens { get; set; }
}

class Token
{
    public string Text { get; set; }
    public string[] Tags { get; set; }
}

Then you would build an array of sentences

Sentence[] sentences = ...;

Often you don't know the maximum size of the collections in advance. In such cases List<T> is better than T[] . If you need an array at some point, you can easily convert it to an array later

List<string> l = new List<string>();
l.Add("...");
l.Add("...");
string[] a = l.ToString();

According to your explanations you just want to store the index of the sentences together with the tagged data and keep the sentences in a separate array. If the new a 3D array (or maybe a combination of arrays and lists?) has the same length as the sentences array, there is no point in storing the index, as each entry is at the same index as the corresponding sentence.

But It is difficult to know what you have in mind, even by seeing your code. Can you show us a sample of the input data?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM