Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#553] Add language to TextAnalysisRecord.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Nov 20, 2024
1 parent 54a1d35 commit bf2c16d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
22 changes: 19 additions & 3 deletions src/main/java/cz/cvut/kbss/termit/model/TextAnalysisRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
*/
package cz.cvut.kbss.termit.model;

import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints;
import cz.cvut.kbss.jopa.vocabulary.DC;
import cz.cvut.kbss.termit.model.resource.Resource;
import cz.cvut.kbss.termit.util.Vocabulary;

Expand All @@ -44,12 +46,16 @@ public class TextAnalysisRecord extends AbstractEntity {
@OWLObjectProperty(iri = Vocabulary.s_p_ma_slovnik_pro_analyzu)
private Set<URI> vocabularies;

@OWLAnnotationProperty(iri = DC.Terms.LANGUAGE, simpleLiteral = true)
private String language;

public TextAnalysisRecord() {
}

public TextAnalysisRecord(Instant date, Resource analyzedResource) {
public TextAnalysisRecord(Instant date, Resource analyzedResource, String language) {
this.date = date;
this.analyzedResource = analyzedResource;
this.language = language;
}

public Instant getDate() {
Expand All @@ -76,6 +82,14 @@ public void setVocabularies(Set<URI> vocabularies) {
this.vocabularies = vocabularies;
}

public String getLanguage() {
return language;
}

public void setLanguage(String language) {
this.language = language;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -86,12 +100,13 @@ public boolean equals(Object o) {
}
return Objects.equals(date, that.date) &&
Objects.equals(analyzedResource, that.analyzedResource) &&
Objects.equals(vocabularies, that.vocabularies);
Objects.equals(vocabularies, that.vocabularies) &&
Objects.equals(language, that.language);
}

@Override
public int hashCode() {
return Objects.hash(date, analyzedResource, vocabularies);
return Objects.hash(date, analyzedResource, vocabularies, language);
}

@Override
Expand All @@ -100,6 +115,7 @@ public String toString() {
"date=" + date +
",analyzedResource=" + analyzedResource +
",vocabularies=" + vocabularies +
", language=" + language +
"}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void storeTextAnalysisRecord(File file, TextAnalysisInput config) {
LOG.trace("Creating record of text analysis event for file {}.", file);
assert config.getVocabularyContexts() != null;

final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file);
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file, config.getLanguage());
record.setVocabularies(new HashSet<>(config.getVocabularyContexts()));
recordDao.persist(record);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ void setUp() {
@Test
void findLatestGetsLatestTextAnalysisRecordForResource() {
final URI vocabulary = Generator.generateUri();
final TextAnalysisRecord old = new TextAnalysisRecord(Instant.ofEpochMilli(System.currentTimeMillis() - 10000), resource);
final TextAnalysisRecord old = new TextAnalysisRecord(Instant.ofEpochMilli(System.currentTimeMillis() - 10000),
resource, Environment.LANGUAGE);
old.setVocabularies(Collections.singleton(vocabulary));
final TextAnalysisRecord latest = new TextAnalysisRecord(Utils.timestamp(), resource);
final TextAnalysisRecord latest = new TextAnalysisRecord(Utils.timestamp(), resource, Environment.LANGUAGE);
latest.setVocabularies(Collections.singleton(vocabulary));
transactional(() -> {
sut.persist(old);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ void getLatestTextAnalysisRecordRetrievesAnalysisRecordFromService() throws Exce
final File file = generateFile();
when(identifierResolverMock.resolveIdentifier(RESOURCE_NAMESPACE, FILE_NAME)).thenReturn(file.getUri());
when(resourceServiceMock.findRequired(file.getUri())).thenReturn(file);
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file);
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file, Environment.LANGUAGE);
record.setVocabularies(Collections.singleton(Generator.generateUri()));
when(resourceServiceMock.findLatestTextAnalysisRecord(file)).thenReturn(record);
final MvcResult mvcResult = mockMvc.perform(get(PATH + "/" + FILE_NAME + "/text-analysis/records/latest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.ResourceRepositoryService;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import cz.cvut.kbss.termit.util.Utils;
Expand Down Expand Up @@ -388,7 +387,7 @@ void removeFileThrowsTermItExceptionWhenFileIsNotLinkedToDocument() {
@Test
void findLatestTextAnalysisRecordRetrievesLatestTextAnalysisRecordForResource() {
final File file = Generator.generateFileWithId("test.html");
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file);
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file, Environment.LANGUAGE);
when(textAnalysisService.findLatestAnalysisRecord(file)).thenReturn(Optional.of(record));

final TextAnalysisRecord result = sut.findLatestTextAnalysisRecord(file);
Expand Down Expand Up @@ -530,7 +529,7 @@ void getContentWithoutUnconfirmedOccurrencesRemovesUnconfirmedOccurrencesFromFil

@Test
void addFileToDocumentSetsFileLanguageToDefaultConfiguredWhenNotProvided() {
config.getPersistence().setLanguage(Constants.DEFAULT_LANGUAGE);
config.getPersistence().setLanguage(Environment.LANGUAGE);
final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final Document document = Generator.generateDocumentWithId();
document.setVocabulary(vocabulary.getUri());
Expand All @@ -546,7 +545,7 @@ void addFileToDocumentSetsFileLanguageToDefaultConfiguredWhenNotProvided() {

@Test
void addFileToDocumentDoesNotModifyLanguageWhenItIsAlreadySet() {
config.getPersistence().setLanguage(Constants.DEFAULT_LANGUAGE);
config.getPersistence().setLanguage(Environment.LANGUAGE);
final Vocabulary vocabulary = Generator.generateVocabularyWithId();
final Document document = Generator.generateDocumentWithId();
document.setVocabulary(vocabulary.getUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import cz.cvut.kbss.termit.rest.handler.ErrorInfo;
import cz.cvut.kbss.termit.service.BaseServiceTestRunner;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Utils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -326,6 +325,7 @@ void analyzeFileBacksUpFileContentBeforeSavingNewAnalyzedContent() throws Except

@Test
void analyzeFileCreatesTextAnalysisRecord() {
file.setLanguage("cs");
mockServer.expect(requestTo(config.getTextAnalysis().getUrl()))
.andExpect(method(HttpMethod.POST)).andExpect(content().string(containsString(CONTENT)))
.andRespond(withSuccess(CONTENT, MediaType.APPLICATION_XML));
Expand All @@ -334,11 +334,12 @@ void analyzeFileCreatesTextAnalysisRecord() {
verify(textAnalysisRecordDao).persist(captor.capture());
assertEquals(file, captor.getValue().getAnalyzedResource());
assertEquals(Collections.singleton(vocabulary.getUri()), captor.getValue().getVocabularies());
assertEquals(file.getLanguage(), captor.getValue().getLanguage());
}

@Test
void findLatestAnalysisRecordFindsLatestTextAnalysisRecordForResource() {
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file);
final TextAnalysisRecord record = new TextAnalysisRecord(Utils.timestamp(), file, Environment.LANGUAGE);
record.setVocabularies(Collections.singleton(vocabulary.getUri()));
when(textAnalysisRecordDao.findLatest(file)).thenReturn(Optional.of(record));

Expand Down Expand Up @@ -518,7 +519,7 @@ void supportsLanguageGetsListOfSupportedLanguagesFromTextAnalysisServiceAndCheck
@Test
void supportsLanguageReturnsTrueWhenTextAnalysisServiceLanguagesEndpointUrlIsNotConfigured() {
String endpointUrl = config.getTextAnalysis().getLanguagesUrl();
file.setLanguage(Constants.DEFAULT_LANGUAGE);
file.setLanguage(Environment.LANGUAGE);
config.getTextAnalysis().setLanguagesUrl(null);
assertTrue(sut.supportsLanguage(file));
// Reset configuration state
Expand Down

0 comments on commit bf2c16d

Please sign in to comment.