Skip to content

Commit b8a4be7

Browse files
authored
Merge pull request #6088 from dudua99/fix/pdf-outline-filter-rank-4497
תיקון issue #4497 — סינון ניווט PDF: דף עם סימן עמוד כמילה שלמה, ודירוג מדויק/תחילית
2 parents f1389c1 + 1d512b1 commit b8a4be7

3 files changed

Lines changed: 171 additions & 10 deletions

File tree

‎lib/pdf_book/view/pdf_outlines_screen.dart‎

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,107 @@ List<PdfOutlineSearchEntry> filterPdfOutline(
4444
) {
4545
final normalizedQuery = normalizeFindText(rawQuery);
4646
if (normalizedQuery.isEmpty) return entries;
47-
return entries
48-
.where(
49-
(e) => findNormalizedTextMatches(
50-
normalizedQuery: normalizedQuery,
51-
normalizedPrimaryText: e.normalizedTitle,
52-
),
53-
)
54-
.toList();
47+
// הנרמול מוחק "." ו-":", ולכן "ב." היה תואם כל כותרת עם האות ב ו"קיא:"
48+
// היה שווה ל"קיא.". שאילתה שמסתיימת בסימן עמוד מחפשת את הדף כמילה שלמה.
49+
final amud = _amudMark(rawQuery);
50+
final dafStart = normalizedQuery.lastIndexOf(' ') + 1;
51+
final daf = amud == null ? null : normalizedQuery.substring(dafStart);
52+
final exact = <PdfOutlineSearchEntry>[];
53+
final exactUnmarked = <PdfOutlineSearchEntry>[];
54+
final prefix = <PdfOutlineSearchEntry>[];
55+
final prefixUnmarked = <PdfOutlineSearchEntry>[];
56+
final contains = <PdfOutlineSearchEntry>[];
57+
final containsUnmarked = <PdfOutlineSearchEntry>[];
58+
for (final entry in entries) {
59+
final title = entry.normalizedTitle;
60+
final firstMatch = title.indexOf(normalizedQuery);
61+
final rank = title == normalizedQuery
62+
? 0
63+
: firstMatch == 0
64+
? 1
65+
: firstMatch > 0
66+
? 2
67+
: -1;
68+
if (rank < 0) continue;
69+
final dafMatch = daf == null
70+
? (matches: true, marked: false)
71+
: _titleDafMatch(
72+
entry.node.title,
73+
title,
74+
normalizedQuery,
75+
firstMatch,
76+
daf,
77+
dafStart,
78+
amud!,
79+
);
80+
if (!dafMatch.matches) continue;
81+
final prefersMarked = amud != null && dafMatch.marked;
82+
switch (rank) {
83+
case 0:
84+
(prefersMarked ? exact : exactUnmarked).add(entry);
85+
case 1:
86+
(prefersMarked ? prefix : prefixUnmarked).add(entry);
87+
default:
88+
(prefersMarked ? contains : containsUnmarked).add(entry);
89+
}
90+
}
91+
// מדויק, תחילית ואז הכלה. סימן עמוד תואם קודם באותה דרגה.
92+
return [
93+
...exact,
94+
...exactUnmarked,
95+
...prefix,
96+
...prefixUnmarked,
97+
...contains,
98+
...containsUnmarked,
99+
];
55100
}
56101

102+
String? _amudMark(String text) {
103+
final trimmed = text.trimRight();
104+
if (trimmed.endsWith('.')) return '.';
105+
if (trimmed.endsWith(':')) return ':';
106+
return null;
107+
}
108+
109+
/// האם יש בכותרת מילה שהיא הדף [daf], בלי סימן עמוד או עם הסימן [amud].
110+
({bool matches, bool marked}) _titleDafMatch(
111+
String rawTitle,
112+
String normalizedTitle,
113+
String normalizedQuery,
114+
int firstMatch,
115+
String daf,
116+
int dafStart,
117+
String amud,
118+
) {
119+
final matches = <int>{};
120+
for (var start = firstMatch; start >= 0;) {
121+
matches.add(start + dafStart);
122+
start = normalizedTitle.indexOf(normalizedQuery, start + 1);
123+
}
124+
var normalizedOffset = 0;
125+
var hasWord = false;
126+
var unmarked = false;
127+
for (final word in rawTitle.split(_whitespace)) {
128+
final normalizedWord = normalizeFindText(word);
129+
if (normalizedWord.isEmpty) continue;
130+
if (hasWord) normalizedOffset++;
131+
hasWord = true;
132+
final dafOffset = normalizedWord.length - daf.length;
133+
if (normalizedWord.endsWith(daf) &&
134+
(dafOffset == 0 || normalizedWord[dafOffset - 1] == ' ') &&
135+
matches.contains(normalizedOffset + dafOffset)) {
136+
final mark = _amudMark(word.replaceFirst(_closingPunctuation, ''));
137+
if (mark == amud) return (matches: true, marked: true);
138+
if (mark == null) unmarked = true;
139+
}
140+
normalizedOffset += normalizedWord.length;
141+
}
142+
return (matches: unmarked, marked: false);
143+
}
144+
145+
final RegExp _whitespace = RegExp(r'\s+');
146+
final RegExp _closingPunctuation = RegExp(r'''[)\]}"'׳״”’»›]+$''');
147+
57148
class OutlineView extends StatefulWidget {
58149
const OutlineView({
59150
super.key,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:otzaria/pdf_book/view/pdf_outlines_screen.dart';
3+
import 'package:pdfrx/pdfrx.dart';
4+
5+
// issue #4497: בחולין PDF, חיפוש "ב." בחלונית הניווט הוביל ל"נ." וחיפוש
6+
// "קיא:" הוביל ל"קב" — הנקודה והנקודתיים נמחקו בנרמול, ו"ב" התאים לכל דף
7+
// שיש בו האות ב, בסדר תוכן העניינים.
8+
List<String> _filter(List<String> titles, String query) {
9+
final outline = [
10+
for (final t in titles)
11+
PdfOutlineNode(title: t, dest: null, children: const []),
12+
];
13+
return filterPdfOutline(
14+
flattenPdfOutlineForSearch(outline),
15+
query,
16+
).map((e) => e.node.title).toList();
17+
}
18+
19+
void main() {
20+
const chullin = ['ב.', 'ב:', 'יב.', 'נ.', 'נב.', 'קב.', 'קיא.', 'קיא:'];
21+
22+
test('"ב." מוצא את דף ב עמוד א בלבד, ולא נב./יב./קב.', () {
23+
expect(_filter(chullin, 'ב.'), ['ב.']);
24+
});
25+
26+
test('"קיא:" מוצא את עמוד ב ולא את עמוד א', () {
27+
expect(_filter(chullin, 'קיא:'), ['קיא:']);
28+
expect(_filter(chullin, 'קיא.'), ['קיא.']);
29+
});
30+
31+
test('סימן עמוד בתוך סוגריים נשמר, וסימן תואם קודם באותה דרגה', () {
32+
expect(_filter(['דף (קיא.)', 'דף (קיא:)', 'דף קיא'], 'קיא:'), [
33+
'דף (קיא:)',
34+
'דף קיא',
35+
]);
36+
expect(_filter(['דף ב', 'דף ב.'], 'ב.'), ['דף ב.', 'דף ב']);
37+
// בלי סימן מפורש נשמר סדר תוכן העניינים.
38+
expect(_filter(['דף ב', 'דף ב.'], 'ב'), ['דף ב', 'דף ב.']);
39+
});
40+
41+
test('סימן העמוד שייך לאותו מופע שבו מסתיימת התאמת השאילתה', () {
42+
expect(_filter(['חלק ב: דף ב.'], 'חלק ב.'), isEmpty);
43+
expect(_filter(['חלק ב: דף ב.'], 'ב.'), ['חלק ב: דף ב.']);
44+
expect(_filter(['דף־ב:', 'דף־ב.'], 'דף ב:'), ['דף־ב:']);
45+
expect(_filter(['יב־ב:'], 'יב:'), isEmpty);
46+
});
47+
48+
test('דף עם עמוד בתוך כותרת ארוכה נמצא, ובלי עמוד — כל הדף', () {
49+
const titles = ['חולין דף ב.', 'חולין דף ב:', 'חולין דף נב.', 'מסכת ב"ב'];
50+
expect(_filter(titles, 'ב:'), ['חולין דף ב:']);
51+
expect(_filter(titles, 'דף ב.'), ['חולין דף ב.']);
52+
// כותרת בלי סימן עמוד אינה נפסלת בגלל העמוד.
53+
expect(_filter(['דף ב', 'דף נב'], 'ב.'), ['דף ב']);
54+
});
55+
56+
test('בלי סימן עמוד: התאמה מדויקת ותחילית קודמות להכלה', () {
57+
expect(_filter(chullin, 'קיא'), ['קיא.', 'קיא:']);
58+
expect(_filter(chullin, 'ב').take(2), ['ב.', 'ב:']);
59+
expect(_filter(['בבא בתרא דף ב', 'דף ג', 'דף קו'], 'דף'), [
60+
'דף ג',
61+
'דף קו',
62+
'בבא בתרא דף ב',
63+
]);
64+
});
65+
}

‎test/pdf_book/pdf_outline_search_filter_test.dart‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ void main() {
1616
_node('מסכת חולין', [_node('דף קו')]),
1717
];
1818

19-
test('הסינון זהה לבדיקת כותרת בודדת, באותו סדר ועם אותן רמות', () {
19+
test('הסינון זהה לבדיקת כותרת בודדת, עם אותן רמות', () {
2020
final entries = flattenPdfOutlineForSearch(outline);
2121
for (final query in [
2222
'',
2323
'בבא',
24-
'דף',
2524
'מסכת',
2625
'הקדמה',
2726
'חולין קו',
@@ -33,6 +32,12 @@ void main() {
3332
.toList();
3433
expect(filterPdfOutline(entries, query), expected, reason: query);
3534
}
35+
// תחילית קודמת להכלה (issue #4497); בתוך דרגה — סדר תוכן העניינים.
36+
expect(filterPdfOutline(entries, 'דף').map((e) => e.node.title), [
37+
'דף ג',
38+
'דף קו',
39+
'בָּבָא בַּתְרָא דף ב',
40+
]);
3641
expect(entries.map((e) => (e.node.title, e.level)).toList(), [
3742
('הַקְדָּמָה', 0),
3843
('מסכת ב"ב', 0),

0 commit comments

Comments
 (0)