• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rero / sonar / 17425918180

03 Sep 2025 07:11AM UTC coverage: 95.796% (-0.6%) from 96.378%
17425918180

push

github

PascalRepond
translations: extract messages

Co-Authored-by: Pascal Repond <pascal.repond@rero.ch>

7816 of 8159 relevant lines covered (95.8%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.29
/sonar/affiliations/affiliation_resolver.py
1
# Swiss Open Access Repository
2
# Copyright (C) 2021 RERO
3
#
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU Affero General Public License as published by
6
# the Free Software Foundation, version 3 of the License.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
12
#
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

16
"""Affiliations resolver."""
17

18
import csv
1✔
19

20
from fuzzywuzzy import fuzz
1✔
21
from werkzeug.utils import cached_property
1✔
22

23
CSV_FILE = "./data/affiliations.csv"
1✔
24

25

26
class AffiliationResolver:
1✔
27
    """Affiliation resolver."""
28

29
    @cached_property
1✔
30
    def affiliations(self):
1✔
31
        """List of affiliations retrieved from a dedicated file.
32

33
        :returns: List of affliations
34
        """
35
        affiliations = []
1✔
36

37
        with open(CSV_FILE) as file:
1✔
38
            reader = csv.reader(file, delimiter="\t")
1✔
39
            for row in reader:
1✔
40
                affiliation = []
1✔
41
                for index, value in enumerate(row):
1✔
42
                    if index > 0 and value:
1✔
43
                        affiliation.append(value)
1✔
44

45
                if affiliation:
1✔
46
                    affiliations.append(affiliation)
1✔
47

48
        return affiliations
1✔
49

50
    def resolve(self, searched_affiliation):
1✔
51
        """Resolve affiliations from given parameter.
52

53
        :param searched_affiliation: Affiliation to match.
54
        :returns: String of matching affiliation.
55
        """
56
        if not searched_affiliation:
1✔
57
            return None
1✔
58

59
        collected_affiliations = []
1✔
60
        for affiliations in self.affiliations:
1✔
61
            # the first string in the row is the standard form, to be stored
62
            standard_form = affiliations[0]
1✔
63
            for affiliation in affiliations:
1✔
64
                score = fuzz.partial_ratio(searched_affiliation, affiliation)
1✔
65
                if score > 92 and standard_form not in collected_affiliations:
1✔
66
                    # handle special case UZH / ZHdK
67
                    # TODO: solve this special case by converting the CSV file to JSON
68
                    # using rejected forms https://github.com/rero/sonar/issues/824
69
                    if (
1✔
70
                        affiliation.lower() == "zurich university"
71
                        and "zurich university of the arts" in searched_affiliation.lower()
72
                    ):
73
                        continue
×
74
                    # handle special case CERN/Lucerne
75
                    if affiliation.lower() == "cern" and "lucerne" in searched_affiliation.lower():
1✔
76
                        continue
×
77
                    # handle special case Freiburg im Breisgau
78
                    if "university of freiburg" in searched_affiliation.lower():
1✔
79
                        break
1✔
80

81
                    collected_affiliations.append(standard_form)
1✔
82
        return collected_affiliations
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc