Skip to content

Instantly share code, notes, and snippets.

@romcter
Created June 9, 2020 11:45
Show Gist options
  • Save romcter/7aaa1cdf810ea3c65b40da031860ac53 to your computer and use it in GitHub Desktop.
Save romcter/7aaa1cdf810ea3c65b40da031860ac53 to your computer and use it in GitHub Desktop.
package Colections.HashMap;
import java.io.*;
import java.util.HashMap;
import java.util.Scanner;
public class Dictionary {
public Dictionary() {
}
private HashMap<String, String> diction = new HashMap<>();
private String str;
public void addInDiction() {
System.out.println("Do you want to add your words?(1.Yes/2.No)");
Scanner scnForInt = new Scanner(System.in);
Scanner scnForText = new Scanner(System.in);
int choise = scnForInt.nextInt();
if (choise == 1) {
while (true) {
System.out.println("Enter english word below.");
String key = scnForText.nextLine();
System.out.println("Enter translation.");
String content = scnForText.nextLine();
if (key != null && content != null) {
diction.put(key, content);
System.out.println("Done!");
}
System.out.println("Do you want to add another word?(1.Yes/2.No)");
int continued = scnForInt.nextInt();
if (continued == 2) break;
}
}
scnForInt.close();
scnForText.close();
diction.put("very", "очень");
diction.put("have", "имею");
diction.put("big", "большой");
diction.put("hello", "привет");
diction.put("world", "мир");
diction.put("I", "я");
diction.put("man", "мужчина");
diction.put("women", "женщина");
diction.put("brother", "брат");
diction.put("sister", "сестра");
diction.put("daughter", "дочь");
diction.put("wife", "жена");
diction.put("salary", "зарплата");
diction.put("father", "отец");
diction.put("bad", "плохой");
diction.put("sad", "грусный");
diction.put("mad", "сумашедший");
diction.put("wise", "мудрый");
diction.put("nice", "отлично");
diction.put("great", "велеколепно");
diction.put("crazy", "безумный");
diction.put("everything", "всё");
diction.put("because", "потому что");
diction.put("lot", "много");
diction.put("translate", "переводить");
diction.put("program", "программа");
diction.put("digits", "цифра");
diction.put("number", "число");
diction.put("table", "стол");
diction.put("cheer", "ура");
diction.put("love", "люблю");
diction.put("my", "моё");
diction.put("to", "в");
diction.put("too", "слишком(также)");
}
public void readFile() {
try (BufferedReader br = new BufferedReader(new FileReader("/Users/macbookpro/IdeaProjects/Pror.kiev.ua/src/Colections/HashMap/Text"))) {
while (true) {
String st = br.readLine();
if (st == null) break;
str = st;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void translate() {
String[] mass = str.split("[ -,;:.!?№%()]");
String res = "";
for (String st : mass) {
String test = diction.get(st.toLowerCase());
if (test != null) res += test + " ";
else res += st + " ";
}
System.out.println(res);
try (BufferedWriter BR = new BufferedWriter(new FileWriter("/Users/macbookpro/IdeaProjects/Pror.kiev.ua/src/Colections/HashMap/Ukrainian.out."))) {
BR.write(res);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package Colections.HashMap;
public class Main {
public static void main(String[] args) {
Dictionary D = new Dictionary();
D.addInDiction();
D.readFile();
D.translate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment