如何对齐或比较两个字符串数组?

时间:2016-03-22 03:42:27

标签: c#

我有2个字符串数组用于保存文本句子的单词,我有一个字符串数组用于保存第一个数组中单词的标签。我想根据第一个数组标签获得第二个数组字中的单词标签。 两个数组可能没有相同的长度,我们有这个选择标签的规则:

//"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.

例如:

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"};

我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:1)

  

此代码适用于给定的输入,我无法预见案例   哪里可能会失败。你想出这样一个案子,让我知道。如果这不是你想要的,这段代码应该给你足够的想法让你开始。

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();
        }