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

add polarity, type and topic DTOs and service calls #36

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 0 additions & 4 deletions src/main/java/upc/edu/gessi/repo/AppGraphRepoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class AppGraphRepoApplication {

private Logger logger = LoggerFactory.getLogger(AppGraphRepoApplication.class);

public static void main(String[] args) {
SpringApplication.run(AppGraphRepoApplication.class, args);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/upc/edu/gessi/repo/dto/Review/PolarityDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package upc.edu.gessi.repo.dto.Review;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import upc.edu.gessi.repo.dto.LanguageModel.LanguageModelDTO;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PolarityDTO implements Serializable {
private String polarity;
private LanguageModelDTO languageModel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.Serializable;
import java.util.List;
import java.util.Set;

@Data
@NoArgsConstructor
Expand All @@ -23,4 +24,10 @@ public class ReviewFeatureResponseDTO implements Serializable {
private String reviewText;

private List<FeatureDTO> featureDTOs;

private List<PolarityDTO> polarityDTOs;

private List<TypeDTO> typeDTOs;

private List<TopicDTO> topicDTOs;
}
4 changes: 3 additions & 1 deletion src/main/java/upc/edu/gessi/repo/dto/Review/SentenceDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import upc.edu.gessi.repo.dto.LanguageModel.LanguageModelDTO;

import java.io.Serializable;

Expand All @@ -19,6 +18,9 @@ public class SentenceDTO implements Serializable {
private String id;
private SentimentDTO sentimentData;
private FeatureDTO featureData;
private PolarityDTO polarityData;
private TopicDTO topicData;
private TypeDTO typeData;
private String text;

}
19 changes: 19 additions & 0 deletions src/main/java/upc/edu/gessi/repo/dto/Review/TopicDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package upc.edu.gessi.repo.dto.Review;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import upc.edu.gessi.repo.dto.LanguageModel.LanguageModelDTO;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TopicDTO implements Serializable {
private String topic;
private LanguageModelDTO languageModel;
}
19 changes: 19 additions & 0 deletions src/main/java/upc/edu/gessi/repo/dto/Review/TypeDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package upc.edu.gessi.repo.dto.Review;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import upc.edu.gessi.repo.dto.LanguageModel.LanguageModelDTO;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TypeDTO implements Serializable {
private String type;
private LanguageModelDTO languageModel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public void addReviewToMobileApplication(String packageName, String reviewId) {
List<Statement> statements = new ArrayList<>();
IRI applicationIRI = factory.createIRI(schemaIRI.getAppIRI() + "/" + packageName);
IRI reviewIRI = factory.createIRI(schemaIRI.getReviewIRI() + "/" + reviewId);
statements.add(factory.createStatement(applicationIRI, schemaIRI.getHasPartIRI(), reviewIRI));
statements.add(factory.createStatement(applicationIRI, schemaIRI.getReviewsIRI(), reviewIRI));
commitChanges(statements);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,77 @@ public List<ReviewFeatureResponseDTO> findAllByAppIdAndFeatures(final String app
if (!reviewsResult.hasNext()) {
throw new NoReviewsFoundException("No review was found");
}
List<ReviewFeatureResponseDTO> reviewDTOs = new ArrayList<>();

// Use a map to group results by review ID
Map<String, ReviewFeatureResponseDTO> reviewMap = new HashMap<>();

while (reviewsResult.hasNext()) {
ReviewFeatureResponseDTO reviewFeatureResponseDTO = getReviewFeatureDTO(reviewsResult.next());
reviewDTOs.add(reviewFeatureResponseDTO);
BindingSet bindings = reviewsResult.next();
String reviewId = bindings.getBinding("id").getValue().stringValue();
System.out.println(bindings.toString());

// Get or create ReviewFeatureResponseDTO
ReviewFeatureResponseDTO dto = reviewMap.computeIfAbsent(reviewId, k -> {
ReviewFeatureResponseDTO newDto = new ReviewFeatureResponseDTO();
newDto.setId(reviewId);
if (bindings.getBinding("text") != null) {
newDto.setReviewText(bindings.getBinding("text").getValue().stringValue());
}
newDto.setFeatureDTOs(new ArrayList<>());
newDto.setPolarityDTOs(new ArrayList<>());
newDto.setTypeDTOs(new ArrayList<>());
newDto.setTopicDTOs(new ArrayList<>());
return newDto;
});

// Add feature if present and not empty
if (bindings.getBinding("feature") != null) {
String featureValue = bindings.getBinding("feature").getValue().stringValue();
if (!featureValue.isEmpty()) {
FeatureDTO featureDTO = new FeatureDTO();
featureDTO.setFeature(featureValue);
if (bindings.getBinding("model") != null) {
String modelValue = bindings.getBinding("model").getValue().stringValue();
if (!modelValue.isEmpty()) {
featureDTO.setLanguageModel(new LanguageModelDTO(modelValue));
}
}
dto.getFeatureDTOs().add(featureDTO);
}
}

// Add polarity if present and not empty
if (bindings.getBinding("polarityId") != null) {
String polarityValue = bindings.getBinding("polarityId").getValue().stringValue();
if (!polarityValue.isEmpty()) {
PolarityDTO polarityDTO = new PolarityDTO();
polarityDTO.setPolarity(polarityValue);
dto.getPolarityDTOs().add(polarityDTO);
}
}

// Add type if present and not empty
if (bindings.getBinding("typeId") != null) {
String typeValue = bindings.getBinding("typeId").getValue().stringValue();
if (!typeValue.isEmpty()) {
TypeDTO typeDTO = new TypeDTO();
typeDTO.setType(typeValue);
dto.getTypeDTOs().add(typeDTO);
}
}

// Add topic if present and not empty
if (bindings.getBinding("topicId") != null) {
String topicValue = bindings.getBinding("topicId").getValue().stringValue();
if (!topicValue.isEmpty()) {
TopicDTO topicDTO = new TopicDTO();
topicDTO.setTopic(topicValue);
dto.getTopicDTOs().add(topicDTO);
}
}
}
return reviewDTOs;

return new ArrayList<>(reviewMap.values());
}


Expand Down Expand Up @@ -409,6 +474,9 @@ private ReviewFeatureResponseDTO getReviewFeatureDTO(final BindingSet bindings)
}

FeatureDTO featureDTO = new FeatureDTO();
PolarityDTO polarityDTO = new PolarityDTO();
TypeDTO typeDTO = new TypeDTO();
TopicDTO topicDTO = new TopicDTO();
if (bindings.getBinding("feature") != null && bindings.getBinding("feature").getValue() != null) {
String feature = bindings.getBinding("feature").getValue().stringValue();
featureDTO.setFeature(feature);
Expand All @@ -417,8 +485,23 @@ private ReviewFeatureResponseDTO getReviewFeatureDTO(final BindingSet bindings)
String model = bindings.getBinding("model").getValue().stringValue();
featureDTO.setLanguageModel(new LanguageModelDTO(model));
}
reviewFeatureResponseDTO.setFeatureDTOs(Collections.singletonList(featureDTO));
if (bindings.getBinding("polarity") != null && bindings.getBinding("polarity").getValue() != null) {
String polarity = bindings.getBinding("polarity").getValue().stringValue();
polarityDTO.setPolarity(polarity);
}
if (bindings.getBinding("type") != null && bindings.getBinding("type").getValue() != null) {
String type = bindings.getBinding("type").getValue().stringValue();
typeDTO.setType(type);
}
if (bindings.getBinding("topic") != null && bindings.getBinding("topic").getValue() != null) {
String topic = bindings.getBinding("topic").getValue().stringValue();
topicDTO.setTopic(topic);
}

reviewFeatureResponseDTO.setFeatureDTOs(Collections.singletonList(featureDTO));
reviewFeatureResponseDTO.setPolarityDTOs(Collections.singletonList(polarityDTO));
reviewFeatureResponseDTO.setTypeDTOs(Collections.singletonList(typeDTO));
reviewFeatureResponseDTO.setTopicDTOs(Collections.singletonList(topicDTO));

return reviewFeatureResponseDTO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public IRI insert(SentenceDTO dto) {
if (dto.getFeatureData() != null && dto.getFeatureData().getFeature() != null) {
addSenteceFeatureIntoStatements(statements, dto, sentenceIRI);
}
if (dto.getPolarityData() != null && dto.getPolarityData().getPolarity() != null) {
addSentencePolarityIntoStatements(statements, dto, sentenceIRI);
}
if (dto.getTopicData() != null && dto.getTopicData().getTopic() != null) {
addSentenceTopicIntoStatements(statements, dto, sentenceIRI);
}
if (dto.getTypeData() != null && dto.getTypeData().getType() != null) {
addSentenceTypeIntoStatements(statements, dto, sentenceIRI);
}
commitChanges(statements);
return sentenceIRI;
}
Expand Down Expand Up @@ -108,6 +117,30 @@ private void addSenteceFeatureIntoStatements(final List<Statement> statements,

}

private void addSentencePolarityIntoStatements(final List<Statement> statements,
final SentenceDTO sentenceDTO,
final IRI sentenceIRI) {
//String reviewId = "";
//deletePolarityIfExists(reviewId);
addPolarityIntoStatements(statements, sentenceDTO, sentenceIRI);
}

private void addSentenceTopicIntoStatements(final List<Statement> statements,
final SentenceDTO sentenceDTO,
final IRI sentenceIRI) {
//String reviewId = "";
//deleteTopicIfExists(reviewId);
addTopicIntoStatements(statements, sentenceDTO, sentenceIRI);
}

private void addSentenceTypeIntoStatements(final List<Statement> statements,
final SentenceDTO sentenceDTO,
final IRI sentenceIRI) {
//String reviewId = "";
//deleteTypeIfExists(reviewId);
addTypeIntoStatements(statements, sentenceDTO, sentenceIRI);
}

private void deleteSentimentIfExisting(String reviewId) {
TupleQueryResult result = Utils.runSparqlSelectQuery(repository.getConnection(), reviewQueryBuilder.hasReviewSentiments(reviewId));
if (result.hasNext()) {
Expand Down Expand Up @@ -170,6 +203,57 @@ private void addFeatureIntoStatements(final List<Statement> statements,
}
}

private void addPolarityIntoStatements(final List<Statement> statements,
final SentenceDTO sentenceDTO,
final IRI sentenceIRI) {
String polarity = sentenceDTO.getPolarityData().getPolarity();
IRI polarityIRI = factory.createIRI(schemaIRI.getPolarityIRI() + "/" + polarity);
statements.add(factory.createStatement(polarityIRI, schemaIRI.getTypeIRI(), schemaIRI.getPolarityIRI()));
statements.add(factory.createStatement(sentenceIRI, schemaIRI.getPolarityPropertyIRI(), polarityIRI));
statements.add(factory.createStatement(polarityIRI, schemaIRI.getIdentifierIRI(), factory.createLiteral(polarity)));
statements.add(factory.createStatement(polarityIRI, schemaIRI.getNameIRI(), factory.createLiteral(polarity)));
if (sentenceDTO.getPolarityData().getLanguageModel() != null) {
addLanguageModel(
statements,
sentenceDTO.getPolarityData().getLanguageModel().getModelName(),
polarityIRI);
}
}

private void addTopicIntoStatements(final List<Statement> statements,
final SentenceDTO sentenceDTO,
final IRI sentenceIRI) {
String topic = sentenceDTO.getTopicData().getTopic();
IRI topicIRI = factory.createIRI(schemaIRI.getTopicIRI() + "/" + topic);
statements.add(factory.createStatement(topicIRI, schemaIRI.getTypeIRI(), schemaIRI.getTopicIRI()));
statements.add(factory.createStatement(sentenceIRI, schemaIRI.getTopicPropertyIRI(), topicIRI));
statements.add(factory.createStatement(topicIRI, schemaIRI.getIdentifierIRI(), factory.createLiteral(topic)));
statements.add(factory.createStatement(topicIRI, schemaIRI.getNameIRI(), factory.createLiteral(topic)));
if (sentenceDTO.getTopicData().getLanguageModel() != null) {
addLanguageModel(
statements,
sentenceDTO.getTopicData().getLanguageModel().getModelName(),
topicIRI);
}
}

private void addTypeIntoStatements(final List<Statement> statements,
final SentenceDTO sentenceDTO,
final IRI sentenceIRI) {
String type = sentenceDTO.getTypeData().getType();
IRI typeIRI = factory.createIRI(schemaIRI.getReviewTypeIRI() + "/" + type);
statements.add(factory.createStatement(typeIRI, schemaIRI.getTypeIRI(), schemaIRI.getReviewTypeIRI()));
statements.add(factory.createStatement(sentenceIRI, schemaIRI.getTypePropertyIRI(), typeIRI));
statements.add(factory.createStatement(typeIRI, schemaIRI.getIdentifierIRI(), factory.createLiteral(type)));
statements.add(factory.createStatement(typeIRI, schemaIRI.getNameIRI(), factory.createLiteral(type)));
if (sentenceDTO.getTypeData().getLanguageModel() != null) {
addLanguageModel(
statements,
sentenceDTO.getTypeData().getLanguageModel().getModelName(),
typeIRI);
}
}

private void addLanguageModel(final List<Statement> statements,
final String languageModel,
final IRI iri) {
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/upc/edu/gessi/repo/util/ReviewQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ public String getCountQuery() {

public String findReviewsByAppIdAndFeatures(String appId, final List<String> features) {
StringBuilder queryBuilder = new StringBuilder();

queryBuilder.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
queryBuilder.append("PREFIX schema: <https://schema.org/>\n");
queryBuilder.append("SELECT ?id ?text ?feature ?model\n");
queryBuilder.append("PREFIX mapp: <https://gessi.upc.edu/en/tools/mapp-kg/>\n");
queryBuilder.append("SELECT ?id ?text ?feature ?model ?polarityId ?typeId ?topicId\n");
queryBuilder.append("WHERE {\n");
queryBuilder.append(" ?app a schema:MobileApplication ;\n");
queryBuilder.append(" schema:identifier \"" + appId + "\" ;\n");
Expand All @@ -287,13 +287,25 @@ public String findReviewsByAppIdAndFeatures(String appId, final List<String> fea
queryBuilder.append(" schema:disambiguatingDescription ?languageModel .\n");
queryBuilder.append(" ?languageModel a schema:softwareApplication ;\n");
queryBuilder.append(" schema:identifier ?model .\n");
queryBuilder.append(" VALUES ?feature {\n");

for (String feature : features) {
queryBuilder.append(" \"" + feature + "\"\n");
}

queryBuilder.append(" OPTIONAL {\n");
queryBuilder.append(" ?reviewSentence mapp:polarity ?polarity .\n");
queryBuilder.append(" ?polarity schema:identifier ?polarityId .\n");
queryBuilder.append(" }\n");
queryBuilder.append(" OPTIONAL {\n");
queryBuilder.append(" ?reviewSentence mapp:type ?type .\n");
queryBuilder.append(" ?type schema:identifier ?typeId .\n");
queryBuilder.append(" }\n");
queryBuilder.append(" OPTIONAL {\n");
queryBuilder.append(" ?reviewSentence mapp:topic ?topic .\n");
queryBuilder.append(" ?topic schema:identifier ?topicId .\n");
queryBuilder.append(" }\n");
if (features != null && !features.isEmpty()) {
queryBuilder.append(" VALUES ?feature {\n");
for (String feature : features) {
queryBuilder.append(" \"" + feature + "\"\n");
}
queryBuilder.append(" }\n");
}
queryBuilder.append("}\n");

return queryBuilder.toString();
Expand Down
Loading
Loading