hyperboria/nexus/meta_api/query_extensionner/checks.py
the-superpirate 6b69d092fa - fix(nexus): Fix DOI detection in messages
GitOrigin-RevId: e4d4519221319c26134d8cbbd011eca2def2b6b3
2021-05-02 11:00:14 +03:00

62 lines
1.3 KiB
Python

import re
from enum import Enum
from nexus.nlptools.regex import (
DOI_REGEX,
ISBN_REGEX,
NID_REGEX,
ONLY_DOI_REGEX,
URL_REGEX,
)
# ToDo: redo all, code is logically incorrect now
class QueryClass(Enum):
Default = 'default'
DOI = 'doi'
ISBN = 'isbn'
NID = 'nid'
URL = 'url'
def check_doi(query) -> (QueryClass, str):
if (
((r := re.search(DOI_REGEX, query)) and re.search(URL_REGEX, query))
or re.search(ONLY_DOI_REGEX, query)
):
doi = (r[1] + '/' + r[2]).lower()
return {
'doi': doi,
'query': f'doi:"{doi}"',
'class': QueryClass.DOI,
}
def check_isbn(query: str) -> (QueryClass, str):
if r := re.search(ISBN_REGEX, query):
isbn = r[1].replace('-', '')
return {
'isbn': isbn,
'query': 'isbns:' + isbn,
'class': QueryClass.ISBN
}
def check_nid(query: str) -> (QueryClass, str):
if r := re.search(NID_REGEX, query):
return {
'id': r[1],
'query': 'id:' + r[1],
'class': QueryClass.NID,
}
def check_url(query: str) -> (QueryClass, str):
if r := re.search(URL_REGEX, query):
return {
'url': r[0],
'query': r[0],
'class': QueryClass.URL,
}