前几天我试着问这个,诚然一开始没有很好地表达这个问题或邮政编码,答案被关闭了。所以我在这里再次尝试,因为老实说,这让我很快发疯。:)
我正在尝试实现这个Address Parser,它最初是一个基于控制台的 c# 程序。我已成功地将其转换为一个独立的 WPF 程序,该程序仅包含一个TextBox
用于输入、一个Button
用于激活解析和一个TextBlock
用于显示结果。在写这篇文章时,我确实将输出截断为我在主程序中需要的内容,但它仍然可以正常工作。我已经在下面包含了整个代码。
我的下一步是将其移植到我的主程序中,我通过使用复制/粘贴来完成。但是,在运行此程序时,程序会在按下按钮后挂起。最终 VS 给出了一个错误,即进程时间过长而没有输出消息,并且 TaskManager 中的内存使用量逐渐从 ~70k 增加到 3,000,000。针对这种情况,我将Parsing
方法分配给了一个后台worker,希望能减轻主进程的工作量。这确实解决了程序冻结的问题,但后台线程只是做了同样的事情,提高了 RAM 使用率并且什么也不返回。
所以现在我陷入了僵局。我知道问题出在var result = parser.ParseAddress(input);
语句的某个地方,因为当对每一行代码使用断点时,这是最后一个触发的。但基本上我不明白为什么这会在一个 WPF 程序而不是另一个程序中导致问题。
如果有必要,我会非常乐意将主程序的完整源代码发布到某个地方,但我无法想象在这里发布大约 20 个不同的类文件和项目的代码会是个好主意。:)
独立的 WPF 应用程序
namespace AddressParseWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void Execute()
{
AddressParser.AddressParser parser = new AddressParser.AddressParser();
var input = inputTextBox.Text;
var result = parser.ParseAddress(input);
if (result == null)
{
outputTextBlock.Text = "ERROR. Input could not be parsed.";
}
else
{
outputTextBlock.Text = (result.StreetLine + ", " + result.City + ", " + result.State + " " + result.Zip);
}
}
private void actionButton_Click(object sender, RoutedEventArgs e)
{
Execute();
}
}
}
将 Parser 移植到的主程序
public void ExecuteAddressParse()
{
AddressParser.AddressParser parser = new AddressParser.AddressParser();
var input = inputTextBox.Text;
var result = parser.ParseAddress(input);
if (result == null)
{
outputTextBlock.Text = "ERROR. Input could not be parsed.";
}
else
{
outputTextBlock.Text = (result.StreetLine + ", " + result.City + ", " + result.State + " " + result.Zip);
}
}
private void actionButton_Click(object sender, RoutedEventArgs e)
{
ExecuteAddressParse();
}
ParseAddress 方法
public AddressParseResult ParseAddress(string input)
{
if (!string.IsNullOrWhiteSpace(input))
{
var match = addressRegex.Match(input.ToUpperInvariant());
if (match.Success)
{
var extracted = GetApplicableFields(match);
return new AddressParseResult(Normalize(extracted));
}
}
return null;
}
正则表达式匹配方法
private static void InitializeRegex()
{
var suffixPattern = new Regex(
string.Join(
"|",
new [] {
string.Join("|", suffixes.Keys),
string.Join("|", suffixes.Values.Distinct())
}),
RegexOptions.Compiled);
var statePattern =
@"\b(?:" +
string.Join(
"|",
new [] {
string.Join("|", states.Keys.Select(x => Regex.Escape(x))),
string.Join("|", states.Values)
}) +
@")\b";
var directionalPattern =
string.Join(
"|",
new [] {
string.Join("|", directionals.Keys),
string.Join("|", directionals.Values),
string.Join("|", directionals.Values.Select(x => Regex.Replace(x, @"(\w)", @"$1\.")))
});
var zipPattern = @"\d{5}(?:-?\d{4})?";
var numberPattern =
@"(
((?<NUMBER>\d+)(?<SECONDARYNUMBER>(-[0-9])|(\-?[A-Z]))(?=\b)) # Unit-attached
|(?<NUMBER>\d+[\-\ ]?\d+\/\d+) # Fractional
|(?<NUMBER>\d+-?\d*) # Normal Number
|(?<NUMBER>[NSWE]\ ?\d+\ ?[NSWE]\ ?\d+) # Wisconsin/Illinois
)";
var streetPattern =
string.Format(
CultureInfo.InvariantCulture,
@"
(?:
# special case for addresses like 100 South Street
(?:(?<STREET>{0})\W+
(?<SUFFIX>{1})\b)
|
(?:(?<PREDIRECTIONAL>{0})\W+)?
(?:
(?<STREET>[^,]*\d)
(?:[^\w,]*(?<POSTDIRECTIONAL>{0})\b)
|
(?<STREET>[^,]+)
(?:[^\w,]+(?<SUFFIX>{1})\b)
(?:[^\w,]+(?<POSTDIRECTIONAL>{0})\b)?
|
(?<STREET>[^,]+?)
(?:[^\w,]+(?<SUFFIX>{1})\b)?
(?:[^\w,]+(?<POSTDIRECTIONAL>{0})\b)?
)
)
",
directionalPattern,
suffixPattern);
var rangedSecondaryUnitPattern =
@"(?<SECONDARYUNIT>" +
string.Join("|", rangedSecondaryUnits.Keys) +
@")(?![a-z])";
var rangelessSecondaryUnitPattern =
@"(?<SECONDARYUNIT>" +
string.Join(
"|",
string.Join("|", rangelessSecondaryUnits.Keys)) +
@")\b";
var allSecondaryUnitPattern = string.Format(
CultureInfo.InvariantCulture,
@"
(
(:?
(?: (?:{0} \W*)
| (?<SECONDARYUNIT>\#)\W*
)
(?<SECONDARYNUMBER>[\w-]+)
)
|{1}
),?
",
rangedSecondaryUnitPattern,
rangelessSecondaryUnitPattern);
var cityAndStatePattern = string.Format(
CultureInfo.InvariantCulture,
@"
(?:
(?<CITY>[^\d,]+?)\W+
(?<STATE>{0})
)
",
statePattern);
var placePattern = string.Format(
CultureInfo.InvariantCulture,
@"
(?:{0}\W*)?
(?:(?<ZIP>{1}))?
",
cityAndStatePattern,
zipPattern);
var addressPattern = string.Format(
CultureInfo.InvariantCulture,
@"
^
# Special case for APO/FPO/DPO addresses
(
[^\w\#]*
(?<STREETLINE>.+?)
(?<CITY>[AFD]PO)\W+
(?<STATE>A[AEP])\W+
(?<ZIP>{4})
\W*
)
|
# Special case for PO boxes
(
\W*
(?<STREETLINE>(P[\.\ ]?O[\.\ ]?\ )?BOX\ [0-9]+)\W+
{3}
\W*
)
|
(
[^\w\#]* # skip non-word chars except # (eg unit)
( {0} )\W*
{1}\W+
(?:{2}\W+)?
{3}
\W* # require on non-word chars at end
)
$ # right up to end of string
",
numberPattern,
streetPattern,
allSecondaryUnitPattern,
placePattern,
zipPattern);
addressRegex = new Regex(
addressPattern,
RegexOptions.Compiled |
RegexOptions.Singleline |
RegexOptions.IgnorePatternWhitespace);
}