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

Fix crashing index command when targeted directory contains subject files #705

Merged
merged 4 commits into from
May 16, 2023

Conversation

juhoinkinen
Copy link
Member

@juhoinkinen juhoinkinen commented May 15, 2023

The annif index command crashes when the targeted directory contains subject files (files with either *.tsv or *.key extensions):

annif index dummy-fi tests/corpora/archaeology/fulltext/
Traceback (most recent call last):
...
  File "/home/local/jmminkin/git/Annif/annif/corpus/subject.py", line 248, in from_string
    subject_ids.add(subject_index.by_uri(uri))
AttributeError: 'NoneType' object has no attribute 'by_uri'

The bug is due to this change done in PR #663.

I made the change because it seemed unnecessary and confusing to pass the subject index and language to DocumentDirectory when it is used only for obtaining documents to give suggestions to.

Reverting the change, i.e. passing subject index and language to DocumentDirectory is the simplest way to fix the bug, and on second thought maybe it also is better conceptually: corpora should have subject index and language defined, even if they are not used.

Also renames vars to better correspond their origin/usage: keyfile(name) -> subjfile(name).

@juhoinkinen juhoinkinen added this to the 1.0 milestone May 15, 2023
@codecov
Copy link

codecov bot commented May 15, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (2d1d3ce) 99.66% compared to head (b602ea2) 99.66%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #705   +/-   ##
=======================================
  Coverage   99.66%   99.66%           
=======================================
  Files          89       89           
  Lines        6288     6295    +7     
=======================================
+ Hits         6267     6274    +7     
  Misses         21       21           
Impacted Files Coverage Δ
annif/cli.py 100.00% <100.00%> (ø)
annif/corpus/document.py 100.00% <100.00%> (ø)
tests/conftest.py 100.00% <100.00%> (ø)
tests/test_backend.py 100.00% <100.00%> (ø)
tests/test_cli.py 100.00% <100.00%> (ø)
tests/test_corpus.py 100.00% <100.00%> (ø)
tests/test_project.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@juhoinkinen juhoinkinen marked this pull request as ready for review May 15, 2023 11:29
@juhoinkinen juhoinkinen requested a review from osma May 15, 2023 11:29
Copy link
Member

@osma osma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the problem for sure and the variable renamings are sensible.

However, I think another approach would also be possible. DocumentDirectory already takes a require_subjects flag, and when set to False, the subjects are not required, but they are still parsed from tsv/key files and returned if such files exist. This is wasted effort in the case of the annif index operation, because the subjects aren't going to be used anyway. So another solution would be to modify DocumentDirectory so it doesn't even check for tsv/key files if require_subjects==False. Then it would also be unnecessary to pass the subject index and language to the DocumentDirectory. If the default value of these parameters were set to None, it could be constructed like this: DocumentDirectory(directory, require_subjects=False). I think that would be even more elegant than the solution in this PR. However, I'm happy to accept either solution.

@sonarcloud
Copy link

sonarcloud bot commented May 16, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
18.2% 18.2% Duplication

Copy link
Member

@osma osma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@juhoinkinen juhoinkinen merged commit 6f13121 into main May 16, 2023
@juhoinkinen juhoinkinen deleted the fix-failing-index-command branch May 16, 2023 12:04
@san-uh
Copy link

san-uh commented Jun 19, 2023

Dear Annif colleagues, thank you for already working on this problem. We in the DNB have recently started using Annif 0.61.0 and have now noticed that we cannot currently use an "index" command with this version, so we have to switch back to 0.60.0. Just one question: Is a bugfix release 0.61.1 planned or will the solution come with 1.0? Thanks and greetings, Sandro

@juhoinkinen
Copy link
Member Author

Hi @san-uh,

This bug realized only when the targeted directory contains files with either *.tsv or *.key extension (as far as I'm aware). Does index command work for you if you remove these files from the targeted directory (if there are them)?

@san-uh
Copy link

san-uh commented Jun 19, 2023

Hello @juhoinkinen,
thank you very much for the explanation. I deleted the *.tsv files that were previously in the directory and the index command now works. So with this workaround annif 0.61.0 including index is usable. Thanks for the feedback! Sandro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants