Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Mar 11, 2024
0 parents commit 6bdf629
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: JUNIT Test

on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }} # location for the settings.xml file
- name: Build
run: mvn clean -B package --file pom.xml
- name: Test
run: mvn test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
target/*
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# MentionExtractor

This Kotlin class is responsible for extracting mentions from a text based on specified patterns.
It provides flexible configuration options for handling mentions in a text.

## Usage

To use the `MentionExtractor` class:

1. Create a `MentionPattern` instance and configure it according to your requirements.

2. Use the `MentionExtractor.Builder` to construct an instance of `MentionExtractor`, providing the configured `MentionPattern` and any additional settings such as the maximum number of mentions to handle.

3. Use the methods provided by `MentionExtractor` to extract mentions from text, check if a text contains a mention, or count the number of mentions in a text.

Here's an example of how to use `MentionExtractor`:

```kotlin
// Configure MentionPattern
val mentionPattern = MentionPattern().withSpecialChars()

// Create MentionExtractor instance
val extractor = MentionExtractor.Builder()
.pattern(mentionPattern)
.maxMentions(5)
.build()

// Extract mentions from text
val text = "Hey @user1, did you see @user2's message?"
val mentions = extractor.fromString(text)

// Check if text contains a mention
val containsMention = extractor.containsMention(text)

// Count the number of mentions in text
val mentionCount = extractor.countMentions(text)
```

## Testing

We use JUnit 5 for testing the `MentionExtractor`. To run the tests:

1. Ensure you have Maven installed on your system.

2. Navigate to the root directory of the project containing the `pom.xml` file.

3. Run the following Maven command:

```bash
mvn test
```

This will execute the JUnit tests defined in the `test` package and provide the test results.

## License

This project is licensed under the Apache 2.0 License—see the [LICENSE](LICENSE) file for details.
93 changes: 93 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.brenoepics</groupId>
<artifactId>MentionExtractor</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.9.23</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
119 changes: 119 additions & 0 deletions src/main/java/io/github/brenoepics/MentionExtractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.github.brenoepics

import java.util.regex.Pattern

/**
* This class is responsible for handling mentions in a text. It uses a pattern to identify mentions
* and has a limit for the maximum number of mentions it can handle.
*/
class MentionExtractor private constructor(builder: Builder) {
private val pattern: Pattern
private val maxMentions: Int

/**
* Private constructor used by the Builder to create an instance of MentionExtractor.
*/
init {
this.pattern = builder.pattern!!.build()
this.maxMentions = builder.maxMentions
}

/**
* This class is responsible for building an instance of MentionExtractor. It uses the builder
* pattern to allow for more readable and flexible construction of MentionExtractor instances.
*/
class Builder {
internal var pattern: MentionPattern? = null
var maxMentions: Int = 5

/**
* Sets the pattern to be used by the MentionExtractor.
*
* @param mentionPattern The pattern to identify mentions.
* @return The builder instance.
*/
fun pattern(mentionPattern: MentionPattern?): Builder {
this.pattern = mentionPattern
return this
}

/**
* Sets the maximum number of mentions that the MentionExtractor can handle.
*
* @param maxMentions The maximum number of mentions.
* @return The builder instance.
*/
fun maxMentions(maxMentions: Int): Builder {
this.maxMentions = maxMentions
return this
}

/**
* Builds an instance of MentionExtractor using the set pattern and maxMentions.
*
* @return A new instance of MentionExtractor.
*/
fun build(): MentionExtractor {
return MentionExtractor(this)
}
}

/**
* Extracts mentions from a given text up to the maximum number of mentions.
*
* @param text The text to extract mentions from.
* @return A set of mentions.
*/
fun fromString(text: String?): Set<String> {
return fromString(text, maxMentions)
}

/**
* Extracts mentions from a given text up to the maximum number of mentions.
*
* @param text The text to extract mentions from.
* @param maxMentions The maximum number of mentions to extract.
* @return A set of mentions.
*/
fun fromString(text: String?, maxMentions: Int): Set<String> {
val mentioned: MutableSet<String> = HashSet()
val matcher = text?.let { pattern.matcher(it) }
if (matcher != null) {
while (matcher.find() && mentioned.size < maxMentions) {
mentioned.add(matcher.group(1))
}
}
return mentioned
}

/**
* Checks if a given text contains a mention.
*
* @param text The text to check for mentions.
* @return True if the text contains a mention, false otherwise
*/
fun containsMention(text: String?): Boolean {
val matcher = text?.let { pattern.matcher(it) }
if (matcher != null) {
return matcher.find()
}
return false
}

/**
* Counts the number of mentions in a given text.
*
* @param text The text to count mentions in.
* @return The number of mentions in the text.
*/
fun countMentions(text: String?): Int {
var count = 0
val matcher = text?.let { pattern.matcher(it) }
if (matcher != null) {
while (matcher.find()) {
count++
}
}
return count
}
}
88 changes: 88 additions & 0 deletions src/main/java/io/github/brenoepics/MentionPattern.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.github.brenoepics

import java.util.regex.Pattern

/**
* This class is used to build different types of patterns for mentions. A mention is a way to call
* out someone in a conversation, usually prefixed with '@'.
*/
class MentionPattern {
private var pattern: Pattern
/** The default constructor initializes the pattern to match any word character after '@'. */
init {
this.pattern = compile("@(\\w+)")
}

/**
* This method modifies the pattern to include special characters. The pattern will match any
* alphanumeric character or hyphen after '@', but not at the start or end of a word.
*
* @return this MentionPattern instance
*/
fun withSpecialChars(): MentionPattern {
this.pattern = compile("@([A-Za-z0-9._%-]+)")
return this
}

/**
* This method modifies the pattern to match email-like mentions. The pattern will match any
* string that looks like an email address after '@'.
*
* @return this MentionPattern instance
*/
fun withEmailLike(): MentionPattern {
this.pattern = compile("@([A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4})")
return this
}

/**
* This method modifies the pattern to include underscores. The pattern will match any
* alphanumeric character or underscore after '@'.
*
* @return this MentionPattern instance
*/
fun withUnderscores(): MentionPattern {
this.pattern = compile("@([A-Za-z0-9_]+)")
return this
}

/**
* This method resets the pattern to the default. The pattern will match any word character after
* '@'.
*
* @return this MentionPattern instance
*/
fun defaultPattern(): MentionPattern {
this.pattern = compile("@(\\w+)")
return this
}

/**
* This method sets a custom pattern for the MentionPattern instance.
*
* @param pattern the custom pattern to be used
* @return this MentionPattern instance
*/
fun customPattern(pattern: String): MentionPattern {
this.pattern = compile(pattern)
return this
}

/**
* This method sets the pattern to be case-sensitive.
*
* @return this MentionPattern instance
*/
private fun compile(pattern: String): Pattern {
return Pattern.compile(pattern)
}

/**
* This method builds and returns the current pattern.
*
* @return the current pattern
*/
fun build(): Pattern {
return this.pattern
}
}
Loading

0 comments on commit 6bdf629

Please sign in to comment.