Skip to content
Snippets Groups Projects
Commit 997df0cd authored by Tomasz Walkowiak's avatar Tomasz Walkowiak
Browse files

Contexts for categories

parent b0a3c9f5
No related branches found
No related tags found
No related merge requests found
Pipeline #2787 passed
...@@ -13,6 +13,7 @@ import java.util.HashMap; ...@@ -13,6 +13,7 @@ import java.util.HashMap;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -27,7 +28,7 @@ public class JSON2XLSX { ...@@ -27,7 +28,7 @@ public class JSON2XLSX {
HashMap<String, Integer> keys = new HashMap<>(); HashMap<String, Integer> keys = new HashMap<>();
final String KEY = "__SENTENCES__"; final String KEY = "__SENTENCES__";
private void process(String fileIn, JSONObject options, XSSFRow row, int column) throws Exception { private void process(String fileIn, JSONObject options, XSSFRow row, int column, String name) throws Exception {
String content = new String(Files.readAllBytes(Paths.get(fileIn))); String content = new String(Files.readAllBytes(Paths.get(fileIn)));
JSONObject json = new JSONObject(content); JSONObject json = new JSONObject(content);
...@@ -45,7 +46,7 @@ public class JSON2XLSX { ...@@ -45,7 +46,7 @@ public class JSON2XLSX {
} }
if (json.has(KEY)) { if (json.has(KEY)) {
mergeSentences(json.getJSONObject(KEY)); mergeSentences(json.getJSONObject(KEY), name);
} }
} }
...@@ -73,13 +74,13 @@ public class JSON2XLSX { ...@@ -73,13 +74,13 @@ public class JSON2XLSX {
for (String fn : file.list()) { for (String fn : file.list()) {
row = sheet.createRow(RowNum); row = sheet.createRow(RowNum);
row.createCell(col - 1).setCellValue(fn); row.createCell(col - 1).setCellValue(fn);
process(fileIn + "/" + fn, options, row, col); process(fileIn + "/" + fn, options, row, col, fn);
RowNum++; RowNum++;
} }
} else { } else {
row = sheet.createRow(RowNum); row = sheet.createRow(RowNum);
process(fileIn, options, row, col); process(fileIn, options, row, col, "");
} }
header(row0, col); header(row0, col);
...@@ -93,28 +94,45 @@ public class JSON2XLSX { ...@@ -93,28 +94,45 @@ public class JSON2XLSX {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
new JSON2XLSX().process("test/fog/", "test/fog.xlsx", new JSONObject()); new JSON2XLSX().process("test/fog/", "test/fog.xlsx", new JSONObject());
new JSON2XLSX().process("test/kat/", "test/kat.xlsx", new JSONObject()); new JSON2XLSX().process("test/kat2/", "test/kat2.xlsx", new JSONObject());
} }
private JSONObject allSentences; private JSONObject allSentences;
private void mergeSentences(JSONObject json) { private void addFileName(JSONObject json, String name) {
for (String key : json.keySet()) {
for (String skey : json.getJSONObject(key).keySet()) {
JSONArray o = json.getJSONObject(key).getJSONArray(skey);
for (int i = 0; i < o.length(); i++) {
o.getJSONObject(i).put("name", name);
}
}
}
}
private void mergeSentences(JSONObject json, String name) {
addFileName(json, name);
for (String key : json.keySet()) { for (String key : json.keySet()) {
if (!allSentences.has(key)) { if (!allSentences.has(key)) {
allSentences.put(key, json.getJSONObject(key)); allSentences.put(key, json.getJSONObject(key));
} else { } else {
for (String skey : json.getJSONObject(key).keySet()) { for (String skey : json.getJSONObject(key).keySet()) {
if (!allSentences.getJSONObject(key).has(skey)) { if (!allSentences.getJSONObject(key).has(skey)) {
allSentences.getJSONObject(key).put(skey, json.getJSONObject(key).getJSONArray(skey)); JSONArray o = json.getJSONObject(key).getJSONArray(skey);
allSentences.getJSONObject(key).put(skey, o);
} else { } else {
for(int i=0;i<json.getJSONObject(key).getJSONArray(skey).length();i++) for (int i = 0; i < json.getJSONObject(key).getJSONArray(skey).length(); i++) {
allSentences.getJSONObject(key).getJSONArray(skey).put(json.getJSONObject(key).getJSONArray(skey).getString(i)); JSONObject o = json.getJSONObject(key).getJSONArray(skey).getJSONObject(i);
allSentences.getJSONObject(key).getJSONArray(skey).put(o);
}
} }
} }
} }
} }
} }
private void putAllSentences(XSSFWorkbook workBook) { private void putAllSentences(XSSFWorkbook workBook) {
...@@ -122,14 +140,27 @@ public class JSON2XLSX { ...@@ -122,14 +140,27 @@ public class JSON2XLSX {
XSSFSheet sheet = workBook.createSheet(key); XSSFSheet sheet = workBook.createSheet(key);
int col = 2; int col = 2;
int RowNum = 0; int RowNum = 0;
for (String skey : allSentences.getJSONObject(key).keySet()) XSSFRow head = sheet.createRow(RowNum++);
for (int i=0;i<allSentences.getJSONObject(key).getJSONArray(skey).length();i++) head.createCell(col - 2).setCellValue("file name");
{ //System.out.println(allSentences.getJSONObject(key).getJSONArray(skey).get(i)); head.createCell(col - 1).setCellValue("key");
String value = allSentences.getJSONObject(key).getJSONArray(skey).getString(i); head.createCell(col).setCellValue("left context");
head.createCell(col + 1).setCellValue("sentence");
head.createCell(col + 2).setCellValue("right context");
for (String skey : allSentences.getJSONObject(key).keySet()) {
for (int i = 0; i < allSentences.getJSONObject(key).getJSONArray(skey).length(); i++) { //System.out.println(allSentences.getJSONObject(key).getJSONArray(skey).get(i));
JSONObject o = allSentences.getJSONObject(key).getJSONArray(skey).getJSONObject(i);
String name = o.getString("name");
String s = o.getString("s");
String sl = o.getString("sl");
String sr = o.getString("sr");
XSSFRow row = sheet.createRow(RowNum++); XSSFRow row = sheet.createRow(RowNum++);
row.createCell(col - 2).setCellValue(name);
row.createCell(col - 1).setCellValue(skey); row.createCell(col - 1).setCellValue(skey);
row.createCell(col).setCellValue(value); row.createCell(col).setCellValue(sl);
row.createCell(col + 1).setCellValue(s);
row.createCell(col + 2).setCellValue(sr);
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment