Skip to content
Open
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: 4 additions & 0 deletions dict/russian.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
o=про
снова=знову
все=всі
всегда=завжди
3 changes: 2 additions & 1 deletion src/com/lang/LanguageDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;

@Component
public class LanguageDetector {

private Map<Language, List<String>> mapping;
Expand All @@ -21,6 +22,6 @@ public Language detectLanguage(String text) {
}

private void initMapping() {
mapping = new HashMap<Language, List<String>>();
mapping = new HashMap<>();
}
}
5 changes: 2 additions & 3 deletions src/com/main/AppConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.main;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ComponentScan("com")
public class AppConfiguration {

//TODO: Implement me
}
23 changes: 18 additions & 5 deletions src/com/main/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
import java.util.List;
import java.util.Map;

@Component
public class Dictionary {

private Map<Language, Map<String, String>> dictionaries = new HashMap<Language, Map<String, String>>();
@Autowired
ResourceLoader resourceLoader;

private Map<Language, Map<String, String>> dictionaries = new HashMap<>();

public String translate(String word, Language language) {
//TODO: Implement me
return null;
Map<String, String> dictionary = getDictionary(language);
word = word.replaceAll("\\p{Punct}", "");
String translation = dictionary.get(word);
return translation == null ? word : translation;

}

private Map<String, String> getDictionary(Language language) {
Expand All @@ -27,7 +34,13 @@ private Map<String, String> getDictionary(Language language) {
}

private Map<String, String> loadDictionary(Language language) {
//TODO: Implement me
return null;
Map<String, String> dictionary = new HashMap<>();
List<String> lines = resourceLoader.load("dict/" + language.name().toLowerCase() + ".dict");
for (String line : lines) {
String[] words = line.split("=");
dictionary.put(words[0], words[1]);
}

return dictionary;
}
}
1 change: 0 additions & 1 deletion src/com/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
public static void main(String[] args) throws BeansException {
Expand Down
3 changes: 2 additions & 1 deletion src/com/main/ResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.List;

@Component
public class ResourceLoader {

public List<String> load(String source) {
Expand All @@ -17,6 +18,6 @@ public List<String> load(String source) {
} catch (IOException e) {
e.printStackTrace();
}
return new ArrayList<String>();
return new ArrayList<>();
}
}
16 changes: 14 additions & 2 deletions src/com/main/TextSource.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TextSource {

@Autowired
ResourceLoader resourceLoader;

public String getText(String path) {
//TODO: Implement me
return null;
StringBuilder sb = new StringBuilder();

for (String line : resourceLoader.load(path)) {
sb.append(line);
}

return sb.toString();
}
}
32 changes: 27 additions & 5 deletions src/com/main/Translator.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
package com.main;

import com.lang.Language;
import com.lang.LanguageDetector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Translator {

public String translate(String source) {
//TODO: Implement me
return null;
TextSource textSource;
LanguageDetector languageDetector;
Dictionary dictionary;

@Autowired
public Translator(TextSource textSource, LanguageDetector languageDetector, Dictionary dictionary) {
this.textSource = textSource;
this.languageDetector = languageDetector;
this.dictionary = dictionary;
}

public String translate(String source) {
StringBuilder translatedText = new StringBuilder();
String text = textSource.getText(source);
Language language = languageDetector.detectLanguage(text);

for (String word : text.split("\\s+")) {
translatedText
.append(dictionary.translate(word.trim(), language))
.append(" ");
}

return translatedText.toString();
}

}