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

Extract features from reviews #14

Merged
merged 8 commits into from
Jun 10, 2024
Merged
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
9 changes: 3 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
FROM maven:3.8.6-openjdk-18-slim

RUN mkdir app-repo

RUN mkdir /app-repo
ADD . /app-repo/

WORKDIR /app-repo

RUN mvn clean

RUN mvn package spring-boot:repackage

EXPOSE 3003

ENTRYPOINT ["java"]
ENV SPRING_PROFILES_ACTIVE=docker

CMD ["-jar", "./target/repo-0.0.1-SNAPSHOT.jar"]
ENTRYPOINT ["java", "-jar", "./target/repo-0.0.1-SNAPSHOT.jar"]
14 changes: 14 additions & 0 deletions src/main/java/upc/edu/gessi/repo/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package upc.edu.gessi.repo;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@ public interface InductiveKnowledgeAPI extends BaseAPI {
@GetMapping("/last-review")
int getLastReview();

@PostMapping("/derived-NL-Features")
@PostMapping("/derived-nl-features")
int derivedNLFeatures(@RequestParam(value = "documentType") DocumentType documentType,
@RequestParam(value = "batch-size") Integer batchSize,
@RequestParam(value = "from") Integer from);
@RequestParam(value = "batch-size", defaultValue = "0") Integer batchSize,
@RequestParam(value = "from", defaultValue = "0") Integer from,
@RequestParam(value = "feature-model", defaultValue = "transfeatex") String featureModel);

@PostMapping("/compute-Feature-Similarity")
@PostMapping("/compute-feature-similarity")
void computeFeatureSimilarity(@RequestParam(defaultValue = "0.5", name = "threshold") double synonymThreshold);

@DeleteMapping("/Feature-Similarities")

@DeleteMapping("/feature-similarities")
void deleteFeatureSimilarities();

@PostMapping("/compute-Similarity")
@PostMapping("/compute-similarity")
void computeSimilarity(@RequestParam(defaultValue = "JACCARD") SimilarityAlgorithm algorithm);

@GetMapping("/find-Similar-Apps")
@GetMapping("/find-similar-apps")
Map<String, List<SimilarityApp>> getTopKSimilarApps(@RequestBody List<String> apps,
@RequestParam Integer k,
@RequestParam DocumentType documentType);

@GetMapping("/find-Apps-By-Feature")
@GetMapping("/find-apps-by-feature")
Map<String, List<SimilarityApp>> findAppsByFeature(@RequestBody List<String> features,
@RequestParam Integer k,
@RequestParam DocumentType documentType);

@GetMapping("/find-Apps-By-Features")
@GetMapping("/find-apps-by-features")
List<SimilarityApp> findAppsByFeatures(@RequestBody List<String> features,
@RequestParam Integer k,
@RequestParam DocumentType documentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public int getLastReview() {
}

@Override
@PostMapping("/derived-NL-Features")
public int derivedNLFeatures(@RequestParam(value = "documentType", defaultValue = "DESCRIPTION") DocumentType documentType,
@RequestParam(value = "batch-size", defaultValue = "10") Integer batchSize,
@RequestParam(value = "from", defaultValue = "0") Integer from) {
public int derivedNLFeatures(final DocumentType documentType,
final Integer batchSize,
final Integer from,
final String featureModel) {
logger.info("Generating derived deductive knowledge from natural language documents");
logger.info("Document type: " + documentType);
if (documentType.equals(DocumentType.REVIEWS)) {
logger.info("Deducting features from reviews...");
try {
return ((FeatureService) useService(FeatureService.class)).extractFeaturesFromReviews(batchSize, from);
return ((FeatureService) useService(FeatureService.class)).extractFeaturesFromReviews(batchSize, from, featureModel);
} catch (Exception e) {
return dbConnection.getCount();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import java.io.Serializable;
import java.util.List;

public class AnalyzedDocument implements Serializable {
public class AnalyzedDocumentDTO implements Serializable {

private String id;
private String text;
private List<String> features;

public AnalyzedDocument(String id, List<String> features) {
public AnalyzedDocumentDTO(String id, List<String> features) {
this.id = id;
this.features = features;
}

public AnalyzedDocument(String id, String text) {
public AnalyzedDocumentDTO(String id, String text) {
this.id = id;
this.text = text;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/upc/edu/gessi/repo/dto/Review/HUBResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package upc.edu.gessi.repo.dto.Review;

import java.util.List;

public class HUBResponseDTO {
private List<ReviewDTO> analyzed_reviews;

public List<ReviewDTO> getAnalyzed_reviews() {
return analyzed_reviews;
}

public void setAnalyzed_reviews(List<ReviewDTO> analyzed_reviews) {
this.analyzed_reviews = analyzed_reviews;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ void addSentenceToReview(String reviewId,
List<GraphReview> getReviews(String nodeId);

SentenceDTO getSentenceDTO(TupleQueryResult result);

List<ReviewDTO> findBatched(final int limit, final int offset);

List<ReviewDTO> findAllSimplified();

Integer getCount();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package upc.edu.gessi.repo.repository.impl;

import io.swagger.models.auth.In;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
Expand Down Expand Up @@ -130,7 +131,7 @@ public List<ReviewDTO> findListed(List<String> reviewIds) throws NoReviewsFoundE
}
List<ReviewDTO> reviewDTOs = new ArrayList<>();
while (reviewsResult.hasNext()) {
ReviewDTO reviewDTO = getReviewDTO(reviewsResult);
ReviewDTO reviewDTO = getReviewDTO(reviewsResult.next());
reviewDTOs.add(reviewDTO);
}
return reviewDTOs;
Expand All @@ -142,7 +143,10 @@ public IRI insert(ReviewDTO dto) {
if (dto.getId() != null) {
IRI reviewIRI = factory.createIRI(schemaIRI.getReviewIRI() + "/" + dto.getId());
statements.add(factory.createStatement(reviewIRI, schemaIRI.getTypeIRI(), schemaIRI.getReviewIRI()));
IRI applicationIRI = factory.createIRI(schemaIRI.getAppIRI() + "/" + dto.getPackageName());
IRI applicationIRI = null;
if (dto.getPackageName() != null) {
applicationIRI = factory.createIRI(schemaIRI.getAppIRI() + "/" + dto.getPackageName());
}
if (applicationIRI != null) {
statements.add(factory.createStatement(applicationIRI, schemaIRI.getReviewsIRI(), reviewIRI));
}
Expand Down Expand Up @@ -280,6 +284,51 @@ public SentenceDTO getSentenceDTO(final TupleQueryResult result) {
return sentenceDTO;
}

@Override
public List<ReviewDTO> findBatched(int limit, int offset) {
String query = reviewQueryBuilder.findAllQueryWithLimitOffset(limit, offset);
TupleQueryResult result = Utils.runSparqlSelectQuery(repository.getConnection(), query);
List<ReviewDTO> reviewDTOList = new ArrayList<>();
while (result.hasNext()) {
reviewDTOList.add(getReviewDTO(result.next()));
}
return reviewDTOList;
}

@Override
public List<ReviewDTO> findAllSimplified() {
String query = reviewQueryBuilder.findAllSimplifiedQuery();
TupleQueryResult result = Utils.runSparqlSelectQuery(repository.getConnection(), query);
List<ReviewDTO> reviewDTOList = new ArrayList<>();
while (result.hasNext()) {
reviewDTOList.add(getReviewDTO(result.next()));
}
return reviewDTOList;
}


@Override
public Integer getCount() {
String query = reviewQueryBuilder.getCountQuery();
TupleQueryResult result = Utils.runSparqlSelectQuery(repository.getConnection(), query);

try {
if (result.hasNext()) {
BindingSet bindingSet = result.next();
String countStr = bindingSet.getValue("count").stringValue();
return Integer.parseInt(countStr);
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
result.close();
}
}


private void commitChanges(final List<Statement> statements) {
RepositoryConnection repoConnection = repository.getConnection();
repoConnection.add(statements);
Expand All @@ -288,16 +337,25 @@ private void commitChanges(final List<Statement> statements) {



private ReviewDTO getReviewDTO(final TupleQueryResult result) {
private ReviewDTO getReviewDTO(final BindingSet bindings) {
ReviewDTO ReviewDTO = new ReviewDTO();
BindingSet bindings = result.next();
if (existsShortReviewBinding(bindings)) {
String idValue = bindings.getBinding("id").getValue().stringValue();
String textValue = bindings.getBinding("text").getValue().stringValue();
String appValue = bindings.getBinding("app_identifier").getValue().stringValue();
ReviewDTO.setId(idValue);
ReviewDTO.setReviewText(textValue);
ReviewDTO.setApplicationId(appValue);
if (bindings.getBinding("id") != null && bindings.getBinding("id").getValue() != null) {
String idValue = bindings.getBinding("id").getValue().stringValue();
ReviewDTO.setId(idValue);
}

if (bindings.getBinding("text") != null && bindings.getBinding("text").getValue() != null) {
String textValue = bindings.getBinding("text").getValue().stringValue();
ReviewDTO.setReviewText(textValue);
}

if (bindings.getBinding("app_identifier") != null && bindings.getBinding("app_identifier").getValue() != null) {
String appValue = bindings.getBinding("app_identifier").getValue().stringValue();
ReviewDTO.setApplicationId(appValue);

}

if (bindings.getBinding("date") != null && bindings.getBinding("date").getValue() != null) {
String dateString = bindings.getBinding("date").getValue().stringValue();
try {
Expand Down Expand Up @@ -329,8 +387,6 @@ private boolean existsShortReviewBinding(BindingSet bindings) {
return bindings.getBinding("id") != null
&& bindings.getBinding("id").getValue() != null
&& bindings.getBinding("text") != null
&& bindings.getBinding("text").getValue() != null
&& bindings.getBinding("app_identifier") != null
&& bindings.getBinding("app_identifier").getValue() != null;
&& bindings.getBinding("text").getValue() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public interface FeatureService extends CrudService<Feature> {
void extractFeaturesByDocument(DocumentType documentType, int batchSize);

int extractFeaturesFromReviews(int batchSize, int from);
int extractFeaturesFromReviews(int batchSize, int from, String featureModel);
}
7 changes: 7 additions & 0 deletions src/main/java/upc/edu/gessi/repo/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

import upc.edu.gessi.repo.dto.Review.ReviewDTO;

import java.util.List;

public interface ReviewService extends CrudService<ReviewDTO> {
List<ReviewDTO> getBatched(final int batch, final int offset);

List<ReviewDTO> getAllSimplified();

Integer getReviewCount();
}
Loading
Loading