Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzedir command #95

Merged
merged 5 commits into from
Apr 6, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial implementation of analyzedir command. Fixes #94
  • Loading branch information
osma committed Apr 6, 2018
commit c481c8a7bb41a7027b7a55137302c690b2b5ba01
46 changes: 46 additions & 0 deletions annif/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import collections
import logging
import os.path
import re
import sys
import click
import click_log
Expand Down Expand Up @@ -151,6 +153,50 @@ def run_analyze(project_id, limit, threshold, backend_param):
click.echo("{}\t<{}>\t{}".format(hit.score, hit.uri, hit.label))


@cli.command('analyzedir')
@click_log.simple_verbosity_option(logger)
@click.argument('project_id')
@click.argument('directory')
@click.option('--suffix', default='.annif')
@click.option('--force', default=False)
@click.option('--limit', default=10)
@click.option('--threshold', default=0.0)
@click.option('--backend-param', '-b', multiple=True)
def run_analyzedir(
project_id,
directory,
suffix,
force,
limit,
threshold,
backend_param):
""""
Analyze a directory with documents. Write the results in TSV files
with the given suffix.

USAGE: annif analyzedir <project_id> <directory> [--suffix=SUFFIX]
[--force=FORCE] [--limit=N] [--threshold=N]
"""
project = get_project(project_id)
backend_params = parse_backend_params(backend_param)
hit_filter = HitFilter(limit, threshold)

for docfilename, dummy_subjectfn in annif.corpus.DocumentDirectory(
directory, require_subjects=False):
with open(docfilename) as docfile:
text = docfile.read()
subjectfilename = re.sub(r'\.txt$', suffix, docfilename)
if os.path.exists(subjectfilename) and not force:
click.echo(
"Not overwriting {} (use --force to override)".format(
subjectfilename))
continue
with open(subjectfilename, 'w') as subjfile:
for hit in hit_filter(project.analyze(text, backend_params)):
line = "<{}>\t{}\t{}".format(hit.uri, hit.label, hit.score)
click.echo(line, file=subjfile)


@cli.command('eval')
@click_log.simple_verbosity_option(logger)
@click.argument('project_id')
Expand Down