-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from BartekBanachowicz/Jakub
More transformations and doc comments
- Loading branch information
Showing
26 changed files
with
706 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/pl/put/poznan/transformer/logic/FindInDictionary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package pl.put.poznan.transformer.logic; | ||
|
||
import com.google.common.collect.BiMap; | ||
import com.google.common.collect.HashBiMap; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class FindInDictionary { | ||
private final BiMap <String, String> dictionary = HashBiMap.create(); | ||
|
||
private void load(){ | ||
String fileName = "src/main/resources/dictionary.csv"; | ||
Path pathToFile = Paths.get(fileName); | ||
String line; | ||
String[] elements; | ||
|
||
try(BufferedReader reader = Files.newBufferedReader(pathToFile)){ | ||
line = reader.readLine(); | ||
|
||
while(line != null){ | ||
elements = line.split(","); | ||
dictionary.put(elements[0], elements[1]); | ||
line = reader.readLine(); | ||
} | ||
|
||
} catch (IOException ioException){ | ||
ioException.printStackTrace(); | ||
} | ||
} | ||
|
||
public String findWordAndReplace(String shortcut){ | ||
String result = shortcut; | ||
if(dictionary.containsKey(shortcut)){ | ||
result = dictionary.get(shortcut); | ||
} | ||
return result; | ||
} | ||
|
||
public String findShortcutAndReplace(String word){ | ||
String result = word; | ||
if(dictionary.inverse().containsKey(word)){ | ||
result = dictionary.inverse().get(word); | ||
} | ||
return result; | ||
} | ||
|
||
public String getWord(String shortcut){ | ||
return dictionary.get(shortcut); | ||
} | ||
|
||
public String getShortcut(String word){ | ||
return dictionary.inverse().get(word); | ||
} | ||
|
||
public String[] getListOfShortcuts(){ | ||
return dictionary.keySet().toArray(new String[0]); | ||
} | ||
|
||
public String[] getListOfWords(){ | ||
return dictionary.inverse().keySet().toArray(new String[0]); | ||
} | ||
|
||
public FindInDictionary(){ | ||
this.load(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pl.put.poznan.transformer.logic; | ||
|
||
import static java.lang.Character.isUpperCase; | ||
import static java.lang.Character.toUpperCase; | ||
|
||
public class Invert extends TextDecorator{ | ||
|
||
public Boolean[] findUpperLetters(String text){ | ||
Boolean[] upperLettersPositions = new Boolean[text.length()]; | ||
|
||
for(int i=0; i<text.length();i++){ | ||
upperLettersPositions[i] = isUpperCase(text.charAt(i)); | ||
} | ||
|
||
return upperLettersPositions; | ||
} | ||
|
||
private String makeLettersGreatAgain(String text, Boolean[] upperLettersPositions){ | ||
StringBuilder result = new StringBuilder(); | ||
|
||
for(int i=0; i<text.length(); i++){ | ||
if(upperLettersPositions[i]){ | ||
result.append(toUpperCase(text.charAt(i))); | ||
|
||
} else { | ||
result.append(text.charAt(i)); | ||
} | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
private String changeOrder(String text){ | ||
StringBuilder result = new StringBuilder(); | ||
|
||
for(int i=text.length()-1; i>=0; i--){ | ||
result.append(text.charAt(i)); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
private String reverse(String text){ | ||
String result; | ||
|
||
result = text.toLowerCase(); | ||
result = changeOrder(result); | ||
result = makeLettersGreatAgain(result,findUpperLetters(text)); | ||
|
||
return result; | ||
} | ||
|
||
public Invert(Transformer t){ | ||
super(t); | ||
} | ||
|
||
@Override | ||
public String GetText() { | ||
return reverse(super.GetText()); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/pl/put/poznan/transformer/logic/ReplaceFullWords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package pl.put.poznan.transformer.logic; | ||
|
||
import static pl.put.poznan.transformer.logic.TextTransformer.dictionary; | ||
|
||
public class ReplaceFullWords extends TextDecorator { | ||
|
||
|
||
private String replace(String text){ | ||
|
||
String[] keySet = dictionary.getListOfWords(); | ||
|
||
for(String word : keySet){ | ||
if(text.contains(word)) { | ||
text = text.replaceAll(word, dictionary.getShortcut(word)); | ||
} | ||
} | ||
|
||
return text; | ||
} | ||
|
||
public ReplaceFullWords(Transformer t) {super(t);} | ||
|
||
@Override | ||
public String GetText(){ | ||
return replace(super.GetText()); | ||
} | ||
} |
Oops, something went wrong.