fewlaps / mention-detector

Detect @mentions and @sequences @of @mentions in your Strings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Coverage Status

mention-detector

Detect @mentions and @sequence @of @mentions with this fully tested library.

QuitNow!, our main application to help people quit smoking, has an inapp chat to create a beautiful community of ex-smokers. They like to mention each other in the chat, and sometimes, someone mentions lots of people. Everytime the app finds a mention, it creates a span to bold the mention. The bad news are that, sometimes, a message could include more than 10 or 20 mentions, and that's a lot of spans. In that case, the best thing to do is to join those 20 spans in only one, which improves the performance dramatically.

So, we needed a fast way to look for mentions and sequences of mentions. And here is it.

Time to introduce the library!

String text = "Hello! This is a great introduction to @MentionDetector!";
//With MentionDetector you can... well. You can detect mentions!
MentionDetector detector = new MentionDetector(text);

detector.hasMentions(); //Returns if the text has any mention
detector.getMentions(); //Returns a list with the mention to @MentionDetector

Mention mention = detector.getMentions().get(0); //If we get a mention, we could ask:
mention.start(); //Where the mention starts? At 39!
mention.end(); //Where the mention ends? At 55!
mention.username(); //What's the mentioned username? @MentionDetector
mention.usernameWithoutAtSymbol(); //And what's the actual username? MentionDetector

//In addition, you can also get the mention sequences.
detector.getSequences();

//A sequence is a list of mentions only separated by spaces. For example:
String fourMentions = "Hello! I love @trains @bananas @pizza and @sushi";
//In this case, "@trains @bananas @pizza" is one sequence, and "@sushi" is another one.

Sequence sequence = new MentionDetector(fourMentions).getSequences().get(0);
sequence.start(); //Where the sequence starts? At 14!
sequence.end(); //Where the sequence ends? At 37!

//Now, you can create one StyleSpan instead of three. And that's great for performance!

Warning! Users are mad

We're using this library at QuitNow! to highlight when a user mentions to another one in a in-app chat. The thing is that users are crazy. They take the stability of the library to the next step. So, if you plan to detect mentions in a String generated by a human, don't hesitate to wrap it in a lovely try-catch. In addition, if you can open an issue everytime you find a pattern, we will reproduce it with a test and fix it. Or, if you feel confortable launching us a Pull Request with the test and the solution, it will be impressive!

Download

repositories { jcenter() }
    
compile 'com.fewlaps.mentiondetector:mention-detector:1.2.4'
  • Grab via Maven:
<repository>
  <id>jcenter</id>
  <url>http://jcenter.bintray.com</url>
</repository>

<dependency>
    <groupId>com.fewlaps.mentiondetector</groupId>
    <artifactId>mention-detector</artifactId>
    <version>1.2.4</version>
</dependency>

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Fewlaps

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Detect @mentions and @sequences @of @mentions in your Strings

License:MIT License


Languages

Language:Java 100.0%