Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fcs-clarin-endpoint-hamburg
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
Arkhangelskiy, Timofey
fcs-clarin-endpoint-hamburg
Commits
209a749e
Commit
209a749e
authored
2 years ago
by
Arkhangelskiy, Timofey
Browse files
Options
Downloads
Patches
Plain Diff
Start advanced search processing functions
parent
628d8a55
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
common/query_parser.py
+61
-13
61 additions, 13 deletions
common/query_parser.py
common/views_logic.py
+10
-5
10 additions, 5 deletions
common/views_logic.py
main.py
+1
-2
1 addition, 2 deletions
main.py
with
72 additions
and
20 deletions
common/query_parser.py
+
61
−
13
View file @
209a749e
...
...
@@ -10,9 +10,13 @@ class QueryParser:
This class contains commonly used methods for initial parsing of a GET
query. It does not include platform-specific methods.
"""
# Regexes for simple search
rxTermQuery
=
re
.
compile
(
'
^(?:(?:[^
"
]|
\\\\
"
)*|
"
(?:[^
"
]|
\\\\
"
)*
"
)$
'
)
# Regexes for advanced search
rxWithinClause
=
re
.
compile
(
'
+within +(s|sentence|u|utterance|p|paragraph|
'
'
t|turn|text|session) *$
'
)
def
__init__
(
self
):
pass
...
...
@@ -91,10 +95,10 @@ class QueryParser:
# Abstract function
raise
NotImplementedError
()
def
translate_
fcsql
(
self
,
query
:
str
,
config
:
ResourceConfig
,
basicSearch
:
bool
=
False
,
start
=
0
,
end
=-
1
):
def
translate_
simple
(
self
,
query
:
str
,
config
:
ResourceConfig
,
start
=
0
,
end
=-
1
):
"""
Translate a
n FCS-
QL query into a corpus-specific query
(GET query,
JSON Elasticsearch query or whatever).
Translate a
simple search (C
QL
)
query into a corpus-specific query
(GET query,
JSON Elasticsearch query or whatever).
If something is wrong with the query, raise a Diagnostic exception.
This is a top-level platform-independent function. It recursively
parses the query by locating the hierarchically highest logical operator
...
...
@@ -110,8 +114,7 @@ class QueryParser:
raise
Diagnostic
(
DiagnosticTypes
.
sru
,
27
)
if
self
.
rxTermQuery
.
search
(
query
)
is
not
None
:
return
self
.
build_get_string
(
self
.
term_query
(
query
,
config
),
config
)
return
self
.
build_get_string
(
self
.
translate_fcsql
(
query
,
config
,
basicSearch
=
basicSearch
,
return
self
.
build_get_string
(
self
.
translate_simple
(
query
,
config
,
start
=
start
,
end
=
end
),
config
)
# if query.count('(') != query.count(')'):
...
...
@@ -129,21 +132,66 @@ class QueryParser:
iOpPos
,
strOp
=
self
.
find_operator
(
query
,
start
,
end
)
if
iOpPos
==
-
1
:
if
query
[
start
]
==
'
(
'
and
query
[
end
-
1
]
==
'
)
'
:
return
self
.
translate_
fcsql
(
query
,
config
,
basicSearch
=
basicSearch
,
start
=
start
+
1
,
end
=
end
-
1
)
return
self
.
translate_
simple
(
query
,
config
,
start
=
start
+
1
,
end
=
end
-
1
)
else
:
return
self
.
term_query
(
query
[
start
:
end
],
config
)
if
strOp
in
(
'
AND
'
,
'
OR
'
):
resultLeft
=
self
.
translate_fcsql
(
query
,
config
,
basicSearch
=
basicSearch
,
start
=
start
,
end
=
iOpPos
)
resultRight
=
self
.
translate_fcsql
(
query
,
config
,
basicSearch
=
basicSearch
,
start
=
iOpPos
+
len
(
strOp
),
end
=
end
)
resultLeft
=
self
.
translate_simple
(
query
,
config
,
start
=
start
,
end
=
iOpPos
)
resultRight
=
self
.
translate_simple
(
query
,
config
,
start
=
iOpPos
+
len
(
strOp
),
end
=
end
)
if
len
(
resultLeft
)
<=
0
or
len
(
resultRight
)
<=
0
:
raise
Diagnostic
(
DiagnosticTypes
.
sru
,
10
)
return
self
.
binary_bool
(
strOp
,
resultLeft
,
resultRight
,
config
)
elif
strOp
==
'
NOT
'
:
resultRight
=
self
.
translate_
fcsql
(
query
,
config
,
basicSearch
=
basicSearch
,
start
=
iOpPos
+
len
(
strOp
),
resultRight
=
self
.
translate_
simple
(
query
,
config
,
start
=
iOpPos
+
len
(
strOp
),
end
=
end
)
return
self
.
not_bool
(
resultRight
,
config
)
return
{}
def
adv_main_query
(
self
,
query
:
str
,
config
:
ResourceConfig
,
start
=
0
,
end
=-
1
):
if
len
(
query
)
<=
0
:
raise
Diagnostic
(
DiagnosticTypes
.
sru
,
27
)
if
start
>=
len
(
query
)
-
1
or
end
<=
0
:
raise
Diagnostic
(
DiagnosticTypes
.
sru
,
10
)
while
start
<
len
(
query
)
and
query
[
start
]
in
'
\t\n
'
:
start
+=
1
while
end
>
0
and
query
[
end
-
1
]
in
'
\t\n
'
:
end
-=
1
if
start
>=
end
:
raise
Diagnostic
(
DiagnosticTypes
.
sru
,
10
)
raise
NotImplementedError
return
{}
def
translate_advanced
(
self
,
query
:
str
,
config
:
ResourceConfig
):
"""
Translate an advanced search (FCS-QL) query into a corpus-specific query
(GET query, JSON Elasticsearch query or whatever).
If something is wrong with the query, raise a Diagnostic exception.
This is a top-level platform-independent function. It recursively
parses the query by locating the hierarchically highest logical operator
in the current query and then calling a respective lower-level
function, which may be platform-specific.
"""
withinClause
=
''
end
=
len
(
query
)
m
=
self
.
rxWithinClause
.
search
(
query
)
if
m
is
not
None
:
withinClause
=
m
.
group
(
1
)
if
withinClause
==
'
s
'
:
withinClause
=
'
sentence
'
elif
withinClause
==
'
u
'
:
withinClause
=
'
utterance
'
elif
withinClause
==
'
p
'
:
withinClause
=
'
paragraph
'
elif
withinClause
==
'
t
'
:
withinClause
=
'
turn
'
query
=
self
.
rxWithinClause
.
sub
(
''
,
query
)
end
=
len
(
query
)
if
end
==
0
:
raise
Diagnostic
(
DiagnosticTypes
.
sru
,
27
)
return
self
.
adv_main_query
(
query
,
config
,
start
=
0
,
end
=
end
)
def
validate_query
(
self
,
operation
,
version
,
queryType
,
query
,
xFcsEndpointDescription
,
xFcsContext
,
xFcsDataviews
,
xFcsRewritesAllowed
):
...
...
@@ -182,7 +230,7 @@ class QueryParser:
# Check version-specific parameters and values
if
version
==
SRUVersion
.
v1_2
:
if
queryType
==
QueryType
.
cql
:
if
queryType
==
QueryType
.
fcs
:
diagnostics
.
append
(
Diagnostic
(
DiagnosticTypes
.
sru
,
8
,
details
=
'
queryType
'
))
for
dv
in
xFcsDataviews
.
split
(
'
,
'
):
dv
=
dv
.
strip
()
...
...
This diff is collapsed.
Click to expand it.
common/views_logic.py
+
10
−
5
View file @
209a749e
...
...
@@ -80,8 +80,11 @@ def process_search_retrieve(version: SRUVersion,
"""
if
config
.
platform
==
CorpPlatform
.
tsakorpus
:
try
:
strGetParams
=
app
.
qp_tsakorpus
.
translate_fcsql
(
query
,
config
)
print
(
strGetParams
)
if
queryType
==
QueryType
.
cql
:
strGetParams
=
app
.
qp_tsakorpus
.
translate_simple
(
query
,
config
)
else
:
strGetParams
=
app
.
qp_tsakorpus
.
translate_advanced
(
query
,
config
)
# print(strGetParams)
res
=
app
.
qp_tsakorpus
.
send_query
(
strGetParams
,
config
)
except
Diagnostic
as
diag
:
return
fatal_response
(
Operation
.
searchRetrieve
,
version
,
diagnostics
+
[
diag
],
request
,
templates
)
...
...
@@ -98,9 +101,11 @@ def process_search_retrieve(version: SRUVersion,
media_type
=
'
application/xml
'
)
elif
config
.
platform
==
CorpPlatform
.
litterae
:
try
:
strGetParams
=
app
.
qp_litterae
.
translate_fcsql
(
query
,
config
)
print
(
strGetParams
)
# return strGetParams
if
queryType
==
QueryType
.
cql
:
strGetParams
=
app
.
qp_litterae
.
translate_simple
(
query
,
config
)
else
:
strGetParams
=
app
.
qp_litterae
.
translate_simple
(
query
,
config
)
# print(strGetParams)
res
=
app
.
qp_litterae
.
send_query
(
strGetParams
,
config
)
print
(
res
)
except
Diagnostic
as
diag
:
...
...
This diff is collapsed.
Click to expand it.
main.py
+
1
−
2
View file @
209a749e
...
...
@@ -42,7 +42,7 @@ def endpoint(
corpusID
:
str
,
operation
:
Operation
=
Operation
.
explain
,
version
:
SRUVersion
=
SRUVersion
.
v2_0
,
queryType
:
QueryType
=
QueryType
.
fcs
,
queryType
:
QueryType
=
QueryType
.
cql
,
query
:
str
=
''
,
xFcsEndpointDescription
:
str
=
Query
(
default
=
''
,
...
...
@@ -82,7 +82,6 @@ def endpoint(
xFcsDataviews
,
xFcsRewritesAllowed
)
# Now, do the substantial things
return
process_request
(
operation
,
version
,
queryType
,
query
,
searchOptions
,
config
,
diagnostics
,
app
,
request
,
templates
)
# return {'operation': operation, 'version': version}
if
__name__
==
'
__main__
'
:
...
...
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