aboutsummaryrefslogtreecommitdiffstats
path: root/fuzzycat
diff options
context:
space:
mode:
authorMartin Czygan <martin@archive.org>2021-09-21 18:34:31 +0000
committerMartin Czygan <martin@archive.org>2021-09-21 18:34:31 +0000
commita37404f30b2c1afa0b46ee30a5b59d7312c119d0 (patch)
tree8f38d396909049971559a5ab574561960d6d5f22 /fuzzycat
parentc587a084defe54103aa147b7ab91542a11a548b1 (diff)
parent5fa61d89320af880d5bf6b3231f6478887cfb6a6 (diff)
downloadfuzzycat-a37404f30b2c1afa0b46ee30a5b59d7312c119d0.tar.gz
fuzzycat-a37404f30b2c1afa0b46ee30a5b59d7312c119d0.zip
Merge branch 'wip-martin-review-cleanup' into 'master'
review notes and some cleanup See merge request webgroup/fuzzycat!7
Diffstat (limited to 'fuzzycat')
-rw-r--r--fuzzycat/config.py1
-rw-r--r--fuzzycat/matching.py77
-rw-r--r--fuzzycat/simple.py2
-rw-r--r--fuzzycat/utils.py2
-rw-r--r--fuzzycat/verify.py9
5 files changed, 82 insertions, 9 deletions
diff --git a/fuzzycat/config.py b/fuzzycat/config.py
index 61050d1..d6989cf 100644
--- a/fuzzycat/config.py
+++ b/fuzzycat/config.py
@@ -1,4 +1,3 @@
-
from dynaconf import Dynaconf
settings = Dynaconf(envvar_prefix="FUZZYCAT")
diff --git a/fuzzycat/matching.py b/fuzzycat/matching.py
index c94a308..310dfc2 100644
--- a/fuzzycat/matching.py
+++ b/fuzzycat/matching.py
@@ -10,9 +10,9 @@ import requests
from fatcat_openapi_client import ContainerEntity, DefaultApi, ReleaseEntity
from fatcat_openapi_client.rest import ApiException
+from fuzzycat.config import settings
from fuzzycat.entities import entity_from_dict, entity_from_json
from fuzzycat.utils import es_compat_hits_total
-from fuzzycat.config import settings
FATCAT_API_URL = settings.get("FATCAT_API_URL", "https://api.fatcat.wiki/v0")
@@ -73,12 +73,83 @@ def match_release_fuzzy(
if r:
return [r]
+
+ if release.title is not None and release.contribs is not None:
+ names = " ".join([c.raw_name for c in release.contribs])
+ body = {
+ "track_total_hits": True,
+ "query": {
+ "bool": {
+ "must": [
+ {
+ "match": {
+ "title": {
+ "query": release.title,
+ "operator": "AND",
+ "fuzziness": "AUTO",
+ },
+ }
+ },
+ {
+ "match": {
+ "contrib_names": {
+ "query": names,
+ "operator": "AND",
+ "fuzziness": "AUTO",
+ }
+ }
+ },
+ ],
+ },
+ },
+ "size": size,
+ }
+ resp = es.search(body=body, index="fatcat_release")
+ if es_compat_hits_total(resp) > 0:
+ return response_to_entity_list(resp, entity_type=ReleaseEntity, size=size, api=api)
+
+ body = {
+ "track_total_hits": True,
+ "query": {
+ "bool": {
+ "should": [
+ {
+ "match": {
+ "title": {
+ "query": release.title,
+ "operator": "AND",
+ "fuzziness": "AUTO",
+ },
+ }
+ },
+ {
+ "match": {
+ "contrib_names": {
+ "query": names,
+ "operator": "AND",
+ "fuzziness": "AUTO",
+ }
+ }
+ },
+ ],
+ },
+ },
+ "size": size,
+ }
+ resp = es.search(body=body, index="fatcat_release")
+ if es_compat_hits_total(resp) > 0:
+ return response_to_entity_list(resp, entity_type=ReleaseEntity, size=size, api=api)
+
+ # Note: If the title is short, we will get lots of results here; do we need
+ # to check for title length or result set length length or result set
+ # length here?
body = {
+ "track_total_hits": True,
"query": {
"match": {
"title": {
"query": release.title,
- "operator": "AND"
+ "operator": "AND",
}
}
},
@@ -91,6 +162,7 @@ def match_release_fuzzy(
# Get fuzzy.
# https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
body = {
+ "track_total_hits": True,
"query": {
"match": {
"title": {
@@ -106,6 +178,7 @@ def match_release_fuzzy(
if es_compat_hits_total(resp) > 0:
return response_to_entity_list(resp, entity_type=ReleaseEntity, size=size, api=api)
+
# TODO: perform more queries on other fields.
return []
diff --git a/fuzzycat/simple.py b/fuzzycat/simple.py
index 8b206b1..ff59ba2 100644
--- a/fuzzycat/simple.py
+++ b/fuzzycat/simple.py
@@ -25,8 +25,8 @@ from fuzzycat.common import Reason, Status
from fuzzycat.entities import entity_to_dict
from fuzzycat.grobid_unstructured import grobid_parse_unstructured
from fuzzycat.matching import match_release_fuzzy
-from fuzzycat.verify import verify
from fuzzycat.utils import clean_doi
+from fuzzycat.verify import verify
@dataclass
diff --git a/fuzzycat/utils.py b/fuzzycat/utils.py
index a1c5124..303daf6 100644
--- a/fuzzycat/utils.py
+++ b/fuzzycat/utils.py
@@ -81,6 +81,7 @@ def dict_key_exists(doc, path):
else:
return True
+
def clean_doi(raw: Optional[str]) -> Optional[str]:
if not raw:
return None
@@ -95,6 +96,7 @@ def clean_doi(raw: Optional[str]) -> Optional[str]:
raw = raw[:8] + raw[9:]
return raw
+
def doi_prefix(v):
"""
Return the prefix of a DOI.
diff --git a/fuzzycat/verify.py b/fuzzycat/verify.py
index 1eeea40..5b90c47 100644
--- a/fuzzycat/verify.py
+++ b/fuzzycat/verify.py
@@ -90,9 +90,9 @@ from fuzzycat.common import Reason, Status
from fuzzycat.data import (CONTAINER_NAME_BLACKLIST, PUBLISHER_BLACKLIST, TITLE_BLACKLIST,
TITLE_FRAGMENT_BLACKLIST)
from fuzzycat.entities import entity_to_dict
-from fuzzycat.utils import (author_similarity_score, contains_chemical_formula, dict_key_exists,
- doi_prefix, has_doi_prefix, jaccard, num_project, parse_page_string,
- slugify_string, clean_doi)
+from fuzzycat.utils import (author_similarity_score, clean_doi, contains_chemical_formula,
+ dict_key_exists, doi_prefix, has_doi_prefix, jaccard, num_project,
+ parse_page_string, slugify_string)
Verify = collections.namedtuple("Verify", "status reason")
@@ -597,8 +597,7 @@ def verify(a: Dict, b: Dict, min_title_length=5) -> Tuple[str, str]:
try:
a_parsed_pages = parse_page_string(glom(a, "pages"))
b_parsed_pages = parse_page_string(glom(b, "pages"))
- if (a_parsed_pages.count != None
- and b_parsed_pages.count != None
+ if (a_parsed_pages.count != None and b_parsed_pages.count != None
and abs(a_parsed_pages.count - b_parsed_pages.count) > 5):
return Verify(Status.DIFFERENT, Reason.PAGE_COUNT)
except (ValueError, PathAccessError):