-
Notifications
You must be signed in to change notification settings - Fork 49
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
W4 #209
Open
enginer
wants to merge
2
commits into
devstarter:master
Choose a base branch
from
drgremlin:w4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
W4 #209
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/ayfaar/app/utils/contents/ContentsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.ayfaar.app.utils.contents; | ||
|
||
import org.springframework.stereotype.Component; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Component | ||
public class ContentsUtils { | ||
|
||
public static String filterLengthWordsAfter(String paragraph, String search, int countWordsBeforeAndAfter){ | ||
String wholeFind = ""; | ||
String searchResult = null; | ||
String str = paragraph; | ||
String find = search; | ||
|
||
//check if not the full text | ||
Pattern pattern = Pattern.compile("\\S*" + find + "\\S*",Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); | ||
Matcher matcher = pattern.matcher(str); | ||
while (matcher.find()) { | ||
wholeFind = matcher.group(); | ||
} | ||
|
||
//create minimal string | ||
int countWords = countWordsBeforeAndAfter; | ||
String[] sp = str.split(" +"); // "+" for multiple spaces | ||
List<String> strings = Arrays.asList(sp); | ||
String[] findStringArr = {}; | ||
if (wholeFind.equals("")) findStringArr = find.split(" "); | ||
else findStringArr = wholeFind.split(" "); | ||
|
||
int lengthFindStringArr = findStringArr.length; | ||
int lengthParagraph = sp.length; | ||
String firstPosition = findStringArr[0]; | ||
String lastPosition = findStringArr[lengthFindStringArr - 1]; | ||
int iLast = strings.indexOf(lastPosition); | ||
|
||
for (int i = 0; i < lengthParagraph; i++) { | ||
String addFirstDots = ""; | ||
if (sp[i].equals(firstPosition) && sp[i + lengthFindStringArr-1].equals(lastPosition)) { | ||
|
||
String after = ""; | ||
for (int j = 1; j <= countWords; j++) { | ||
if(iLast+j < lengthParagraph) after += " " + sp[iLast+j]; | ||
} | ||
|
||
after = after.replaceAll("[-+.^:,]$",""); | ||
if(!after.equals(" ") && iLast + countWords < lengthParagraph) after += "..."; | ||
if (!firstPosition.equals(sp[0])) addFirstDots = "..."; | ||
searchResult = addFirstDots + wholeFind + after; | ||
} | ||
} | ||
|
||
return searchResult == null ? "" : searchResult; | ||
} | ||
} | ||
|
72 changes: 72 additions & 0 deletions
72
src/test/java/org/ayfaar/app/utils/contents/ContentsUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.ayfaar.app.utils.contents; | ||
|
||
|
||
import org.ayfaar.app.IntegrationTest; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static junit.framework.Assert.assertNull; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ContentsUtilsTest{ | ||
|
||
@Autowired | ||
ContentsUtils contentsUtils; | ||
|
||
@Test //Test full text | ||
public void filterLengthWordsAfterTest(){ | ||
|
||
String paragraph = "Аспекты Качеств - главная основа Фокусной Динамики."; | ||
|
||
String find = "Аспекты Качеств - главная основа Фокусной Динамики."; | ||
String s = contentsUtils.filterLengthWordsAfter(paragraph, find, 3); | ||
|
||
assertEquals("Аспекты Качеств - главная основа Фокусной Динамики.", s); | ||
} | ||
|
||
@Test //текст из середины, результат должен быть впереди с "..." и сзади плюс три слова и "..." | ||
public void filterLengthWordsAfterTest1(){ | ||
|
||
|
||
String paragraph = "Аспекты Качеств - главная основа Фокусной Динамики."; | ||
|
||
String find = "Качеств"; | ||
String s = contentsUtils.filterLengthWordsAfter(paragraph, find, 3); | ||
|
||
assertEquals("...Качеств - главная основа...", s); | ||
} | ||
|
||
@Test //текст с конца строки, троеточие подставляется только впереди строки | ||
public void filterLengthWordsAfterTest2(){ | ||
|
||
|
||
String paragraph = "Аспекты Качеств - главная основа Фокусной Динамики."; | ||
|
||
String find = "Фокусной Динамики."; | ||
String s = contentsUtils.filterLengthWordsAfter(paragraph, find, 3); | ||
|
||
assertEquals("...Фокусной Динамики.", s); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тоже самое |
||
} | ||
|
||
@Test //текст с начала строки, в результат дописывается еще три слова плюс троеточие подставляется только вконце строки | ||
public void filterLengthWordsAfterTest3(){ | ||
|
||
String paragraph = "Аспекты Качеств - главная основа Фокусной Динамики."; | ||
|
||
String find = "Аспекты"; | ||
String s = contentsUtils.filterLengthWordsAfter(paragraph, find, 3); | ||
|
||
assertEquals("Аспекты Качеств - главная...", s); | ||
} | ||
|
||
@Test //поиск не существующего текста, должен выдавать пустую строку | ||
public void filterLengthWordsAfterTest4(){ | ||
|
||
String paragraph = "Аспекты Качеств - главная основа Фокусной Динамики."; | ||
|
||
String find = "sdlkdfsfsdfdsdfjs"; | ||
String s = contentsUtils.filterLengthWordsAfter(paragraph, find, 3); | ||
|
||
assertEquals("", s); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В этом случае я ожидаю увидеть:
"Аспекты Качеств - главная основа Фокусной Динамики" так как общая сумма слов не привышает 7