I think you need a score which resets every (x) paragraphs that don’t contain enough characters to meet the threshold. So
- Loop through entire body
- Look at the current paragraph
- Check our current “is this code?” score
- Does this paragraph have a higher than expected special character score? Also, is this paragraph shorter than we’d expect?
- If we are already in “is this code?” mode, add the number of lines in sequence so far to the current score, to make it greater
- If the last (x) paragraphs have zero code, set “is this code?” to zero and record the ending line number
- If this is the first paragraph to trigger code mode set “is this code?” to the value of the paragraph score, and record the starting line number
You’d need a sizable test corpus for this to work. I know @zogstrip has one because I’ve seen it ![]()
Also
we can realistically limit our check to, say, the “big ten” languages. Per the tags page that would be C#, Java, PHP, JavaScript, Objective-C, C, C++, Python, Ruby.
This is also interesting as an approach
Run your syntax highlighter on the text. If it ends up highlighting some high percentage of it, it’s probably code.
However Random Syntax Highlighting tends to love highlighting numbers in text for no reason. So you’d need to play with that, but it’s a very interesting idea.
What happens when you randomly type text and have it in a syntax highlighting block?
Let's find out. Is this code? I dunno, is it code? You tell me.
