Ros
Ros

Reputation: 37

How to align or compare two string array?

I have 2 string array for saving words of a text sentence and I have one string array for saving tags of words in first array. I want to get the tags of words in second array words according to first array tags. both array may not have equal length and we have this rules for selecting tags:

//"a" and "b" suppose are words and "t" is a tag.
if tag1("a")="t" then tag2("a")=tag1("a")="t"
if tag1("a b")=="t" then tag2("a")=tag2("b")="t"
if tag1("a")=="t1" and tag1("b")=="t2" then tag2("a b")=tag1("a")="t1"
for other cases,which tag is selected is not important.
there is a 1-to-1 correspondence between elements in str1 and tag1.
In fact, if two element in str1 and str2 are same then the tags of them are same.

for example:

string[] str1={"this","is","a simple","text","for","example","."};
string[] str2={"this","is","a","simple text","for","example","."};
string[] tag1={"tt","ii","aa","ttt","ff","ee","pp"};

string[] tag2=new string[str2.length]

///getting tag2 array is the desired result:
string[] tag2={"tt","ii","aa","aa","ff","ee","pp"};

How to I can do it?

Thanks.

Upvotes: 1

Views: 93

Answers (1)

Renuka Deshmukh
Renuka Deshmukh

Reputation: 998

This code works for the given input and I am unable to foresee a case where it might fail. It you come up with such a case, let me know. If this is not what you are looking for, this code should give you enough of an idea to get you started.

static void Main(string[] args)
        {
            //string[] str1 = { "this", "is", "a simple", "text", "for", "example", "." };
            //string[] str2 = { "this", "is", "a", "simple text", "for", "example", "." };
            //string[] tag1 = { "tt", "ii", "aa", "ttt", "ff", "ee", "pp" };
            //string[] tag2 = { "tt", "ii", "aa", "aa", "ff", "ee", "pp" };

            //string[] str1 = { "this", "is", "a simple", "text", "for", "this", "example", "." };
            //string[] str2 = { "this", "is", "a", "simple text", "for", "this example", "." };
            //string[] tag1 = { "tt", "ii", "aa", "ttt", "ff","tttt", "ee", "pp" };
            //string[] tag2 = { "tt", "ii", "aa", "aa", "ff", "tttt", "pp" };

            string[] str1 = { "this", "is", "a simple", "text", "for", "this", "example", "." };
            string[] str2 = { "this", "is", "a", "simple text", "for", "this example", "." };
            string[] tag1 = { "tt", "ii", "aa", "ttt", "ff", "tt", "ee", "pp" };
            string[] tag2 = { "tt", "ii", "aa", "aa", "ff", "tt", "pp" };

            Dictionary<string, string> map = new Dictionary<string, string>();
            Dictionary<string, int> wordIndexDict = new Dictionary<string, int>();
            for (int i = 0; i < str1.Length; i++)
            {
                string word = str1[i];
                string tag = tag1[i];
                string[] words = word.Split();
                foreach (var item in words)
                {
                    int cnt = 1;
                    if (wordIndexDict.ContainsKey(item))
                    {
                        cnt = wordIndexDict[item];
                        wordIndexDict[item] = cnt + 1;
                        cnt = cnt + 1;
                    }
                    else {
                        wordIndexDict.Add(item, cnt);
                    }
                    map.Add(item + "_" + cnt, tag);

                }
            }

            wordIndexDict = new Dictionary<string, int>();

            List<string> output = new List<string>();
            for (int i = 0; i < str2.Length; i++)
            {
                string word = str2[i];
                string[] words = word.Split();

                int cnt = 1;
                if (wordIndexDict.ContainsKey(words[0]))
                {
                    cnt = wordIndexDict[words[0]];
                    wordIndexDict[words[0]] = cnt + 1;
                    cnt = cnt + 1;
                }
                else {
                    wordIndexDict.Add(words[0], cnt);
                }
                string key = words[0] + "_" + cnt;
                if (map.ContainsKey(key))
                {
                    string tag = map[key];
                        output.Add(tag);
                }
            }

            foreach (var item in output)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }

Upvotes: 1

Related Questions