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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions .idea/libraries/spring_expression_3_2_2_RELEASE1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 1 addition & 19 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

924 changes: 780 additions & 144 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Spring.iml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
<orderEntry type="library" name="spring-core-3.2.2.RELEASE" level="project" />
<orderEntry type="library" name="commons-logging-1.1.1" level="project" />
<orderEntry type="library" name="spring-expression-3.2.2.RELEASE" level="project" />
<orderEntry type="library" name="spring-expression-3.2.2.RELEASE1" level="project" />
</component>
</module>

</module>
3 changes: 3 additions & 0 deletions src/com/lang/LanguageDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Language, List<String>> mapping;
Expand All @@ -20,6 +22,7 @@ public Language detectLanguage(String text) {
return Language.ENGLISH;
}

@PostConstruct
private void initMapping() {
mapping = new HashMap<Language, List<String>>();
}
Expand Down
8 changes: 4 additions & 4 deletions src/com/main/AppConfiguration.java
Original file line number Diff line number Diff line change
@@ -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
}
66 changes: 45 additions & 21 deletions src/com/main/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Language, Map<String, String>> dictionaries = new HashMap<Language, Map<String, String>>();

public String translate(String word, Language language) {
//TODO: Implement me
return null;
}

private Map<String, String> getDictionary(Language language) {
Map<String, String> dictionary = dictionaries.get(language);
if (null == dictionary) {
dictionary = loadDictionary(language);
dictionaries.put(language, dictionary);
}
return dictionary;
}

private Map<String, String> loadDictionary(Language language) {
//TODO: Implement me
return null;
}
@Autowired
ResourceLoader resourceLoader;

private Map<Language, Map<String, String>> 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<String, String> getDictionary(Language language) {
Map<String, String> dictionary = dictionaries.get(language);
if (null == dictionary) {
dictionary = loadDictionary(language);
dictionaries.put(language, dictionary);
}
return dictionary;
}

private Map<String, String> loadDictionary(Language language) {
List<String> load = resourceLoader.load("/home/sergei/GeekHub/HomeTaskWeek11/dict/english.dict");
Map<String, String> 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;
}
}
4 changes: 2 additions & 2 deletions src/com/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/com/main/ResourceLoader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +10,7 @@
import java.util.ArrayList;
import java.util.List;

@Service
public class ResourceLoader {

public List<String> load(String source) {
Expand Down
20 changes: 16 additions & 4 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.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;
}
}
27 changes: 22 additions & 5 deletions src/com/main/Translator.java
Original file line number Diff line number Diff line change
@@ -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();
}
}