所以我长期以来一直在尝试这么多东西,但似乎无法实现这一点:
此代码的基本逻辑是:
这是我现在这样做的方法:
public string TextSpeak(string text)
{
var reader = new StreamReader(File.OpenRead(@"H:\SECourseowork\textwords.csv"));
Dictionary<string, string> dict = new Dictionary<string, string>();
Regex allcaps = new Regex("[A-Z]{1,7}");
string uneditedtext = text;
MatchCollection matched = allcaps.Matches(uneditedtext);
bool found = false;
while (found == false && !reader.EndOfStream)
{
var lines = reader.ReadLine();
var values = lines.Split(',');
var upperCaseWords = uneditedtext.Split(' ').Where(w => w == w.ToUpper());
// string capsrgx = @"([A-Z]{1,7})";
// Regex capspresent = new Regex(capsrgx);
// Regex allcaps = new Regex("[A-Z]{1,7}");
// MatchCollection matched = allcaps.Matches(uneditedtext);
// Match m = Regex.Match(text, capsrgx);
// Match test = allcaps.Match(uneditedtext);
dict.Add(values[0], values[1]);
foreach (Match matches in matched)
{
testlist.Add(matches.Value);
// Console.WriteLine(matches.Value);
//foreach (string word in uneditedtext.Split(' '))
//For each key in dictionary
foreach (var k in dict)
{
//If the unedited text contains a key from the dictionary
if (matches.Value.Contains(k.Key))
{
//replace that string with the key and its corresponding value
text = uneditedtext.Replace(k.Key, k.Key.ToString() + "<" + k.Value.ToString() + ">");
//When match and replcae found, return true
//m = m.NextMatch();
found = true;
}
else
{
found = false;
}
}
}
}
Console.WriteLine(text);
return text;
}
我得到了输出:
AAP AAR&lt;无论如何&gt;
即使我输入了多个键/值对,也会发生这种情况。但是我希望这两个密钥都被它们的扩展版本所取代。
非常感谢任何帮助,谢谢!