diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ca22b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen +# Created by .ignore support plugin (hsz.mobi) diff --git a/.idea/libraries/spring_expression_3_2_2_RELEASE1.xml b/.idea/libraries/spring_expression_3_2_2_RELEASE1.xml new file mode 100644 index 0000000..d0da896 --- /dev/null +++ b/.idea/libraries/spring_expression_3_2_2_RELEASE1.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index b676fbb..00fa2df 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,25 +4,7 @@ - + - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 63be2d0..d341c7b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,15 @@ - + + + + + + + + + @@ -62,42 +70,12 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -105,46 +83,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -152,6 +90,9 @@ + + + + + @@ -457,57 +767,68 @@ 1373997660018 + + + - - + + + + - - + - + + - + - + - + + - + - - + - + + - + + @@ -515,19 +836,133 @@ + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + @@ -544,7 +979,8 @@ - + + @@ -553,7 +989,10 @@ - + + + + @@ -561,7 +1000,10 @@ - + + + + @@ -569,7 +1011,12 @@ - + + + + + + @@ -577,7 +1024,10 @@ - + + + + @@ -616,83 +1066,269 @@ - + - - + + - + - + - + - - + - + - - - + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + + - + - - - + + + + + + + + + + + + + + + + + + @@ -736,7 +1372,7 @@ - 1.7 + 1.8 - commons-logging-1.1.1 + spring-expression-3.2.2.RELEASE1 - - + \ No newline at end of file diff --git a/src/com/lang/LanguageDetector.java b/src/com/lang/LanguageDetector.java index 24feb43..6ae43be 100644 --- a/src/com/lang/LanguageDetector.java +++ b/src/com/lang/LanguageDetector.java @@ -2,10 +2,12 @@ import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.List; import java.util.Map; +@Component public class LanguageDetector { private Map> mapping; @@ -20,6 +22,7 @@ public Language detectLanguage(String text) { return Language.ENGLISH; } + @PostConstruct private void initMapping() { mapping = new HashMap>(); } diff --git a/src/com/main/AppConfiguration.java b/src/com/main/AppConfiguration.java index f3e3f0f..5bbf79a 100644 --- a/src/com/main/AppConfiguration.java +++ b/src/com/main/AppConfiguration.java @@ -1,11 +1,11 @@ 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(basePackages = "com") public class AppConfiguration { - - //TODO: Implement me } diff --git a/src/com/main/Dictionary.java b/src/com/main/Dictionary.java index 11890b0..88dbdda 100644 --- a/src/com/main/Dictionary.java +++ b/src/com/main/Dictionary.java @@ -2,32 +2,56 @@ import com.lang.Language; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Repository; import java.util.HashMap; import java.util.List; import java.util.Map; + +@Repository public class Dictionary { - private Map> dictionaries = new HashMap>(); - - public String translate(String word, Language language) { - //TODO: Implement me - return null; - } - - private Map getDictionary(Language language) { - Map dictionary = dictionaries.get(language); - if (null == dictionary) { - dictionary = loadDictionary(language); - dictionaries.put(language, dictionary); - } - return dictionary; - } - - private Map loadDictionary(Language language) { - //TODO: Implement me - return null; - } + @Autowired + ResourceLoader resourceLoader; + + private Map> dictionaries = new HashMap<>(); + + + public String translate(String word, Language language) { + String translated = getDictionary(language).get(word); + if (translated == null) { + return word; + } + return translated; + } + + private Map getDictionary(Language language) { + Map dictionary = dictionaries.get(language); + if (null == dictionary) { + dictionary = loadDictionary(language); + dictionaries.put(language, dictionary); + } + return dictionary; + } + + private Map loadDictionary(Language language) { + List load = resourceLoader.load("/home/sergei/GeekHub/HomeTaskWeek11/dict/english.dict"); + Map dictionary = new HashMap<>(); + + if (language.equals(Language.ENGLISH)) { + for (String s : load) { + String[] arr = s.split("="); + dictionary.put(arr[0], arr[1]); + } + } else if (language.equals(Language.RUSSIAN)) { + for (String s : load) { + String[] arr = s.split("="); + dictionary.put(arr[1], arr[0]); + } + } else + throw new UnsupportedOperationException(); + + return dictionary; + } } diff --git a/src/com/main/Main.java b/src/com/main/Main.java index 139a2f1..63cc1f0 100644 --- a/src/com/main/Main.java +++ b/src/com/main/Main.java @@ -3,14 +3,14 @@ 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 { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class); Translator translator = context.getBean(Translator.class); - String translation = translator.translate("d:/1.txt"); + String translation = translator.translate("/home/translate.txt"); System.out.println(translation); } } diff --git a/src/com/main/ResourceLoader.java b/src/com/main/ResourceLoader.java index 02a2435..8526afe 100644 --- a/src/com/main/ResourceLoader.java +++ b/src/com/main/ResourceLoader.java @@ -1,6 +1,7 @@ package com.main; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import java.io.IOException; import java.nio.charset.Charset; @@ -9,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +@Service public class ResourceLoader { public List load(String source) { diff --git a/src/com/main/TextSource.java b/src/com/main/TextSource.java index 77b4eb7..70c064e 100644 --- a/src/com/main/TextSource.java +++ b/src/com/main/TextSource.java @@ -1,9 +1,21 @@ package com.main; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +@Component public class TextSource { - public String getText(String path) { - //TODO: Implement me - return null; - } + public String getText(String path) { + String str = null; + try { + str = new String(Files.readAllBytes(Paths.get(path))); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } } diff --git a/src/com/main/Translator.java b/src/com/main/Translator.java index 48c4dad..8983a56 100644 --- a/src/com/main/Translator.java +++ b/src/com/main/Translator.java @@ -1,14 +1,31 @@ package com.main; -import org.springframework.stereotype.Component; +import com.lang.Language; +import com.lang.LanguageDetector; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; +@Service public class Translator { - public String translate(String source) { - //TODO: Implement me - return null; - } + @Autowired + TextSource textSource; + + @Autowired + LanguageDetector languageDetector; + @Autowired + Dictionary dictionary; + + public String translate(String source) { + String text = textSource.getText(source); + Language language = languageDetector.detectLanguage(text); + StringBuilder stringBuilder = new StringBuilder(); + for(String s:text.split(" ")){ + stringBuilder.append(dictionary.translate(s,language)+" "); + } + return stringBuilder.toString(); + } }