I am working on discovering sentences that begin and end with hashtags. At the moment, I only have the code to find the words that are part of this mechanism. How to find offers depending on the case below.
Case 1:
Hello,
In this case, I want to discover how are you. Now, if there is only a word, then this case should be ignored.
Case 2:
Hello,
In this case, only words #howand #todayare found that I already have. No suggestions here, as words do not end with a hashtag.
code:
@Override
public List<String> findHashTags(String text){
if(text == null){
return new ArrayList<>();
}
String[] tagSet = text.split(" ");
Set<String> sortedTags = new HashSet<>();
List<String> processedTags = new ArrayList<>();
for(String tags : tagSet){
if(tags.startsWith("#")){
sortedTags.add(tags);
}
}
processedTags.addAll(sortedTags);
return processedTags;
}
Updated Code
@Override
public List<String> findHashTags(String text){
if(text == null){
return new ArrayList<>();
}
Set<String> sortedTags = new HashSet<>();
List<String> processedTags = new ArrayList<>();
Pattern pattern = Pattern.compile("#\\b.*?\\b#|\\B#\\w+");
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
String outString = matcher.group();
outString = outString.replace("#","");
outString = outString.replace(",","");
sortedTags.add(outString);
}
processedTags.addAll(sortedTags);
return processedTags;
}
source
share