Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

W4 #209

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ayfaar.app.controllers;

import lombok.extern.slf4j.Slf4j;
import one.util.streamex.StreamEx;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.ayfaar.app.dao.TermDao;
Expand All @@ -12,6 +13,7 @@
import org.ayfaar.app.utils.ContentsService;
import org.ayfaar.app.utils.TermService;
import org.ayfaar.app.utils.UriGenerator;
import org.ayfaar.app.utils.contents.ContentsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -28,6 +30,7 @@
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.UNICODE_CASE;

@Slf4j
@RestController
@RequestMapping("api/suggestions")
public class NewSuggestionsController {
Expand All @@ -39,9 +42,11 @@ public class NewSuggestionsController {
@Autowired VideoResourceService videoResourceService;
@Autowired RecordService recordService;
@Autowired ItemService itemService;
@Autowired ContentsUtils contentsUtils;

private List<String> escapeChars = Arrays.asList("(", ")", "[", "]", "{", "}");
private static final int MAX_SUGGESTIONS = 5;
private static final int MAX_WORDS_PARAGRAPH_AFTER_SEARCH = 4;

public Map<String, String> suggestions(String q) {
return suggestions(q, false, false, false, false, false, false, false, false, false);
Expand Down Expand Up @@ -74,7 +79,13 @@ public Map<String, String> suggestions(@RequestParam String q,
for (Suggestions item : items) {
Queue<String> queriesQueue = getQueue(q);
for (Map.Entry<String, String> suggestion : getSuggestions(queriesQueue, item)) {
allSuggestions.put(suggestion.getKey(), suggestion.getValue());
String key = suggestion.getKey();
String value = suggestion.getValue();
if(key.contains("ии:пункты:")) {
String suggestionParagraph = contentsUtils.filterLengthWordsAfter(value, q, MAX_WORDS_PARAGRAPH_AFTER_SEARCH);
if(suggestionParagraph != "")allSuggestions.put(key, key.substring(10) + ":" + suggestionParagraph);
}
else allSuggestions.put(key, value);
}
}
return allSuggestions;
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/org/ayfaar/app/utils/contents/ContentsUtils.java
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 src/test/java/org/ayfaar/app/utils/contents/ContentsUtilsTest.java
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В этом случае я ожидаю увидеть:
"Аспекты Качеств - главная основа Фокусной Динамики" так как общая сумма слов не привышает 7

}

@Test //текст с конца строки, троеточие подставляется только впереди строки
public void filterLengthWordsAfterTest2(){


String paragraph = "Аспекты Качеств - главная основа Фокусной Динамики.";

String find = "Фокусной Динамики.";
String s = contentsUtils.filterLengthWordsAfter(paragraph, find, 3);

assertEquals("...Фокусной Динамики.", s);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}