示例#1
0
    def annif2jskos(cls, annif_suggestion: List[dict],
                    annif_project_id: str) -> _ConceptScheme:
        """ Transform an Annif suggestion into a JSKOS concept scheme.

        :param annif_suggestion: the output of calling AnnifClient.suggest
        :param annif_project_id: Annif API project identifier
        """

        # get project details:
        annif = AnnifClient()
        project = annif.get_project(annif_project_id)
        language = project.get("language")
        name = project.get("name")

        # setup concept scheme based on project details:
        scheme = _ConceptScheme(pref_label=_LanguageMap({language: name}))

        # make concept from result and add to concept scheme:
        for result in annif_suggestion:
            label = result.get("label")
            uri = result.get("uri")
            concept = _Concept(uri=uri,
                               pref_label=_LanguageMap({language: label}))
            scheme.concepts.append(concept)

        return scheme
示例#2
0
def _get_keywords_from_annif(payload):
    annif = AnnifClient()
    try:
        results = annif.analyze(project_id='yso-en', text=payload, limit=5)
    except:
        _logger.exception('Could not connect to annif')
        raise Exception('Could not connect to annif')

    keywords = [{
        'label': row['label'],
        'score': row['score']
    } for row in results]
    for row in results:
        _logger.info(row)
    _logger.info(keywords)
    return keywords
示例#3
0
    def _set_input(self, text: str, **kwargs) -> _ConceptScheme:
        """ Use words suggested by Annif on the basis of text to set JSKOS Concept Scheme.

        :param text: input text
        :param **kwargs: required or optional Annif parameters
        """

        annif = AnnifClient()
        annif_suggestion = annif.suggest(project_id=kwargs.get("project_id"),
                                         text=text,
                                         limit=kwargs.get("limit"),
                                         threshold=kwargs.get("threshold"))

        scheme = _Utility.annif2jskos(annif_suggestion, kwargs.get("project_id"))

        return scheme
示例#4
0
from tqdm import tqdm

if not os.path.exists('skr.xml'):
    print('Downloading SKR data')
    call(
        "curl 'https://apurahat.skr.fi/myonnot/xml_apurahat?vuosi=2018&mkr=KR&ala=Tiede' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://apurahat.skr.fi/myonnot' -H 'Connection: keep-alive' -o skr.xml",
        shell=True)

with open('./skr.xml', 'r') as fd:
    data = xmltodict.parse(fd.read())

Apuraha = namedtuple(
    'Apuraha',
    ['saaja', 'otsikko', 'puhdas_otsikko', 'ala', 'rahasto', 'summa', 'annif'])

client = AnnifClient()

memory = joblib.Memory('./cache', verbose=0)


def clean_otsikko(otsikko):
    i = otsikko.find('käsittelevään')
    if i != -1:
        otsikko = otsikko[:i]

    i = otsikko.find('käsittelevän')
    if i != -1:
        otsikko = otsikko[:i]

    return otsikko.strip()
示例#5
0
from finna_client import FinnaClient
finna = FinnaClient()
results = finna.search("bicycle", limit=5)
for rec in results['records']:
    print(rec['title'])

from annif_client import AnnifClient
annif = AnnifClient()
results = annif.analyze(project_id='yso-en',
                        text="The quick brown fox",
                        limit=5)
for result in results:
    print(result)
def client():
    return AnnifClient()
def test_create_client_api_base():
    client = AnnifClient('http://localhost:5000/v1/')
    assert isinstance(client, AnnifClient)
    assert client.api_base == 'http://localhost:5000/v1/'
def test_create_client_default():
    client = AnnifClient()
    assert isinstance(client, AnnifClient)
    assert client.api_base == API_BASE