Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Shortener {
- private Map<String, String> shortningRules;
- /*
- * Default constructor that will load a default abbreviations text file.
- */
- public Shortener(FileReader reader) {
- this.shortningRules = new HashMap<>();
- parseInput(reader);
- }
- public Shortener(Map<String, String> shortningRules) {
- this.shortningRules = shortningRules;
- }
- private void parseInput(FileReader reader) {
- try (BufferedReader br = new BufferedReader(reader)) {
- String line;
- while ((line = br.readLine()) != null) {
- String[] lineComponents = line.split(",");
- this.shortningRules.put(lineComponents[0], lineComponents[1]);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void shortenMessage(String message) {
- for (Entry<String, String> entry : shortningRules.entrySet()) {
- message = message.replaceAll("\\b" + entry.getKey() + "\\b", entry.getValue());
- }
- System.out.println(message);
- }
- public static void main(String[] args) {
- Map<String, String> rules = new HashMap<String, String>() {{
- put("hello", "lo");
- put("any", "ne");
- put("anyone", "ne1");
- put("thanks", "thx");
- }};
- Shortener shortener = new Shortener(rules);
- String msg = "hello? any anyone thanks!";
- shortener.shortenMessage(msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment