Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Woditschka #2

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.idea
24 changes: 22 additions & 2 deletions software-engineer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.drmtx</groupId>
<artifactId>hiring-exerchise</artifactId>
<artifactId>hiring-exercise</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
Expand Down Expand Up @@ -48,6 +48,27 @@
<version>1.4.187</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -69,5 +90,4 @@
</build>



</project>
36 changes: 17 additions & 19 deletions software-engineer/src/main/java/com/drmtx/app/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -15,30 +14,29 @@

@EnableAutoConfiguration
@SpringBootApplication
@EntityScan(basePackageClasses=Application.class)
@Configuration
public class Application {

private static final Logger _logger = LogManager.getLogger(Application.class);
private static final Logger _logger = LogManager.getLogger(Application.class);

@Bean
protected ServletContextListener listener() {
return new ServletContextListener() {
@Bean
protected ServletContextListener listener() {
return new ServletContextListener() {

@Override
public void contextInitialized(ServletContextEvent sce) {
_logger.info("ServletContext initialized");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
_logger.info("ServletContext initialized");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
_logger.info("ServletContext destroyed");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
_logger.info("ServletContext destroyed");
}

};
}
};
}

public static void main(String[] args) throws Exception {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
}
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.drmtx.app.domain;

import com.drmtx.app.domain.base.EntityBase;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "word_frequency")
public class WordFrequency extends EntityBase {

@Column(name = "request_id", nullable = true)
private String requestId;

@Column(name = "term", nullable = true)
private String term;

@Column(name = "term_count", nullable = true)
private int termCount;

public WordFrequency() {
}

public WordFrequency(String requestId, String term, int termCount) {
this.requestId = requestId;
this.term = term;
this.termCount = termCount;
}

public String getRequestId() {
return requestId;
}

public void setRequestId(String requestId) {
this.requestId = requestId;
}

public String getTerm() {
return term;
}

public void setTerm(String term) {
this.term = term;
}

public int getTermCount() {
return termCount;
}

public void setTermCount(int termCount) {
this.termCount = termCount;
}

@Override
public String toString() {
return "WordFrequency{" +
"requestId='" + requestId + '\'' +
", term='" + term + '\'' +
", termCount=" + termCount +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.drmtx.app.domain;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;

/**
* Analyze the word frequency.
* <pre>
* - All whitespace characters should be ignored
* - All words should be converted to lower case
* - Following punctuation characters should be ignored: !,.?
* </pre>
*/
public class WordFrequencyAnalyzer {

/**
* Analyze a text for word frequencies.
*
* @param text the text to analyze
* @return the word frequencies
*/
public Map<String, Integer> analyze(String text) {

// check for empty input
if (StringUtils.isBlank(text)) {
return new HashMap<>();
}

// split the text
List<String> words = Arrays.asList(text.toLowerCase().split("[\\s.,?\\t\\n\\r]"));

// calculate frequencies
return words.stream()
.filter(StringUtils::isNotBlank)
.collect(groupingBy(identity(), summingInt(e -> 1)));
}

/**
* Analyze multiple texts for word frequencies.
*
* @param texts the texts to analyze
* @param requestId the requestId
* @return the word frequencies
*/
public List<WordFrequency> analyze(List<String> texts, String requestId) {
// merge all texts
String allTexts = texts.stream()
.collect(joining(" "));

// analyze word frequencies
Map<String, Integer> frequencies = analyze(allTexts);

// map result to model
return frequencies.entrySet().stream()
.map(e -> new WordFrequency(requestId, e.getKey(), e.getValue()))
.collect(toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.drmtx.app.domain.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.util.Date;

/**
* Entity base class to handle technical details of id and versioning.
*/
@MappedSuperclass
public abstract class EntityBase {

@Transient
protected Logger logger = LoggerFactory.getLogger(getClass());

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "creation_ts", nullable = false)
private Date creationTS;

@Version
@Column(name = "version")
private Integer version;

protected EntityBase() {
creationTS = new Date();
}

protected EntityBase(Date creationTS) {
this.creationTS = creationTS;
}

protected void setId(final Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public Date getCreationTS() {
return creationTS;
}

public void setCreationTS(Date createdAt) {
this.creationTS = createdAt;
}

protected void setVersion(final Integer version) {
this.version = version;
}

public Integer getVersion() {
return version;
}

public boolean isNew() {
return null == getId();
}

@Override
public boolean equals(Object obj) {

if (null == obj) {
return false;
}

if (this == obj) {
return true;
}

if (!getClass().equals(obj.getClass())) {
return false;
}

EntityBase that = (EntityBase) obj;

return null != this.getId() && this.getId().equals(that.getId());
}

@Override
public int hashCode() {
int hashCode = 17;
hashCode += null == getId() ? 0 : getId().hashCode() * 31;
return hashCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.drmtx.app.domain.reddit;

import java.util.ArrayList;
import java.util.List;

/**
* Model root to access all comments from the reddit comments endpoint.
*/
public class RedditComment {
private RedditCommentNode[] commentNodes;

public RedditComment(RedditCommentNode... commentNodes) {
this.commentNodes = commentNodes;
}

/**
* Get comment bodies of all nodes, child and reply nodes.
*
* @return all comment bodies.
*/
public List<String> bodies() {
List<String> bodies = new ArrayList<>();
for (RedditCommentNode commentNode : commentNodes) {
bodies.addAll(commentNode.bodies());
}
return bodies;
}
}
Loading