diff options
author | Andres Rey <[email protected]> | 2018-05-05 10:50:59 +0100 |
---|---|---|
committer | Andres Rey <[email protected]> | 2018-05-05 10:50:59 +0100 |
commit | 4359f3c56c19494a8094c5e6a1b81de90b0b40c7 (patch) | |
tree | 32c6f8c9b097528b2b04137db7ad582b44160583 /src | |
parent | beb2766da7bc205bfdebafbfb168168d375bf4a4 (diff) |
Issue #63: Avoid diving by zero + test case
Diffstat (limited to 'src')
-rw-r--r-- | src/Readability.php | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/Readability.php b/src/Readability.php index d69897b..b8b5b5b 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -983,7 +983,9 @@ class Readability // and whose scores are quite closed with current `topCandidate` node. $alternativeCandidateAncestors = []; for ($i = 1; $i < count($topCandidates); $i++) { - if ($topCandidates[$i]->contentScore / $topCandidate->contentScore >= 0.75) { + // In some cases we may end up with a top candidate with zero content score. To avoid dividing by zero + // we have to use max() and replace zero with a low value like 0.1 + if ($topCandidates[$i]->contentScore / max($topCandidate->contentScore, 0.1) >= 0.75) { array_push($alternativeCandidateAncestors, $topCandidates[$i]->getNodeAncestors(false)); } } |