文字列と辞書オブジェクトが渡されたときに一致の配列を取得するために、.Netフレームワークを介して(または誰かが同様の何かを書いた)方法はありますか?
最初にいくつかの背景
する必要がある
たとえば、スポーツチームのcsvファイルを辞書オブジェクトにロードします...
Team, Variants
Manchester United, Manchester United
Manchester United, manutd
Manchester United, man utd
Manchester United, manchester utd
Manchester United, mufc
Aston Villa, Aston Villa
Aston Villa, avfc
Newcastle United, Newcastle United
Newcastle United, toon army
次に、文字列にその辞書のフレーズが含まれているかどうかを確認します。
文字列の例...
"I wonder if man utd, aston villa andthe toon army will exist in this string"
ここで、一致する文字列のn配列を返したいのですが、出力例は次のようになります。
["Manchester United","Aston Villa", "Newcastle United"]
私は現在、文字列内の単語を分割するために正規表現を使用しています。次に、文字列内の各単語をループして、辞書に対してテストします(ここでの注意は、コードは機能しますが、フレーズではなく単一の単語のみであり、正規表現によるものです)
public static List<string> CheckStringWithDictionary(string input, Dictionary<string, string> dic, int minimumLength)
{
List<string>lst = new List<string>();
string myValue = "";
foreach (Match match in RegexSplitStringToArray(input, minimumLength))
{
if (dic.TryGetValue(match.Value, out myValue))
lst.Add(myValue);
}
return lst;
}
public static MatchCollection RegexSplitStringToArray(string input, int minLength)
{
Regex csvSplit = new Regex("(\\w{3,})", RegexOptions.Compiled);
return csvSplit.Matches(input);
}
辞書ではなく文字列をループする理由は、辞書に10,000以上のアイテムが含まれるため、ループするという点で非常に非効率的であるためです。
これまでのあなたのpatieceに感謝します、そして今質問に...
文字列と辞書オブジェクトが渡されたときに一致の配列を取得するために、.Netフレームワークを介して(または誰かが同様の何かを書いた)方法はありますか?
皆さんありがとう