From 152bbcb2b6cf4138a3e7f362d7539017d51853b0 Mon Sep 17 00:00:00 2001 From: SergeiDev Date: Mon, 18 Jan 2016 17:36:57 +0200 Subject: [PATCH 1/3] init --- .gitignore | 1 + .../spring_expression_3_2_2_RELEASE1.xml | 15 +++++++++++++++ .idea/vcs.xml | 6 ++++++ 3 files changed, 22 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/libraries/spring_expression_3_2_2_RELEASE1.xml create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bfa6a22 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +# 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/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 From ebb15a664da5530c3763d19a0bcc356d11ef2ec6 Mon Sep 17 00:00:00 2001 From: SergeiDev Date: Mon, 18 Jan 2016 21:04:55 +0200 Subject: [PATCH 2/3] implemented all methods --- .gitignore | 8 + .idea/workspace.xml | 1213 ++++++++++++++++++++++++---- Spring.iml | 4 +- src/com/lang/LanguageDetector.java | 2 + src/com/main/AppConfiguration.java | 2 + src/com/main/Dictionary.java | 64 +- src/com/main/Main.java | 2 +- src/com/main/ResourceLoader.java | 1 + src/com/main/TextSource.java | 20 +- src/com/main/Translator.java | 25 +- 10 files changed, 1169 insertions(+), 172 deletions(-) diff --git a/.gitignore b/.gitignore index bfa6a22..2ca22b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +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/workspace.xml b/.idea/workspace.xml index 63be2d0..e4fc55f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,18 @@ - + + + + + + + + + + + + @@ -62,84 +73,12 @@ - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -152,6 +91,9 @@ + + - @@ -210,9 +153,6 @@ - - - @@ -227,7 +167,7 @@ @@ -237,7 +177,7 @@ + + + @@ -288,7 +245,7 @@ - + @@ -302,6 +259,7 @@ + @@ -342,6 +300,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -387,6 +567,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -423,19 +667,58 @@ + + + + + + + + - + + + + + + + + - + - + + @@ -457,45 +740,49 @@ 1373997660018 + - - - + + + + + - + + - + - + - + + - + - - - + @@ -507,7 +794,9 @@ - + + @@ -515,6 +804,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -544,7 +903,8 @@ - + + @@ -577,7 +937,10 @@ - + + + + @@ -616,82 +979,650 @@ - + - - + + - + - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + + + + - + - - - + + + + + + - + - - - + + + + + + + + + + + + + + + + + - - - + + + + + + - + - - + + - + + - + + + + + + + + + + + + + + + + - + - + - - + + @@ -736,7 +1667,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..11e57b2 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; diff --git a/src/com/main/AppConfiguration.java b/src/com/main/AppConfiguration.java index f3e3f0f..fdb000a 100644 --- a/src/com/main/AppConfiguration.java +++ b/src/com/main/AppConfiguration.java @@ -1,10 +1,12 @@ 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..ee04daa 100644 --- a/src/com/main/Dictionary.java +++ b/src/com/main/Dictionary.java @@ -8,26 +8,50 @@ import java.util.List; import java.util.Map; + +@Component 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..8ef14d1 100644 --- a/src/com/main/Main.java +++ b/src/com/main/Main.java @@ -10,7 +10,7 @@ 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/sergei/translate.txt"); System.out.println(translation); } } diff --git a/src/com/main/ResourceLoader.java b/src/com/main/ResourceLoader.java index 02a2435..afe471d 100644 --- a/src/com/main/ResourceLoader.java +++ b/src/com/main/ResourceLoader.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.List; +@Component 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..cb065dd 100644 --- a/src/com/main/Translator.java +++ b/src/com/main/Translator.java @@ -1,14 +1,31 @@ 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; - } + @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(); + } } From e05bb486ccef2f1b4f02e6c9d2b5ad95cc2552f5 Mon Sep 17 00:00:00 2001 From: SergeiDev Date: Tue, 19 Jan 2016 10:50:05 +0200 Subject: [PATCH 3/3] done hw11 --- .idea/misc.xml | 20 +- .idea/workspace.xml | 647 ++++++++--------------------- src/com/lang/LanguageDetector.java | 1 + src/com/main/AppConfiguration.java | 6 +- src/com/main/Dictionary.java | 8 +- src/com/main/Main.java | 4 +- src/com/main/ResourceLoader.java | 3 +- src/com/main/Translator.java | 4 +- 8 files changed, 190 insertions(+), 503 deletions(-) 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/workspace.xml b/.idea/workspace.xml index e4fc55f..d341c7b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,12 @@ - + - - - @@ -74,12 +71,14 @@ - - + + - - - + + + + + @@ -104,14 +103,14 @@ @@ -120,6 +119,13 @@ true + + + @@ -250,7 +270,6 @@ - @@ -696,6 +715,11 @@ + + + + + @@ -732,6 +756,9 @@ + + + @@ -741,34 +768,35 @@ + + - - + - + - - + - + - + - + @@ -779,6 +807,7 @@ + @@ -789,6 +818,9 @@ + + + + + + + + + + + + + + + + + + + + - + + + @@ -816,7 +869,12 @@ - + + + + + + @@ -833,8 +891,8 @@ - - + + @@ -843,7 +901,10 @@ - + + + + @@ -851,7 +912,10 @@ - + + + + @@ -859,7 +923,12 @@ - + + + + + + @@ -868,8 +937,8 @@ - - + + @@ -878,7 +947,9 @@ - + + + @@ -886,7 +957,12 @@ - + + + + + + @@ -903,8 +979,8 @@ - - + + @@ -913,7 +989,10 @@ - + + + + @@ -921,7 +1000,10 @@ - + + + + @@ -929,7 +1011,12 @@ - + + + + + + @@ -938,8 +1025,8 @@ - - + + @@ -990,7 +1077,6 @@ - @@ -998,7 +1084,6 @@ - @@ -1006,223 +1091,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1293,91 +1161,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1451,179 +1234,101 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - + + - + - - - - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + + + - + - - + + - - - - - - + + - + - - - + + + + + + + + - + - - - + + + + + diff --git a/src/com/lang/LanguageDetector.java b/src/com/lang/LanguageDetector.java index 11e57b2..6ae43be 100644 --- a/src/com/lang/LanguageDetector.java +++ b/src/com/lang/LanguageDetector.java @@ -22,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 fdb000a..5bbf79a 100644 --- a/src/com/main/AppConfiguration.java +++ b/src/com/main/AppConfiguration.java @@ -1,13 +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 ee04daa..88dbdda 100644 --- a/src/com/main/Dictionary.java +++ b/src/com/main/Dictionary.java @@ -2,20 +2,20 @@ 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; -@Component +@Repository public class Dictionary { @Autowired ResourceLoader resourceLoader; - private Map> dictionaries = new HashMap>(); + private Map> dictionaries = new HashMap<>(); public String translate(String word, Language language) { @@ -37,7 +37,7 @@ private Map getDictionary(Language language) { private Map loadDictionary(Language language) { List load = resourceLoader.load("/home/sergei/GeekHub/HomeTaskWeek11/dict/english.dict"); - Map dictionary = new HashMap(); + Map dictionary = new HashMap<>(); if (language.equals(Language.ENGLISH)) { for (String s : load) { diff --git a/src/com/main/Main.java b/src/com/main/Main.java index 8ef14d1..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("/home/sergei/translate.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 afe471d..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,7 +10,7 @@ import java.util.ArrayList; import java.util.List; -@Component +@Service public class ResourceLoader { public List load(String source) { diff --git a/src/com/main/Translator.java b/src/com/main/Translator.java index cb065dd..8983a56 100644 --- a/src/com/main/Translator.java +++ b/src/com/main/Translator.java @@ -3,11 +3,11 @@ import com.lang.Language; import com.lang.LanguageDetector; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; -@Component +@Service public class Translator { @Autowired