Fixes Bug #17844 Hashtag does not support numbers e.g. #4.15.0

Fixes Bug #17811 Avoid # being considered an hastagged topic
This commit is contained in:
Massimiliano Assante 2019-12-06 15:17:29 +01:00
parent 2c0614a7d0
commit 004db5a034
3 changed files with 68 additions and 46 deletions

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.socialnetworking</groupId>
<artifactId>social-util-library</artifactId>
<version>1.7.1</version>
<version>1.7.2-SNAPSHOT</version>
<name>social-util-library</name>
<description>
The social-util-library contains utility functions that can be used by the social-networking portlets.

View File

@ -6,10 +6,10 @@ import java.util.regex.Pattern;
public class SanitizedHashTag {
private static final String RECOGNIZE_HASHTAG_REGEX = "^.{0,3}#[\\w-]*[\\W]{0,3}";
private static final String RECOGNIZE_HASHTAG_REGEX = "^.{0,3}#[\\w.-]{1,}[\\W]{0,3}";
private static final Pattern RECOGNIZE_HASHTAG_PATTERN;
private static final String HASHTAG_REGEX = "#[\\w-]*";
private static final String HASHTAG_REGEX = "#[\\w.-]{1,}";
private static final Pattern HASHTAG_PATTERN;
static {
@ -41,6 +41,11 @@ public class SanitizedHashTag {
prefix = string.substring(0,matcher.start());
hashTag = string.substring(matcher.start(), matcher.end());
postfix = string.substring(matcher.end());
if (hashTag.endsWith(".")) {
hashTag = hashTag.substring(0, hashTag.length()-1);
postfix += ".";
}
}else {
throw new IllegalArgumentException(string + " is not a valid TAG");
}

View File

@ -137,6 +137,22 @@ public class MessageParserTest {
logger.debug(messageParser.getParsedMessage());
}
@Test
public void checkHashTag7() throws Exception {
String text = "Checking hashtag between quotes #hashtag1; #hashtag1, #hashtag3. \"#hashtag5\" is recognized (#anotherHashtag) #4. #4.12 # ";
SocialMessageParser messageParser = new SocialMessageParser(text);
logger.debug(messageParser.getParsedMessage());
List<String> htags = messageParser.getHashtags();
for (String hTag : htags) {
logger.debug("found hashtag:"+hTag);
}
logger.debug("messageParser.getHashtags().size() should be 7, is: "+ htags.size());
Assert.assertTrue(htags.size() == 7);
}
@Test
public void checkHashTag() throws Exception {
String token = "\"#hashtag\"";
@ -173,6 +189,7 @@ public class MessageParserTest {
Assert.assertTrue(sanitizedHashTag.getPostfix().compareTo("\");")==0);
}
@Test(expected=IllegalArgumentException.class)
public void checkHashTag4() throws Exception {
String token = ";(\"#hashtag\");]";