Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
slide-index
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Welter, Felix
slide-index
Commits
0e17f9a2
Commit
0e17f9a2
authored
4 years ago
by
felixwelter
Browse files
Options
Downloads
Patches
Plain Diff
Add title focused index relying on whoosh
parent
d8ebf70d
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app.py
+4
-4
4 additions, 4 deletions
app.py
search_index.py
+4
-3
4 additions, 3 deletions
search_index.py
title_focus_search_index.py
+30
-1
30 additions, 1 deletion
title_focus_search_index.py
with
38 additions
and
8 deletions
app.py
+
4
−
4
View file @
0e17f9a2
...
...
@@ -6,14 +6,14 @@ import pdfplumber
from
flask
import
Flask
,
render_template
,
request
,
redirect
,
send_file
,
jsonify
from
werkzeug.utils
import
secure_filename
from
title_focus_search_index
import
TitleFocusSearchIndex
from
title_focus_search_index
import
IndexBased
TitleFocusSearchIndex
app
=
Flask
(
__name__
)
SLIDE_DIR
=
"
slides
"
IMAGE_DIR
=
"
img_cache
"
Index
=
TitleFocusSearchIndex
Index
=
IndexBased
TitleFocusSearchIndex
@app.route
(
'
/
'
)
...
...
@@ -39,7 +39,7 @@ def upload():
index
=
Index
()
for
i
,
page
in
enumerate
(
pdf
.
pages
):
text
=
page
.
extract_text
()
index
.
add
(
str
(
file_path
),
i
,
text
)
index
.
add
(
str
(
file_path
),
i
,
text
,
text
.
split
(
"
\n
"
)[
0
])
# Assumes title in the first line
img_name
=
str
(
file_path
)[
7
:]
+
"
_
"
+
str
(
i
)
+
"
.jpg
"
img_path
=
os
.
path
.
join
(
IMAGE_DIR
,
img_name
)
page
.
to_image
().
save
(
img_path
)
...
...
@@ -52,7 +52,7 @@ def query():
try
:
index
=
Index
()
query
=
request
.
form
.
get
(
"
term
"
)
context
=
request
.
form
.
get
(
"
context
"
)
# TODO: Use context to find better results
context
=
request
.
form
.
get
(
"
context
"
)
result
=
index
.
search
(
query
,
context
)
img_name
=
result
[
"
path
"
][
7
:]
+
"
_
"
+
str
(
result
[
"
page
"
])
+
"
.jpg
"
return
jsonify
({
...
...
This diff is collapsed.
Click to expand it.
search_index.py
+
4
−
3
View file @
0e17f9a2
...
...
@@ -10,7 +10,8 @@ class BasicSearchIndex:
"""
Expose relevant functions of Whoosh using a simple interface
"""
def
__init__
(
self
,
index_dir
=
"
index
"
):
self
.
schema
=
Schema
(
path
=
ID
(
stored
=
True
),
page
=
NUMERIC
(
stored
=
True
),
content
=
TEXT
(
stored
=
True
))
self
.
schema
=
Schema
(
path
=
ID
(
stored
=
True
),
page
=
NUMERIC
(
stored
=
True
),
content
=
TEXT
(
stored
=
True
),
title
=
TEXT
(
stored
=
True
))
try
:
self
.
ix
=
open_dir
(
index_dir
)
except
EmptyIndexError
:
...
...
@@ -19,9 +20,9 @@ class BasicSearchIndex:
def
create
(
self
,
index_dir
):
self
.
ix
=
create_in
(
index_dir
,
self
.
schema
)
def
add
(
self
,
path
,
page
,
content
):
def
add
(
self
,
path
,
page
,
content
,
title
):
writer
=
self
.
ix
.
writer
()
writer
.
add_document
(
path
=
path
,
page
=
page
,
content
=
content
)
writer
.
add_document
(
path
=
path
,
page
=
page
,
content
=
content
,
title
=
title
)
writer
.
commit
()
def
result_list
(
self
,
query
,
context
):
...
...
This diff is collapsed.
Click to expand it.
title_focus_search_index.py
+
30
−
1
View file @
0e17f9a2
from
nltk
import
word_tokenize
from
whoosh.qparser
import
QueryParser
,
OrGroup
from
search_index
import
BasicSearchIndex
class
TitleFocusSearchIndex
(
BasicSearchIndex
):
"""
Return fitting slides, favouring slides with the query contained in the title
"""
def
__init__
(
self
,
booster_words
=
None
):
self
.
booster_words
=
booster_words
or
[
"
concept
"
,
"
definition
"
]
super
().
__init__
()
def
search
(
self
,
query
,
context
):
results
=
self
.
result_list
(
query
,
context
)
# Look for result with query and booster word in the title
for
result
in
results
:
if
query
.
lower
()
in
result
[
"
content
"
].
split
(
"
\n
"
)[
0
].
lower
():
if
query
.
lower
()
in
result
[
"
title
"
].
lower
()
\
and
any
([
bw
in
result
[
"
title
"
].
lower
()
for
bw
in
self
.
booster_words
]):
return
result
for
result
in
results
:
if
query
.
lower
()
in
result
[
"
title
"
].
lower
():
return
result
return
results
[
0
]
class
IndexBasedTitleFocusSearchIndex
(
BasicSearchIndex
):
"""
Return fitting slides, querying for slides with the query contained in the title
"""
def
result_list
(
self
,
query
,
context
):
user_query
=
query
query
=
"
title:({})^2
"
.
format
(
user_query
)
query
+=
"
content:({})^1.2
"
.
format
(
user_query
)
if
len
(
context
.
strip
())
>
0
:
query
+=
"
title:({})^0.2
"
.
format
(
context
)
query
+=
"
content:({})^0.1
"
.
format
(
context
)
query_parser
=
QueryParser
(
"
content
"
,
self
.
ix
.
schema
,
group
=
OrGroup
.
factory
(
0.9
))
parsed_query
=
query_parser
.
parse
(
query
)
return
self
.
ix
.
searcher
().
search
(
parsed_query
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment