Skip to content
Snippets Groups Projects
Commit 628f1311 authored by ywb16155's avatar ywb16155
Browse files

commit #46 - Adding punctuation guard to tokens, adding apostrophe strip to...

commit #46 - Adding punctuation guard to tokens, adding apostrophe strip to answer submission, adding guard to navigation in frontend
parent 3ba3e75f
No related branches found
No related tags found
No related merge requests found
......@@ -8,9 +8,7 @@ import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import org.ejml.simple.SimpleMatrix;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.*;
public class SentimentAnalyzer {
......@@ -48,9 +46,10 @@ public class SentimentAnalyzer {
result.setScore(RNNCoreAnnotations.getPredictedClass(tree));
});
List<String> words = new ArrayList<>();
//TODO - look at fixing tokenization of punctuation..
annotation.get(CoreAnnotations.TokensAnnotation.class).forEach(word -> {
words.add(word.word());
if(!word.word().matches("(?![@',&])\\p{Punct}")) {
words.add(word.word());
}
});
result.setText(words);
}
......
......@@ -6,9 +6,12 @@ import app.entities.AnswerEntity;
import app.pojo.WordDetails;
import app.repositories.AnswerEntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
......@@ -18,6 +21,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class SentimentService {
......@@ -37,7 +41,8 @@ public class SentimentService {
answers.forEach(answerEntity -> sentimentResultMap.put(answerEntity, analyzer.analyse(answerEntity.getAnswerText())));
sentimentResultMap.forEach((k,v) -> {
stopwords.forEach(word -> v.getText().remove(word));
List<String> filteredText = v.getText().stream().filter(word -> !stopwords.contains(word.toLowerCase())).collect(Collectors.toList());
v.setText(filteredText);
});
sentimentResultMap.forEach((k,v) -> {
......@@ -77,13 +82,22 @@ public class SentimentService {
private List<String> getStopwords() {
List<String> stopwords = new ArrayList<>();
Path path = Paths.get("stopwords.txt");
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)){
String line = null;
ClassLoader loader = getClass().getClassLoader();
File file;
try {
file = new File(loader.getResource("stopwords.txt").getFile());
} catch (NullPointerException npe) {
return new ArrayList<>();
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))){
String line;
while ((line = reader.readLine()) != null) {
stopwords.add(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
return new ArrayList<>();
}
return stopwords;
......
......@@ -12,6 +12,8 @@ import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Component
public class StudentInfoService {
......@@ -96,7 +98,7 @@ public class StudentInfoService {
entity.setAnswerId(existing);
}
entity.setAnswerText(response.getAnswerText());
entity.setAnswerText(cleanApostrpohes(response.getAnswerText()));
entity.setUsername(response.getUsername());
entity.setSubmitted(submitted);
entity.setPaperId(parseId(response.getPaperId()));
......@@ -110,6 +112,17 @@ public class StudentInfoService {
}
}
private String cleanApostrpohes(String text) {
Pattern p = Pattern.compile("'");
Matcher m = p.matcher(text);
StringBuffer sb = new StringBuffer();
while(m.find()) {
m.appendReplacement(sb,"");
}
m.appendTail(sb);
return sb.toString();
}
private int hasExistingAnswer(String username, int paperId, int questionId) {
if(answerEntityRepository.existsByUsernameAndPaperIdAndQuestionId(username,paperId,questionId)) {
AnswerEntity entity = answerEntityRepository.findByUsernameAndPaperIdAndQuestionId(username,paperId,questionId).get();
......
......@@ -13,6 +13,7 @@ already
also
although
always
am
among
an
and
......
......@@ -8,18 +8,19 @@ import {LecturerCreateOmpViewComponent} from 'src/app/LecturerComponents/Lecture
import {LecturerTemplateViewComponent} from 'src/app/LecturerComponents/LecturerTemplateView/lecturer-template-view.component';
import {LecturerEditTemplateViewComponent} from 'src/app/LecturerComponents/LecturerEditTemplateView/lecturer-edit-template-view.component';
import {LecturerViewPaperComponent} from 'src/app/LecturerComponents/ViewPaperComponents/lecturer-view-paper.component';
import {LoginGuardService} from 'src/app/services/login-guard.service';
const lecturerRoutes = [
{ path: 'ldashboard', component: LecturerRootComponent },
{ path: 'paper-view', component: LecturerPaperViewComponent },
{ path: 'submissions-view', component: LecturerSubmissionsViewComponent},
{ path: 'response-view', component: LecturerResponseViewComponent },
{ path: 'creation-view', component: LecturerCreateOmpViewComponent },
{ path: 'creation-template-view', component: LecturerCreateOmpViewComponent },
{ path: 'template-view', component: LecturerTemplateViewComponent },
{ path: 'view-paper', component: LecturerViewPaperComponent },
{ path: 'edit-template-view', component: LecturerEditTemplateViewComponent }
{ path: 'ldashboard', component: LecturerRootComponent, canActivate: [LoginGuardService]},
{ path: 'paper-view', component: LecturerPaperViewComponent, canActivate: [LoginGuardService]},
{ path: 'submissions-view', component: LecturerSubmissionsViewComponent, canActivate: [LoginGuardService]},
{ path: 'response-view', component: LecturerResponseViewComponent, canActivate: [LoginGuardService]},
{ path: 'creation-view', component: LecturerCreateOmpViewComponent, canActivate: [LoginGuardService]},
{ path: 'creation-template-view', component: LecturerCreateOmpViewComponent, canActivate: [LoginGuardService]},
{ path: 'template-view', component: LecturerTemplateViewComponent, canActivate: [LoginGuardService]},
{ path: 'view-paper', component: LecturerViewPaperComponent, canActivate: [LoginGuardService]},
{ path: 'edit-template-view', component: LecturerEditTemplateViewComponent, canActivate: [LoginGuardService]}
];
@NgModule({
......
......@@ -4,13 +4,14 @@ import {StudentRootComponent} from './student-root.component';
import {StudentClassViewComponent} from './StudentClassView/student-class-view.component';
import {StudentCompletedViewComponent} from './StudentCompletedView/student-completed-view.component';
import {StudentSubmissionViewComponent} from './StudentSubmissionView/student-submission-view.component';
import {LoginGuardService} from 'src/app/services/login-guard.service';
const studentRoutes = [
{ path: 'sdashboard', component: StudentRootComponent },
{ path: 'class-view', component: StudentClassViewComponent },
{ path: 'completed-view', component: StudentCompletedViewComponent },
{ path: 'submission-view', component: StudentSubmissionViewComponent },
{ path: 'sdashboard', component: StudentRootComponent, canActivate: [LoginGuardService]},
{ path: 'class-view', component: StudentClassViewComponent, canActivate: [LoginGuardService]},
{ path: 'completed-view', component: StudentCompletedViewComponent, canActivate: [LoginGuardService]},
{ path: 'submission-view', component: StudentSubmissionViewComponent, canActivate: [LoginGuardService]},
];
......
......@@ -11,6 +11,7 @@ import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {HttpErrorService} from 'src/app/ErrorComponents/http-error.service';
import {StudentRootModule} from 'src/app/StudentComponents/student-root.module';
import {LecturerRootModule} from 'src/app/LecturerComponents/lecturer-root.module';
import {LoginGuardService} from 'src/app/services/login-guard.service';
@NgModule({
declarations: [
......@@ -30,6 +31,7 @@ import {LecturerRootModule} from 'src/app/LecturerComponents/lecturer-root.modul
providers:
[
LoginService,
LoginGuardService,
HttpErrorService
],
bootstrap: [AppComponent]
......
import {Injectable} from '@angular/core';
import {CanActivate, Router} from '@angular/router';
@Injectable()
export class LoginGuardService implements CanActivate {
constructor(private router: Router) {
}
canActivate(): boolean {
if (!sessionStorage.getItem('username')) {
this.router.navigate(['/login']);
return false;
}
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment