Skip to content

Commit

Permalink
Merge pull request #654 from weather-gov/eg-589-alert-description
Browse files Browse the repository at this point in the history
Alert description parsing
  • Loading branch information
eric-gade authored Jan 18, 2024
2 parents 228ddbf + 93c867b commit 4c435b0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Drupal\weather_data\Service\Test;

use Drupal\weather_data\Service\WeatherAlertTrait;

final class TryParsingDescriptionTextTest extends Base
{
protected function setUp(): void
{
parent::setUp();
}

/**
* Test the tryParsingDescriptionText static method
*/
public function testBasic(): void
{
$rawDescription =
"* WHAT...Snow expected. Total snow accumulations of 5 to 10\ninches.\n\n";
$rawDescription .= "* WHERE...Eastern San Juan Mountains Above 10000 Feet.\n\n";
$rawDescription .= "* WHEN...From 11 PM this evening to 11 PM MST Thursday.\n\n";
$rawDescription .= "* IMPACTS...Travel could be very difficult. The hazardous
conditions may impact travel over Wolf Creek Pass.";
$parsedDescription = WeatherAlertTrait::tryParsingDescriptionText(
$rawDescription,
);

$expected = [
"what" =>
"Snow expected. Total snow accumulations of 5 to 10\ninches.",
"where" => "Eastern San Juan Mountains Above 10000 Feet.",
"when" => "From 11 PM this evening to 11 PM MST Thursday.",
"impacts" =>
"Travel could be very difficult. The hazardous\nconditions may impact travel over Wolf Creek Pass.",
];
$this->assertEquals($expected, $parsedDescription);
}

public function testNotMatching(): void
{
$rawDescription =
"* This is an alert with\n\nmultiple newlines, but it has nothing ";
$rawDescription .= "to do with the what WHERE WHEN\n format we are concerned with.";
$parsedDescription = WeatherAlertTrait::tryParsingDescriptionText(
$rawDescription,
);
$expected = $rawDescription;

$this->assertEquals($expected, $parsedDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,23 @@ final class WeatherAlertTraitTest extends Base
"onset" => "Sunday, 10/01, 2:32 PM MDT",
"ends" => "Sunday, 10/01, 4:32 PM MDT",
"expires" => "Sunday, 10/01, 6:32 PM MDT",
"usesParsedDescription" => false,
],
(object) [
"areaDesc" => false,
"event" => "volcano warning",
"description" =>
"* WHAT...Bad weather\n\n* WHERE...Places\n\n* FAKE...This goes away",
"description" => [
"what" => "Bad weather",
"where" => "Places",
"fake" => "This goes away",
],
"geometry" => [],
"instruction" =>
"Duck! And coooover!\n\nJust duck! And cooooover!",
"onset" => false,
"ends" => false,
"expires" => false,
"usesParsedDescription" => true,
],
(object) [
"areaDesc" => "place 1",
Expand All @@ -57,6 +62,7 @@ final class WeatherAlertTraitTest extends Base
"instruction" => false,
"ends" => false,
"expires" => false,
"usesParsedDescription" => false,
],
(object) [
"event" => "air quality alert",
Expand All @@ -67,6 +73,7 @@ final class WeatherAlertTraitTest extends Base
"ends" => false,
"expires" => false,
"onset" => "Tuesday, 01/09, 12:00 PM MST",
"usesParsedDescription" => false,
],
(object) [
"event" => "air quality alert",
Expand All @@ -77,6 +84,7 @@ final class WeatherAlertTraitTest extends Base
"ends" => false,
"expires" => false,
"onset" => "Tuesday, 01/09, 1:00 PM MST",
"usesParsedDescription" => false,
],
(object) [
"event" => "air quality alert",
Expand All @@ -87,6 +95,7 @@ final class WeatherAlertTraitTest extends Base
"ends" => false,
"expires" => false,
"onset" => "Tuesday, 01/09, 1:00 PM MST",
"usesParsedDescription" => false,
],
(object) [
"event" => "air quality alert",
Expand All @@ -97,6 +106,7 @@ final class WeatherAlertTraitTest extends Base
"ends" => false,
"expires" => false,
"onset" => "Tuesday, 01/09, 2:00 PM MST",
"usesParsedDescription" => false,
],
];
}
Expand Down
35 changes: 33 additions & 2 deletions web/modules/weather_data/src/Service/WeatherAlertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ protected static function turnToDate($str, $timezone)
return $str;
}
public static function tryParsingDescriptionText($str)
{
// look for any word in all caps followed by ellipses, and use that as the label, and
// all text until the next pair of newlines is the content
//
// https://regexper.com/#%2F%5C*%5Cs%2B%28%5BA-Za-z%5Cs%5D%2B%29%5C.%5C.%5C.%28.*%29%28%5Cn%7B2%7D%7C%24%29%2F
$regex = "/\*\s+(?<label>[A-Za-z\s]+)\.\.\.(?<content>.*)(\n{2}|$)/sU";
if (preg_match_all($regex, $str, $matches)) {
$result = [];
for ($i = 0; $i < count($matches["label"]); $i++) {
$label = strtolower($matches["label"][$i]);
$content = $matches["content"][$i];
$result[$label] = $content;
}

return $result;
}

return $str;
}

/**
* Get active alerts for a WFO grid cell.
*/
Expand Down Expand Up @@ -67,9 +88,19 @@ public function getAlertsForLatLon($lat, $lon)
$output->geometry = [];
}

$output->description = self::fixupNewlines(
$output->description ?? false,
$alertDescription = self::tryParsingDescriptionText(
$output->description,
);
if (!is_array($alertDescription)) {
$output->description = self::fixupNewlines(
$alertDescription ?? false,
);
$output->usesParsedDescription = false;
} else {
$output->description = $alertDescription;
$output->usesParsedDescription = true;
}

$output->instruction = self::fixupNewlines(
$output->instruction ?? false,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<p>
{{ alert.description | nl2br }}
</p>

{% if alert.usesParsedDescription %}
{% for label, infoText in alert.description %}
<h3>{{ label | capitalize }}</h3>
<p>{{ infoText }}</p>
{% endfor %}
{% else %}
<p>{{ alert.description }}</p>
{% endif %}

{% if alert.instruction %}
<h3>What to do</h3>
<p>
Expand All @@ -25,4 +35,4 @@
</p>
</div>
</div>
{% endfor %}
{% endfor %}

0 comments on commit 4c435b0

Please sign in to comment.