Skip to content

Commit

Permalink
Merge pull request #46 from BartekBanachowicz/Jakub
Browse files Browse the repository at this point in the history
More transformations and doc comments
  • Loading branch information
Jorewin authored Dec 21, 2021
2 parents 101b606 + 4118257 commit 1f255e2
Show file tree
Hide file tree
Showing 26 changed files with 706 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IntelliJ configuration
.idea/

# Maven build directory
target/
31 changes: 22 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>


<groupId>pl.put.poznan</groupId>
<artifactId>transformer</artifactId>
<version>0.2.0-SNAPSHOT</version>

<version>0.3.0-SNAPSHOT</version>


<distributionManagement>
<repository>
<id>github</id>
Expand All @@ -26,10 +27,11 @@
</repository>
</distributionManagement>


<properties>
<java.version>11</java.version>
</properties>


<dependencies>
<dependency>
Expand All @@ -48,12 +50,20 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>


</dependencies>


Expand All @@ -64,11 +74,13 @@
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.2</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -82,22 +94,23 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>


<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/pl/put/poznan/transformer/logic/Capitalize.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Part of the decorator pattern.
* This class makes it possible to convert the result of other text transformations
* by changing first letters of every word to uppercase.
*/
public class Capitalize extends TextDecorator {

/**
* @see TextDecorator#TextDecorator
*/
public Capitalize(Transformer t) { super(t); }

/**
* Returns the result of the conversion.
*/
@Override
public String GetText() {
String [] words = super.GetText().split("(?<=\\s)(?=\\S)");
Expand Down
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();
}

}
62 changes: 62 additions & 0 deletions src/main/java/pl/put/poznan/transformer/logic/Invert.java
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());
}

}
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());
}
}
Loading

0 comments on commit 1f255e2

Please sign in to comment.