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
16 changes: 8 additions & 8 deletions Spring.iml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration>
<fileset id="fileset1" name="XML Application Context" removed="false">
<file>file://$MODULE_DIR$/src/application-context.xml</file>
<fileset id="fileset" name="Spring Application Context" removed="false">
<file>file://$MODULE_DIR$/src/com/main/AppConfiguration.java</file>
</fileset>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="spring-beans-3.2.2.RELEASE" level="project" />
<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-beans-4.2.4.RELEASE" level="project" />
<orderEntry type="library" name="spring-core-4.2.4.RELEASE" level="project" />
<orderEntry type="library" name="spring-expression-4.2.4.RELEASE" level="project" />
<orderEntry type="library" name="spring-aop-4.2.4.RELEASE" level="project" />
</component>
</module>

</module>
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<>();
}
}
29 changes: 27 additions & 2 deletions src/com/main/AppConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
package com.main;

import com.lang.LanguageDetector;
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
@Bean
public ResourceLoader resourceLoader() {
return new ResourceLoader();
}

@Bean
public LanguageDetector languageDetector() {
return new LanguageDetector();
}

@Bean
public TextSource textSource() {
return new TextSource();
}

@Bean
public Dictionary dictionary() {
return new Dictionary();
}

@Bean
public Translator translator() {
return new Translator();
}
}
48 changes: 40 additions & 8 deletions src/com/main/Dictionary.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
package com.main;

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

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

@Component
public class Dictionary {

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

public String translate(String word, Language language) {
//TODO: Implement me
return null;
public String translate(String word, Language language) {
String translation = getDictionary(language).get(word.toLowerCase());
return toCapital(translation, word);
}

private String toCapital(String translation, String origin) {
if ((translation == null)
|| (origin == null)
|| (origin.length() < 1)
|| (translation.length() < 1)
|| (Character.isLowerCase(origin.charAt(0)))) {
return translation;
}
if (translation.length() == 1) {
return translation.toUpperCase();
}
return translation.substring(0, 1).toUpperCase() + translation.substring(1);
}

private Map<String, String> getDictionary(Language language) {
Map<String, String> dictionary = dictionaries.get(language);
if (null == dictionary) {
Expand All @@ -27,7 +46,20 @@ private Map<String, String> getDictionary(Language language) {
}

private Map<String, String> loadDictionary(Language language) {
//TODO: Implement me
return null;
// List<String> dictionary = resourceLoader.load(System.getProperty("user.dir") + "\\dict\\english.dict");
Properties properties = new Properties();
String propertyFile = System.getProperty("user.dir") + "\\dict\\" + language.toString().toLowerCase() + ".dict";
try (InputStreamReader inputStream = new InputStreamReader(
new FileInputStream(propertyFile), Charset.defaultCharset())) {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return properties.entrySet()
.stream()
.collect(Collectors.toMap(
entry -> String.valueOf(entry.getKey()),
entry -> String.valueOf(entry.getValue())
));
}
}
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<>();
}
}
18 changes: 14 additions & 4 deletions src/com/main/TextSource.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.main;

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

import java.util.stream.Collectors;

@Component
public class TextSource {

public String getText(String path) {
//TODO: Implement me
return null;
@Autowired
private ResourceLoader resourceLoader;

public String getText(String path) {
return resourceLoader.load(path)
.stream()
.collect(Collectors.joining(" "));
}
}
}
37 changes: 32 additions & 5 deletions src/com/main/Translator.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
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;
import java.util.Arrays;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Component
public class Translator {

public String translate(String source) {
//TODO: Implement me
return null;
}
@Autowired
private LanguageDetector languageDetector;

@Autowired
private TextSource textSource;

@Autowired
private Dictionary dictionary;

public static final Pattern wordSplitPattern = Pattern.compile("[^a-zA-Z]+", Pattern.DOTALL);

public String translate(String source) {
String text = textSource.getText(source);
Language language = languageDetector.detectLanguage(text);
Set<String> words = Arrays
.stream(wordSplitPattern.split(text))
.collect(Collectors.toSet());
for (String word: words) {
String translation = dictionary.translate(word, language);
if (translation != null) {
text = text.replaceAll("\\b" + word + "\\b", translation);
}
}
return text;
}
}