wordstudent

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

commit a27566432cb727e572c7523c7afaf0b7ba4a8bc3
parent 3dc1ad14055e20fcc7a90e74a444d62817f6b7a2
Author: WimDupont <WimDupont@users.noreply.gitlab.com>
Date:   Sun,  5 Dec 2021 20:41:37 +0100

change to 1 record per word

Diffstat:
Msrc/main/java/com/wimdupont/converter/DictionaryConverter.java | 2+-
Asrc/main/java/com/wimdupont/converter/WordConverter.java | 21+++++++++++++++++++++
Dsrc/main/java/com/wimdupont/repository/DictionaryRepository.java | 13-------------
Asrc/main/java/com/wimdupont/repository/WordRepository.java | 13+++++++++++++
Asrc/main/java/com/wimdupont/repository/dao/Word.java | 40++++++++++++++++++++++++++++++++++++++++
Msrc/main/java/com/wimdupont/service/WordTester.java | 39++++++++++++++++++++++++++++++++-------
Msrc/main/resources/init.sql | 11++++++++++-
7 files changed, 117 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/wimdupont/converter/DictionaryConverter.java b/src/main/java/com/wimdupont/converter/DictionaryConverter.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Component; import java.util.stream.Collectors; @Component -public record DictionaryConverter(MeaningConverter meaningConverter, +record DictionaryConverter(MeaningConverter meaningConverter, PhoneticConverter phoneticConverter) { public Dictionary convert(DictionaryDto dictionaryDto) { diff --git a/src/main/java/com/wimdupont/converter/WordConverter.java b/src/main/java/com/wimdupont/converter/WordConverter.java @@ -0,0 +1,21 @@ +package com.wimdupont.converter; + +import com.wimdupont.client.model.dto.DictionaryDto; +import com.wimdupont.repository.dao.Word; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.stream.Collectors; + +@Component +public record WordConverter(DictionaryConverter dictionaryConverter) { + + public Word convert(String word, List<DictionaryDto> dictionaryDtoList) { + return Word.builder() + .word(word) + .dictionaries(dictionaryDtoList.stream() + .map(dictionaryConverter::convert) + .collect(Collectors.toList())) + .build(); + } +} diff --git a/src/main/java/com/wimdupont/repository/DictionaryRepository.java b/src/main/java/com/wimdupont/repository/DictionaryRepository.java @@ -1,13 +0,0 @@ -package com.wimdupont.repository; - -import com.wimdupont.repository.dao.Dictionary; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface DictionaryRepository extends JpaRepository<Dictionary, String> { - - List<Dictionary> findByWord(String word); -} diff --git a/src/main/java/com/wimdupont/repository/WordRepository.java b/src/main/java/com/wimdupont/repository/WordRepository.java @@ -0,0 +1,13 @@ +package com.wimdupont.repository; + +import com.wimdupont.repository.dao.Word; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface WordRepository extends JpaRepository<Word, String> { + + Optional<Word> findByWord(String word); +} diff --git a/src/main/java/com/wimdupont/repository/dao/Word.java b/src/main/java/com/wimdupont/repository/dao/Word.java @@ -0,0 +1,40 @@ +package com.wimdupont.repository.dao; + + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString.Exclude; +import org.hibernate.annotations.GenericGenerator; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import java.util.List; + +@Entity +@Builder(toBuilder = true) +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Word { + + @Id + @GeneratedValue(generator = "uuid") + @GenericGenerator(name = "uuid", strategy = "uuid") + @Exclude + private String id; + private String word; + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "word_id") + private List<Dictionary> dictionaries; + + @Override + public String toString(){ + return "hi"; + } +} diff --git a/src/main/java/com/wimdupont/service/WordTester.java b/src/main/java/com/wimdupont/service/WordTester.java @@ -2,11 +2,12 @@ package com.wimdupont.service; import com.wimdupont.client.DictionaryAPI; import com.wimdupont.client.model.dto.DictionaryDto; -import com.wimdupont.converter.DictionaryConverter; -import com.wimdupont.repository.DictionaryRepository; +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; @@ -19,8 +20,10 @@ 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 @@ -31,8 +34,8 @@ public class WordTester { private static final String CSV_PROPERTY = "csv.dir"; private static final DictionaryAPI DICTIONARY_API = new DictionaryAPI(); - private final DictionaryRepository dictionaryRepository; - private final DictionaryConverter dictionaryConverter; + private final WordRepository wordRepository; + private final WordConverter wordConverter; public void process() { Scanner scanner = new Scanner(System.in); @@ -44,17 +47,39 @@ public class WordTester { 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 = ""; - List<Dictionary> result = dictionaryRepository.findByWord(word); + Optional<Word> result = wordRepository.findByWord(word); + Word saved; if (result.isEmpty()) { List<DictionaryDto> response = DICTIONARY_API.getAffirmation(word); - response.forEach(dto -> result.add(dictionaryRepository.save(dictionaryConverter.convert(dto)))); + saved = wordRepository.save(wordConverter.convert(word, response)); + } else { + saved = result.get(); } - for (Dictionary r : result) { + for (Dictionary r : saved.getDictionaries()) { for (Meaning meaning : r.getMeanings()) { System.out.println(meaning.getPartOfSpeech()); for (Definition definition : meaning.getDefinitions()) { diff --git a/src/main/resources/init.sql b/src/main/resources/init.sql @@ -1,8 +1,17 @@ +CREATE TABLE word ( + id VARCHAR(36) PRIMARY KEY NOT NULL, + word VARCHAR(200) UNIQUE +); + +CREATE UNIQUE INDEX word_index ON word(word); + CREATE TABLE dictionary ( id VARCHAR(36) PRIMARY KEY NOT NULL, word VARCHAR(200), phonetic VARCHAR(200), - origin VARCHAR(200) + origin VARCHAR(200), + word_id VARCHAR(36), + CONSTRAINT fk_word_dictionary FOREIGN KEY (word_id) REFERENCES word (id) ); CREATE TABLE phonetic (