|
8 | 8 | import net.b07z.sepia.server.core.data.Answer; |
9 | 9 | import net.b07z.sepia.server.core.data.Language; |
10 | 10 | import net.b07z.sepia.server.core.database.DatabaseInterface; |
| 11 | +import net.b07z.sepia.server.core.tools.Debugger; |
| 12 | +import net.b07z.sepia.server.core.tools.FilesAndStreams; |
11 | 13 |
|
12 | 14 | /** |
13 | | - * Import answers stored in a file to Elasticsearch. |
| 15 | + * Import answers stored in a file to database (usually Elasticsearch). |
14 | 16 | */ |
15 | 17 | public class AnswerImporter { |
| 18 | + |
| 19 | + //Database |
| 20 | + DatabaseInterface database; |
| 21 | + |
| 22 | + /** |
| 23 | + * Construct importer by linking database implementation. |
| 24 | + * @param database - {@link DatabaseInterface} |
| 25 | + */ |
| 26 | + public AnswerImporter(DatabaseInterface database){ |
| 27 | + this.database = database; |
| 28 | + } |
16 | 29 |
|
17 | | - public static void run(File file, Language language, DatabaseInterface es, boolean isMachineTranslated) throws IOException { |
| 30 | + /** |
| 31 | + * Run importer for one file. Imports answer for the default system user (usually assistant userId). |
| 32 | + * @param file - file containing answers for one language |
| 33 | + * @param language - language of the file |
| 34 | + * @param isMachineTranslated - when you are using files that have been translated by a machine |
| 35 | + * @throws IOException |
| 36 | + */ |
| 37 | + public void run(File file, Language language, boolean isMachineTranslated) throws IOException { |
18 | 38 | List<String> lines = Files.readAllLines(file.toPath()); |
| 39 | + int n = 0; |
19 | 40 | for (String line : lines) { |
20 | 41 | Answer answer = Answer.importAnswerString(line, language, isMachineTranslated); |
21 | | - System.out.println(answer); |
22 | | - es.setAnyItemData(Answer.ANSWERS_INDEX, Answer.ANSWERS_TYPE, answer.toJson()); |
| 42 | + //System.out.println(answer); |
| 43 | + database.setAnyItemData(Answer.ANSWERS_INDEX, Answer.ANSWERS_TYPE, answer.toJson()); |
23 | 44 | //System.out.println(answer.toJsonString()); |
| 45 | + n++; |
| 46 | + } |
| 47 | + Debugger.println("Imported answers: " + n, 3); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Import all files in a folder that follow the format "answers_xy" for the default user (usually assistant userId). |
| 52 | + * where "xy" is a valid language code (e.g. "en" or "de"). |
| 53 | + * @param pathToFolder - folder with answers |
| 54 | + * @param isMachineTranslated - are these answers translated by a machine? |
| 55 | + * @throws IOException |
| 56 | + */ |
| 57 | + public void loadFolder(String pathToFolder, boolean isMachineTranslated) throws IOException{ |
| 58 | + List<File> files = FilesAndStreams.directoryToFileList(pathToFolder, null, false); |
| 59 | + for (File f : files){ |
| 60 | + String fileName = f.getName(); |
| 61 | + if (fileName.matches("answers_\\w\\w(\\..*|$)")){ |
| 62 | + String languageCode = fileName.replaceFirst("answers_(\\w\\w).*", "$1"); |
| 63 | + Language lang = Language.from(languageCode); //makes sure that the value is valid |
| 64 | + Debugger.println("Importing answers from file '" + fileName + "' for language: " + lang.toValue(), 3); |
| 65 | + run(f, lang, isMachineTranslated); |
| 66 | + } |
24 | 67 | } |
25 | 68 | } |
26 | 69 |
|
|
0 commit comments