1

私はタグのような文字列一致関数を実行しています。関数チェックには、少なくともタグごとに順序を維持しながら、可能な単語が文字列に含まれています。可能性のリストを事前に作成し、文字列に必要な各組み合わせが含まれているかどうかを確認するだけでよいことがわかりました

多分コードはそれをより明確にするでしょう。

List<List<string[]>> tags;

List<string[]> innerList;

List<List<string>> combinationsList;

public void Generate(string pattern)
{
    // i will add whitespace removal later so it can be ", " instead of only ","

    foreach (string tag in pattern.Split(','))
    {
        innerList = new List<string[]>();

        foreach (var varyword in tag.Split(' '))
        {
            innerList.Add(varyword.Split('|'));
        }
    }

    // atm i lack code to generate combinations in form of List<List<string>> 
    // and drop them into 'combinationsList'
}

// the check function will look something like isMatch = :
public bool IsMatch(string textToTest)
{
    return combinationsList.All(tc => tc.Any(c => textToTest.Contains(c)));
}

たとえば、パターン:

「古い|若いジョン|ボブ、持っている|犬|猫を飼っている」

  • タグ:
    • リスト_1:
      • {老若}
      • {ジョン、ボブ}
    • リスト_2
      • {持っている、持っている}
      • {犬猫}

したがって、combinationsList は次のようになります。

  • 組み合わせリスト:
    • リスト_1
      • 「古いジョン」
      • 「古いボブ」
      • 「若いジョン」
      • 「ヤングボブ」
    • リスト_2
      • 「犬を飼う」
      • 「猫を飼う」
      • 「飼っている犬」
      • 「猫を飼う」

したがって、結果は次のようになります。

  • old bob have cat = true、List_1:"old bob" および List_2:"have cat" を含む
  • young john have car = false, List_1:"young john" が含まれていますが、List_2 の組み合わせは含まれていません

コレクションを反復してそれらの組み合わせを取得する方法と、反復ごとに組み合わせを取得する方法がわかりません。また、古いジョンもジョン・オールドとして生成されないように、順序を台無しにすることはできません。

パターン内の「異体字」には、「犬 | 猫 | マウス」のように 2 つ以上の異体字が含まれる場合があることに注意してください。

4

2 に答える 2

2

このコードは役立つかもしれません

string pattern = "old|young john|bob have|posses dog|cat";
var lists = pattern.Split(' ').Select(p => p.Split('|'));

foreach (var line in CartesianProduct(lists))
{
    Console.WriteLine(String.Join(" ",line));
}


//http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    // base case:
    IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
    foreach (var sequence in sequences)
    {
        var s = sequence; // don't close over the loop variable
        // recursive case: use SelectMany to build the new product out of the old one
        result =
            from seq in result
            from item in s
            select seq.Concat(new[] { item });
    }
    return result;
}
于 2013-04-26T14:23:59.260 に答える
0

別のスレッドで答えを見つけました。

https://stackoverflow.com/a/11110641/1156272

Adamによって投稿されたコードは問題なく動作し、私が必要としていたことを正確に実行します

        foreach (var tag in pattern.Split(','))
        {
            string tg = tag;
            while (tg.StartsWith(" ")) tg = tg.Remove(0,1);
            innerList = new List<List<string>>();

            foreach (var varyword in tg.Split(' '))
            {
                innerList.Add(varyword.Split('|').ToList<string>());
            }

            //Adam's code

            List<String> combinations = new List<String>();
            int n = innerList.Count;
            int[] counter = new int[n];
            int[] max = new int[n];
            int combinationsCount = 1;
            for (int i = 0; i < n; i++)
            {
                max[i] = innerList[i].Count;
                combinationsCount *= max[i];
            }
            int nMinus1 = n - 1;
            for (int j = combinationsCount; j > 0; j--)
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < n; i++)
                {
                    builder.Append(innerList[i][counter[i]]);
                    if (i < n - 1) builder.Append(" "); //my addition to insert whitespace between words
                }
                combinations.Add(builder.ToString());

                counter[nMinus1]++;
                for (int i = nMinus1; i >= 0; i--)
                {
                    // overflow check
                    if (counter[i] == max[i])
                    {
                        if (i > 0)
                        {
                            // carry to the left
                            counter[i] = 0;
                            counter[i - 1]++;
                        }
                    }
                }
            }

            //end

            if(combinations.Count > 0)
                combinationsList.Add(combinations);
        }
    }

    public bool IsMatch(string textToCheck)
    {
        if (combinationsList.Count == 0) return true;

        string t = _caseSensitive ? textToCheck : textToCheck.ToLower();

        return combinationsList.All(tg => tg.Any(c => t.Contains(c)));
    }

魔法のように見えますが、機能します。みんな、ありがとう

于 2013-04-26T21:53:18.880 に答える