-
Notifications
You must be signed in to change notification settings - Fork 53
[222_34] 增强html的格式检测 #2655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[222_34] 增强html的格式检测 #2655
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,220 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ;; Html | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ;; 按行分割文本 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (define (html-string-split-lines s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (let ((len (if (>= (string-length s) 1000) 1000 (string-length s)))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (let loop ((i 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (start 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (result '())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (cond ((>= i len) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (reverse (cons (substring s start i) result))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ((char=? (string-ref s i) #\newline) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (loop (+ i 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (+ i 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (cons (substring s start i) result))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (else (loop (+ i 1) start result)))))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ;; 某个字符在文本中的含量 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (define (charactor-from-string s ch) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (if (not (string-null? s)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (let* ((len (string-length s)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (limit (if (>= len 1000) 1000 len))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (let loop ((ref 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (count 0)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (if (>= ref limit) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (/ count len) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (/ count len) | |
| (/ count limit) |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error in density calculation. The function sums the densities returned by charactor-from-string (which are already ratios count/len) and then divides by len again. This double division produces incorrect results. The correct approach is to sum the character counts and then divide once by the total length, or use the already-computed density values without further division.
| (/ (+ (charactor-from-string substr #\<) | |
| (charactor-from-string substr #\>)) | |
| len)))) | |
| (+ (charactor-from-string substr #\<) | |
| (charactor-from-string substr #\>))))) |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance concern: This function performs multiple linear scans of the same string, calling html-string-count-substring 34 times. Each call scans the entire substring. For better performance, consider combining these checks into a single pass through the string, using a state machine or regex pattern matching to identify all tag types in one scan.
| (let ((count (+ (html-string-count-substring lc-substr "<div") | |
| (html-string-count-substring lc-substr "<span") | |
| (html-string-count-substring lc-substr "<p") | |
| (html-string-count-substring lc-substr "<a") | |
| (html-string-count-substring lc-substr "<img") | |
| (html-string-count-substring lc-substr "<ul") | |
| (html-string-count-substring lc-substr "<ol") | |
| (html-string-count-substring lc-substr "<li") | |
| (html-string-count-substring lc-substr "<table") | |
| (html-string-count-substring lc-substr "<tr") | |
| (html-string-count-substring lc-substr "<td") | |
| (html-string-count-substring lc-substr "<th") | |
| (html-string-count-substring lc-substr "<h1") | |
| (html-string-count-substring lc-substr "<h2") | |
| (html-string-count-substring lc-substr "<h3") | |
| (html-string-count-substring lc-substr "<h4") | |
| (html-string-count-substring lc-substr "<h5") | |
| (html-string-count-substring lc-substr "<h6") | |
| (html-string-count-substring lc-substr "<form") | |
| (html-string-count-substring lc-substr "<input") | |
| (html-string-count-substring lc-substr "<button") | |
| (html-string-count-substring lc-substr "<textarea") | |
| (html-string-count-substring lc-substr "<select") | |
| (html-string-count-substring lc-substr "<option") | |
| (html-string-count-substring lc-substr "<style") | |
| (html-string-count-substring lc-substr "<script") | |
| (html-string-count-substring lc-substr "<meta") | |
| (html-string-count-substring lc-substr "<link") | |
| (html-string-count-substring lc-substr "</div") | |
| (html-string-count-substring lc-substr "</ul") | |
| (html-string-count-substring lc-substr "</ol") | |
| (html-string-count-substring lc-substr "</table") | |
| (html-string-count-substring lc-substr "</tr") | |
| (html-string-count-substring lc-substr "</form") | |
| (html-string-count-substring lc-substr "</style") | |
| (html-string-count-substring lc-substr "</script")))) | |
| (/ count len))))) | |
| (letrec* ((string-prefix-at? | |
| (lambda (s prefix idx) | |
| (let* ((s-len (string-length s)) | |
| (p-len (string-length prefix))) | |
| (if (> (+ idx p-len) s-len) | |
| #f | |
| (let loop ((j 0)) | |
| (if (= j p-len) | |
| #t | |
| (if (char=? (string-ref s (+ idx j)) | |
| (string-ref prefix j)) | |
| (loop (+ j 1)) | |
| #f))))))) | |
| (tags '("<div" | |
| "<span" | |
| "<p" | |
| "<a" | |
| "<img" | |
| "<ul" | |
| "<ol" | |
| "<li" | |
| "<table" | |
| "<tr" | |
| "<td" | |
| "<th" | |
| "<h1" | |
| "<h2" | |
| "<h3" | |
| "<h4" | |
| "<h5" | |
| "<h6" | |
| "<form" | |
| "<input" | |
| "<button" | |
| "<textarea" | |
| "<select" | |
| "<option" | |
| "<style" | |
| "<script" | |
| "<meta" | |
| "<link" | |
| "</div" | |
| "</ul" | |
| "</ol" | |
| "</table" | |
| "</tr" | |
| "</form" | |
| "</style" | |
| "</script"))) | |
| (substr-len (string-length lc-substr))) | |
| (let loop ((i 0) (count 0)) | |
| (if (>= i substr-len) | |
| (/ count len) | |
| (let ((new-count | |
| (let tag-loop ((ts tags) (c count)) | |
| (if (null? ts) | |
| c | |
| (if (string-prefix-at? lc-substr (car ts) i) | |
| (tag-loop (cdr ts) (+ c 1)) | |
| (tag-loop (cdr ts) c)))))) | |
| (loop (+ i 1) new-count))))))) |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error in density calculation. The function sums the densities returned by charactor-from-string (which are already ratios count/len) and then divides by len again. This double division produces incorrect results. The correct approach is to sum the character counts and then divide once by the total length, or use the already-computed density values without further division.
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance concern: This function performs multiple linear scans of the same string, calling html-string-count-substring 25 times. Each call scans the entire line. For better performance, consider combining these checks into a single pass through the line, or using a more efficient pattern matching approach.
| ;; 这一行文本是否包含html标签 | |
| (define (html-line-contains-features? line) | |
| (let ((lc-line (string-downcase line))) | |
| (or | |
| (> (html-string-count-substring lc-line "<div") 0) | |
| (> (html-string-count-substring lc-line "<span") 0) | |
| (> (html-string-count-substring lc-line "<p") 0) | |
| (> (html-string-count-substring lc-line "<a") 0) | |
| (> (html-string-count-substring lc-line "<img") 0) | |
| (> (html-string-count-substring lc-line "<ul") 0) | |
| (> (html-string-count-substring lc-line "<ol") 0) | |
| (> (html-string-count-substring lc-line "<li") 0) | |
| (> (html-string-count-substring lc-line "<table") 0) | |
| (> (html-string-count-substring lc-line "<tr") 0) | |
| (> (html-string-count-substring lc-line "<td") 0) | |
| (> (html-string-count-substring lc-line "<th") 0) | |
| (> (html-string-count-substring lc-line "<h1") 0) | |
| (> (html-string-count-substring lc-line "<h2") 0) | |
| (> (html-string-count-substring lc-line "<h3") 0) | |
| (> (html-string-count-substring lc-line "<h4") 0) | |
| (> (html-string-count-substring lc-line "<h5") 0) | |
| (> (html-string-count-substring lc-line "<h6") 0) | |
| (> (html-string-count-substring lc-line "</div") 0) | |
| (> (html-string-count-substring lc-line "</span") 0) | |
| (> (html-string-count-substring lc-line "</p") 0) | |
| (> (html-string-count-substring lc-line "</a") 0) | |
| (> (html-string-count-substring lc-line "/>") 0) | |
| (> (html-string-count-substring lc-line "<!doctype") 0) | |
| (> (html-string-count-substring lc-line "<?xml") 0)))) | |
| ;; Helper: check whether STR has PREFIX starting at position POS | |
| (define (string-prefix-at? str prefix pos) | |
| (let* ((len-str (string-length str)) | |
| (len-pre (string-length prefix)) | |
| (end (+ pos len-pre))) | |
| (and (<= end len-str) | |
| (string=? (substring str pos end) prefix)))) | |
| ;; 这一行文本是否包含html标签 | |
| (define (html-line-contains-features? line) | |
| (let* ((lc-line (string-downcase line)) | |
| (len (string-length lc-line))) | |
| (let loop ((i 0)) | |
| (cond | |
| ((>= i len) #f) | |
| (else | |
| (let ((c (string-ref lc-line i))) | |
| (cond | |
| ;; Check for patterns starting with '<' | |
| ((char=? c #\<) | |
| (if (or (string-prefix-at? lc-line "<div" i) | |
| (string-prefix-at? lc-line "<span" i) | |
| (string-prefix-at? lc-line "<p" i) | |
| (string-prefix-at? lc-line "<a" i) | |
| (string-prefix-at? lc-line "<img" i) | |
| (string-prefix-at? lc-line "<ul" i) | |
| (string-prefix-at? lc-line "<ol" i) | |
| (string-prefix-at? lc-line "<li" i) | |
| (string-prefix-at? lc-line "<table" i) | |
| (string-prefix-at? lc-line "<tr" i) | |
| (string-prefix-at? lc-line "<td" i) | |
| (string-prefix-at? lc-line "<th" i) | |
| (string-prefix-at? lc-line "<h1" i) | |
| (string-prefix-at? lc-line "<h2" i) | |
| (string-prefix-at? lc-line "<h3" i) | |
| (string-prefix-at? lc-line "<h4" i) | |
| (string-prefix-at? lc-line "<h5" i) | |
| (string-prefix-at? lc-line "<h6" i) | |
| (string-prefix-at? lc-line "</div" i) | |
| (string-prefix-at? lc-line "</span" i) | |
| (string-prefix-at? lc-line "</p" i) | |
| (string-prefix-at? lc-line "</a" i) | |
| (string-prefix-at? lc-line "<!doctype" i) | |
| (string-prefix-at? lc-line "<?xml" i)) | |
| #t | |
| (loop (+ i 1)))) | |
| ;; Preserve detection of "/>" anywhere in the line | |
| ((and (char=? c #\/) | |
| (< (+ i 1) len) | |
| (char=? (string-ref lc-line (+ i 1)) #\>)) | |
| #t) | |
| (else | |
| (loop (+ i 1))))))))) |
Copilot
AI
Jan 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation. This line has a leading space before the opening parenthesis, while all other function definitions in the file start at column 1. Remove the leading space for consistency.
| (define (is-html-string? s) | |
| (define (is-html-string? s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling error in function name: "charactor" should be "character". This typo appears in the function name and should be corrected for consistency with standard English spelling.