harper icon indicating copy to clipboard operation
harper copied to clipboard

Paragraph detected as Single Long Sentence

Open dvdmtw98 opened this issue 1 year ago • 5 comments

Description

Harper detects a paragraph that is made of multiple sentence as one long sentence.

From the SS we can see the sentences are not 50 words long and the the paragraph consists of multiple sentences.

I took a look at multiple notes in my Vault but this paragraph seems to be the only one that has this problem.

It is shown as a Readability issue.

Resources

Screenshots

Image

Image

dvdmtw98 avatar Apr 03 '25 01:04 dvdmtw98

This is just a guess but it looks like all the sentences and with a number followed by a full stop. Harper may find this ambiguous as a floating point number with no digits after the decimal point. Try changing some of the numbers to words and other similar variations.

hippietrail avatar Apr 03 '25 10:04 hippietrail

You are right. I changed one of the number to text and the error is gone. Ideally, this shouldn't happen cause in the case of technical notes its possible to have multiple sentences that end in numbers.

Image

dvdmtw98 avatar Apr 03 '25 14:04 dvdmtw98

You are right. I changed one of the number to text and the error is gone. Ideally, this shouldn't happen cause in the case of technical notes its possible to have multiple sentences that end in numbers.

Image

Well the good news is that we diagnosed the problem. The bad news is I'm guessing it will be hard to fix an edge case like this without introducing a different edge case. I definitely consider it a bug though.

hippietrail avatar Apr 03 '25 14:04 hippietrail

I took a bit of a delve into the code to try to find the root cause of this. If I'm understanding correctly, I think the issue lies in text like "1." being lexed as just a TokenKind::Number, whereas "one." would be lexed as a TokenKind::Word followed by TokenKind::Punctuation. In other words, the lexer for numbers absorbs the dot as a decimal point even if there are no fractional digits following it.

Here is a relevant snippet from lex_number in harper-core/src/lexing/mod.rs:

while !s.is_empty() {
    if let Ok(n) = s.parse::<f64>() {
        let precision = s.chars().rev().position(|c| c == '.').unwrap_or_default();

        return Some(FoundToken {
            token: TokenKind::Number(Number {
                value: n.into(),
                suffix: None,
                radix: 10,
                precision,
            }),
            next_index: s.len(),
        });
    }

    s.pop();
}

This TokenKind::Number token in question then falls into the default false case in is_sentence_terminator (in harper-core/src/token_kind.rs), causing this unintended fusion of sentences.

pub(crate) fn is_sentence_terminator(&self) -> bool {
    match self {
        TokenKind::Punctuation(punct) => [
            Punctuation::Period,
            Punctuation::Bang,
            Punctuation::Question,
        ]
        .contains(punct),
        TokenKind::ParagraphBreak => true,
        _ => false,
    }
}

If we're okay with lexing a decimal point without following fractional digits as a period, I don't think this should be too hard to fix. AFAIK, there's currently no way to determine this from the info contained in TokenKind::Number alone. Though if we added a field like has_decimal_point, we could determine this by checking if has_decimal_point is true and precision is 0. We would then just have to add a case for this in is_sentence_terminator. (Though this would be somewhat hacky.)

This currently seems to cause issues in text like "this and that happened in 2025." as well. In which case it's more difficult to get around since it's generally not reasonable to spell out the number.

86xsk avatar Apr 27 '25 02:04 86xsk

Nice catch! I can't remember if I added support for non-integers but I definitely played with this code a while back and remember the way it parses numbers is by trying to convert text to number on a slice of changing size until the conversion succeeds.

This is nice because it avoids edge cases parsing things you might not think of if writing your own lexical analyser for them. But this shows that it can also be the source of edge cases. Should only require changing the condition from 'parses number' to 'parses number and doesn't end with .'

There are going to be other edge cases like this where two uses of a character "overlap" such as sentences ending or continuing at known or unknown abbreviations ending with a dot.

hippietrail avatar Apr 27 '25 03:04 hippietrail