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
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
7f1707db
Commit
7f1707db
authored
2 years ago
by
Arkhangelskiy, Timofey
Browse files
Options
Downloads
Patches
Plain Diff
Tsakorpus searchRetrieve response (not complete yet)
parent
81840846
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
common/tsakorpus_response_parser.py
+69
-0
69 additions, 0 deletions
common/tsakorpus_response_parser.py
main.py
+13
-1
13 additions, 1 deletion
main.py
static/search_retrieve_response.xml
+2
-2
2 additions, 2 deletions
static/search_retrieve_response.xml
with
84 additions
and
3 deletions
common/tsakorpus_response_parser.py
0 → 100644
+
69
−
0
View file @
7f1707db
from
urllib.parse
import
quote
import
re
import
json
import
urllib.request
from
lxml.html
import
fragment_fromstring
from
.enums
import
*
from
.config
import
ResourceConfig
from
.search_retrieve
import
Record
from
.diagnostics
import
Diagnostic
,
DiagnosticTypes
class
TsakorpusResponseParser
:
"""
Parses responses from a Tsakorpus instance.
"""
def
__init__
(
self
):
pass
def
parse_context
(
self
,
hit
,
config
:
ResourceConfig
,
lang
=
''
):
"""
Parse one hit. Return it as a Record object.
"""
record
=
Record
(
dataView
=
DataView
.
hits
)
if
len
(
lang
)
<=
0
:
lang
=
config
.
search_lang_id
if
(
'
languages
'
not
in
hit
or
lang
not
in
hit
[
'
languages
'
]
or
'
text
'
not
in
hit
[
'
languages
'
][
lang
]):
return
record
content
=
fragment_fromstring
(
hit
[
'
languages
'
][
lang
][
'
text
'
],
create_parent
=
'
div
'
)
text
=
''
for
el
in
content
:
if
el
.
tag
==
'
span
'
and
'
class
'
in
el
.
attrib
and
'
sentence_meta
'
in
el
.
attrib
[
'
class
'
]:
if
el
.
tail
is
not
None
:
text
+=
el
.
tail
.
strip
(
'
\n\t
'
)
continue
if
el
.
text
is
not
None
:
if
'
class
'
in
el
.
attrib
and
re
.
search
(
'
\\
bwmatch
\\
b
'
,
el
.
attrib
[
'
class
'
])
is
not
None
:
text
+=
'
<hits:Hit>
'
+
el
.
text
+
'
</hits:Hit>
'
else
:
text
+=
el
.
text
if
el
.
tail
is
not
None
:
text
+=
el
.
tail
print
(
text
)
record
.
text
=
text
return
record
def
parse
(
self
,
response
,
config
:
ResourceConfig
,
lang
=
''
):
"""
Read a dictionary with the first N hits returned by a Tsakorpus
instance. Return a list of Record objects and the total number of
records found.
"""
nRecords
=
0
if
'
n_sentences
'
in
response
:
nRecords
=
response
[
'
n_sentences
'
]
if
nRecords
<=
0
or
'
contexts
'
not
in
response
:
return
[],
nRecords
records
=
[]
for
context
in
response
[
'
contexts
'
]:
records
.
append
(
self
.
parse_context
(
context
,
config
,
lang
))
return
records
,
nRecords
if
__name__
==
'
__main__
'
:
pass
This diff is collapsed.
Click to expand it.
main.py
+
13
−
1
View file @
7f1707db
...
...
@@ -5,6 +5,7 @@ from fastapi.encoders import jsonable_encoder
from
fastapi.responses
import
JSONResponse
from
common.query_parser
import
QueryParser
from
common.tsakorpus_query_parser
import
TsakorpusQueryParser
from
common.tsakorpus_response_parser
import
TsakorpusResponseParser
from
common.enums
import
*
from
common.diagnostics
import
Diagnostic
from
common.config
import
ResourceConfig
,
read_configs
...
...
@@ -19,6 +20,7 @@ templates = Jinja2Templates(directory='static')
app
.
qp
=
QueryParser
()
app
.
qp_tsakorpus
=
TsakorpusQueryParser
()
app
.
rp_tsakorpus
=
TsakorpusResponseParser
()
app
.
configs
=
read_configs
()
...
...
@@ -29,6 +31,7 @@ def root():
@app.get
(
'
/fcs-endpoint/{corpusID}
'
)
def
endpoint
(
request
:
Request
,
corpusID
:
str
,
operation
:
Operation
=
Operation
.
explain
,
version
:
SRUVersion
=
SRUVersion
.
v2_0
,
...
...
@@ -73,7 +76,16 @@ def endpoint(
except
Diagnostic
as
diag
:
print
(
'
diag
'
,
str
(
diag
))
return
Response
(
content
=
str
(
diag
),
media_type
=
'
application/xml
'
)
return
str
(
res
)
records
,
nHits
=
app
.
rp_tsakorpus
.
parse
(
res
,
config
)
records
=
[
r
.
as_dict
()
for
r
in
records
]
return
templates
.
TemplateResponse
(
'
search_retrieve_response.xml
'
,
{
'
request
'
:
request
,
'
n_hits
'
:
nHits
,
'
records
'
:
records
})
# media_type='application/xml')
# return str(res)
return
{
'
operation
'
:
operation
,
'
version
'
:
version
}
...
...
This diff is collapsed.
Click to expand it.
static/search_retrieve_response.xml
+
2
−
2
View file @
7f1707db
...
...
@@ -13,7 +13,7 @@
<sruResponse:recordPosition>
{{ loop.index }}
</sruResponse:recordPosition>
</sruResponse:record>
{% endfor %}
</sruResponse:records>
{% if n_hits > record
.resource
s|length %}
<sruResponse:nextRecordPosition>
{{ record
.resource
s|length + 1 }}
</sruResponse:nextRecordPosition>
{% endif %}
</sruResponse:records>
{% if n_hits > records|length %}
<sruResponse:nextRecordPosition>
{{ records|length + 1 }}
</sruResponse:nextRecordPosition>
{% endif %}
<sruResponse:resultCountPrecision>
info:srw/vocabulary/resultCountPrecision/1/exact
</sruResponse:resultCountPrecision>
</sruResponse:searchRetrieveResponse>
\ No newline at end of file
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