wordstudent

Word learning program
git clone git://git.wimdupont.com/wordstudent.git
Log | Files | Refs | LICENSE

commit 77fb4ffa54183ae22d8514a79d1c4dc49f2d9b8b
parent a27566432cb727e572c7523c7afaf0b7ba4a8bc3
Author: WimDupont <WimDupont@users.noreply.gitlab.com>
Date:   Wed, 19 Jan 2022 20:47:28 +0100

persisted fetches and updated sysouts

Diffstat:
Msrc/main/java/com/wimdupont/WordStudentAdvancedApplication.java | 2++
Msrc/main/java/com/wimdupont/repository/dao/Antonym.java | 7++++++-
Msrc/main/java/com/wimdupont/repository/dao/Definition.java | 10++++++++--
Msrc/main/java/com/wimdupont/repository/dao/Dictionary.java | 5+++++
Msrc/main/java/com/wimdupont/repository/dao/Meaning.java | 6++++++
Msrc/main/java/com/wimdupont/repository/dao/Synonym.java | 7++++++-
Msrc/main/java/com/wimdupont/repository/dao/Word.java | 2+-
Asrc/main/java/com/wimdupont/service/WordFetcher.java | 32++++++++++++++++++++++++++++++++
Asrc/main/java/com/wimdupont/service/WordService.java | 47+++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main/java/com/wimdupont/service/WordTester.java | 95+++++++++++++++++--------------------------------------------------------------
10 files changed, 133 insertions(+), 80 deletions(-)

diff --git a/src/main/java/com/wimdupont/WordStudentAdvancedApplication.java b/src/main/java/com/wimdupont/WordStudentAdvancedApplication.java @@ -1,5 +1,6 @@ package com.wimdupont; +import com.wimdupont.service.WordService; import com.wimdupont.service.WordTester; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -12,6 +13,7 @@ public class WordStudentAdvancedApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(WordStudentAdvancedApplication.class, args); + context.getBean(WordService.class).saveNew(); context.getBean(WordTester.class).process(); System.exit(0); } diff --git a/src/main/java/com/wimdupont/repository/dao/Antonym.java b/src/main/java/com/wimdupont/repository/dao/Antonym.java @@ -12,8 +12,8 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; @Data -@Builder @Entity +@Builder @NoArgsConstructor @AllArgsConstructor public class Antonym { @@ -24,4 +24,9 @@ public class Antonym { @Exclude private String id; private String word; + + @Override + public String toString() { + return getWord(); + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Definition.java b/src/main/java/com/wimdupont/repository/dao/Definition.java @@ -6,10 +6,8 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString.Exclude; import org.hibernate.annotations.GenericGenerator; -import org.hibernate.annotations.Type; import javax.persistence.CascadeType; -import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @@ -40,4 +38,12 @@ public class Definition { @JoinColumn(name = "definition_id") private List<Antonym> antonyms; + @Override + public String toString() { + return "\t" + getDefinition() + System.lineSeparator() + + (getSynonyms().isEmpty() ? "" : "Synonyms: " + System.lineSeparator() + "\t" + getSynonyms() + System.lineSeparator()) + + (getAntonyms().isEmpty() ? "" : "Antonyms: " + System.lineSeparator() + "\t" + getAntonyms() + System.lineSeparator() + + (getExample() == null ? "" : "Example: " + getExample())); + } + } diff --git a/src/main/java/com/wimdupont/repository/dao/Dictionary.java b/src/main/java/com/wimdupont/repository/dao/Dictionary.java @@ -37,4 +37,9 @@ public class Dictionary { @JoinColumn(name = "dictionary_id") private List<Meaning> meanings; + @Override + public String toString() { + return getMeanings().toString(); + } + } diff --git a/src/main/java/com/wimdupont/repository/dao/Meaning.java b/src/main/java/com/wimdupont/repository/dao/Meaning.java @@ -31,4 +31,10 @@ public class Meaning { @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "meaning_id") private List<Definition> definitions; + + @Override + public String toString() { + return getPartOfSpeech() + System.lineSeparator() + + (getDefinitions().isEmpty() ? "" : "Definitions: " + System.lineSeparator() + getDefinitions()); + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Synonym.java b/src/main/java/com/wimdupont/repository/dao/Synonym.java @@ -12,8 +12,8 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; @Data -@Builder @Entity +@Builder @NoArgsConstructor @AllArgsConstructor public class Synonym { @@ -24,4 +24,9 @@ public class Synonym { @Exclude private String id; private String word; + + @Override + public String toString() { + return getWord(); + } } diff --git a/src/main/java/com/wimdupont/repository/dao/Word.java b/src/main/java/com/wimdupont/repository/dao/Word.java @@ -35,6 +35,6 @@ public class Word { @Override public String toString(){ - return "hi"; + return dictionaries.toString().replaceAll("\\[", "").replaceAll("]", ""); } } diff --git a/src/main/java/com/wimdupont/service/WordFetcher.java b/src/main/java/com/wimdupont/service/WordFetcher.java @@ -0,0 +1,32 @@ +package com.wimdupont.service; + +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +@Component +public class WordFetcher { + + private static final String CSV_PROPERTY = "csv.dir"; + public List<String> fetch() { + final Properties properties = new Properties(); + try (InputStream is = getClass().getResourceAsStream("/application.properties")) { + properties.load(is); + String csvDir = properties.getProperty(CSV_PROPERTY); + List<String> words = new ArrayList<>(); + Files.lines(Path.of(csvDir)).forEach(line -> words.addAll(Arrays.stream(line.split(";")) + .map(String::trim).toList()) + ); + return words; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/wimdupont/service/WordService.java b/src/main/java/com/wimdupont/service/WordService.java @@ -0,0 +1,47 @@ +package com.wimdupont.service; + +import com.wimdupont.client.DictionaryAPI; +import com.wimdupont.client.model.dto.DictionaryDto; +import com.wimdupont.converter.WordConverter; +import com.wimdupont.repository.WordRepository; +import com.wimdupont.repository.dao.Word; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +@Service +@Transactional +@RequiredArgsConstructor +public class WordService { + + private static final DictionaryAPI DICTIONARY_API = new DictionaryAPI(); + + private final WordRepository wordRepository; + private final WordConverter wordConverter; + private final EntityManager entityManager; + private final WordFetcher wordFetcher; + + public void saveNew(){ + List<String> words = wordFetcher.fetch(); + List<Word> savedWordList = wordRepository.findAll(); + System.out.printf("Words in database: %s%n", savedWordList.size()); + List<String> savedWords = savedWordList.stream().map(Word::getWord).toList(); + AtomicInteger count = new AtomicInteger(0); + words.stream() + .filter(csvWord -> !savedWords.contains(csvWord)) + .forEach(newWord -> { + List<DictionaryDto> response = DICTIONARY_API.getAffirmation(newWord); + Word word = wordRepository.save(wordConverter.convert(newWord, response)); + System.out.printf("Saved new word %s%n", word.getWord()); + count.getAndIncrement(); + }); + + if (count.get() > 0) + System.out.printf("Saved %s words%n", count.get()); + entityManager.flush(); + } +} diff --git a/src/main/java/com/wimdupont/service/WordTester.java b/src/main/java/com/wimdupont/service/WordTester.java @@ -4,103 +4,48 @@ import com.wimdupont.client.DictionaryAPI; import com.wimdupont.client.model.dto.DictionaryDto; import com.wimdupont.converter.WordConverter; import com.wimdupont.repository.WordRepository; -import com.wimdupont.repository.dao.Definition; -import com.wimdupont.repository.dao.Dictionary; -import com.wimdupont.repository.dao.Meaning; import com.wimdupont.repository.dao.Word; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; -import java.util.Properties; import java.util.Scanner; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; @Component -@Transactional @RequiredArgsConstructor +@Transactional public class WordTester { - private static final String CSV_PROPERTY = "csv.dir"; private static final DictionaryAPI DICTIONARY_API = new DictionaryAPI(); private final WordRepository wordRepository; private final WordConverter wordConverter; + private final WordFetcher wordFetcher; public void process() { Scanner scanner = new Scanner(System.in); - final Properties properties = new Properties(); - try (InputStream is = getClass().getResourceAsStream("/application.properties")) { - properties.load(is); - String csvDir = properties.getProperty(CSV_PROPERTY); - List<String> words = new ArrayList<>(); - Files.lines(Path.of(csvDir)).forEach(line -> words.addAll(Arrays.stream(line.split(";")) - .map(String::trim).collect(Collectors.toList())) - ); - - System.out.printf("Words in csv file: %s%n", words.size()); - List<Word> savedWordList = wordRepository.findAll(); - System.out.printf("Words in database: %s%n", savedWordList.size()); - - List<String> savedWords = savedWordList.stream().map(Word::getWord).collect(Collectors.toList()); - AtomicInteger count = new AtomicInteger(0); - words.stream() - .filter(csvWord -> !savedWords.contains(csvWord)) - .forEach(newWord -> { - List<DictionaryDto> response = DICTIONARY_API.getAffirmation(newWord); - Word word = wordRepository.save(wordConverter.convert(newWord, response)); - System.out.printf("Saved new word %s%n", word.getWord()); - count.getAndIncrement(); - }); - - if (count.get() > 0) - System.out.printf("Saved %s words%n", count.get()); - - Collections.shuffle(words); - for (String word : words) { - System.out.println(); - String example = ""; - Optional<Word> result = wordRepository.findByWord(word); - Word saved; - if (result.isEmpty()) { - List<DictionaryDto> response = DICTIONARY_API.getAffirmation(word); - saved = wordRepository.save(wordConverter.convert(word, response)); - } else { - saved = result.get(); - } - - for (Dictionary r : saved.getDictionaries()) { - for (Meaning meaning : r.getMeanings()) { - System.out.println(meaning.getPartOfSpeech()); - for (Definition definition : meaning.getDefinitions()) { - example = definition.getExample(); - System.out.println("Definition: " + definition.getDefinition()); - if (!definition.getSynonyms().isEmpty()) - System.out.println("Synonyms: " + definition.getSynonyms()); - if (!definition.getAntonyms().isEmpty()) - System.out.println("Antonyms: " + definition.getAntonyms()); - } - } - } - scanner.nextLine(); - System.out.println(word); - if (example != null && !example.isEmpty()) - System.out.println("Example: " + example); - System.out.println("========================"); + List<String> words = wordFetcher.fetch(); + System.out.printf("Words in csv file: %s%n", words.size()); + + Collections.shuffle(words); + for (String word : words) { + System.out.println(); + System.out.println(word); + Optional<Word> result = wordRepository.findByWord(word); + Word saved; + if (result.isEmpty()) { + List<DictionaryDto> response = DICTIONARY_API.getAffirmation(word); + saved = wordRepository.save(wordConverter.convert(word, response)); + } else { + saved = result.get(); } - System.out.println("All words have been tested."); - } catch (IOException e) { - e.printStackTrace(); + scanner.nextLine(); + System.out.println(saved); + System.out.println("========================"); } + System.out.println("All words have been tested."); } }