From de9a21c7fcb3dbfe557656117e67e22b72fe306d Mon Sep 17 00:00:00 2001 From: monireh sanaei Date: Sun, 20 Aug 2017 17:40:14 +0200 Subject: [PATCH] Added Monireh's examples --- Monireh-AvoidEncoding-Clean | 65 ++++++++++++++++++++++++++++++++ Monireh-AvoidEncoding-Rot | 65 ++++++++++++++++++++++++++++++++ Monireh-PronounceableNames-Rot | 43 +++++++++++++++++++++ Monireh-pronounceableNames-Clean | 43 +++++++++++++++++++++ 4 files changed, 216 insertions(+) create mode 100644 Monireh-AvoidEncoding-Clean create mode 100644 Monireh-AvoidEncoding-Rot create mode 100644 Monireh-PronounceableNames-Rot create mode 100644 Monireh-pronounceableNames-Clean diff --git a/Monireh-AvoidEncoding-Clean b/Monireh-AvoidEncoding-Clean new file mode 100644 index 0000000..c7f12bf --- /dev/null +++ b/Monireh-AvoidEncoding-Clean @@ -0,0 +1,65 @@ +public class DBParserOpenFoodFacts implements DBParser { + + public static final String NO_PRODUCTS = "No products"; + public static final String CODE = "code"; + public static final String TITLE = "title"; + public static final String PARAGRAPH = "

"; + + @Override + public Map parseResults(String entry, String productName) { + + Map productResults = new HashMap(); + + if(!entry.contains(NO_PRODUCTS)) { + + int nbProductsStart = entry.indexOf(PARAGRAPH) + PARAGRAPH.length(); + int nbProductsEnd = entry.indexOf(" "); + + String nbProductsString = entry.substring(nbProductsStart, nbProductsEnd); + + int nbProducts = 0; + try { + nbProducts = Integer.parseInt(nbProductsString); + } catch(NumberFormatException e){ + System.out.println(e.getStackTrace()); + } + System.out.println("Number of products : " + nbProducts); + + int codeSearchIndex = 0; + int codeStartIndex = 0; + int codeEndIndex = 0; + + int titleSearchIndex = 0; + int titleStartIndex = 0; + int titleEndIndex = 0; + + int indexOfCode = -1; + int indexOfTitle = -1; + + while (indexOfCode != entry.lastIndexOf(CODE) && indexOfTitle != entry.lastIndexOf(TITLE)) { + + indexOfCode = entry.indexOf(CODE, codeSearchIndex); + + codeStartIndex = indexOfCode + CODE.length() + 1; + codeEndIndex = entry.indexOf("\\", codeStartIndex); + + String barcode = entry.substring(codeStartIndex, codeEndIndex); + + codeSearchIndex = codeEndIndex + 1; + + indexOfTitle = entry.indexOf(TITLE, titleSearchIndex); + + titleStartIndex = indexOfTitle + TITLE.length() + 3; + titleEndIndex = entry.indexOf("\\", titleStartIndex); + + String title = entry.substring(titleStartIndex, titleEndIndex); + title = title.replace(" ", " "); + + productResults.put(barcode, new Product(barcode, productName, title)); + + titleSearchIndex = titleEndIndex + 1; + } + } + return productResults; + } +} \ No newline at end of file diff --git a/Monireh-AvoidEncoding-Rot b/Monireh-AvoidEncoding-Rot new file mode 100644 index 0000000..5f5de2d --- /dev/null +++ b/Monireh-AvoidEncoding-Rot @@ -0,0 +1,65 @@ +public class CDBParserOpenFoodFacts implements DBParser { + + public static final String Const_NO_PRODUCTS = "No products"; + public static final String Const_CODE = "code"; + public static final String Const_TITLE = "title"; + public static final String Const_PARAGRAPH = "

"; + + @Override + public Map parseResults(String inString, String sProductName) { + + Map productHashMap = new HashMap(); + + if(!inString.contains(Const_NO_PRODUCTS)) { + + int iNbProductsStart = inString.indexOf(Const_PARAGRAPH) + Const_PARAGRAPH.length(); + int iNbProductsEnd = inString.indexOf(" "); + + String sNbProductsString = inString.substring(iNbProductsStart, iNbProductsEnd); + + int iNbProducts = 0; + try { + iNbProducts = Integer.parseInt(sNbProductsString); + } catch(NumberFormatException e){ + System.out.println(e.getStackTrace()); + } + System.out.println("Number of products : " + iNbProducts); + + int iCodeSearchIndex = 0; + int iCodeStartIndex = 0; + int iCodeEndIndex = 0; + + int iTitleSearchIndex = 0; + int iTitleStartIndex = 0; + int iTitleEndIndex = 0; + + int indexOfCode = -1; + int indexOfTitle = -1; + + while (indexOfCode != inString.lastIndexOf(Const_CODE) && indexOfTitle != inString.lastIndexOf(Const_TITLE)) { + + indexOfCode = inString.indexOf(Const_CODE, iCodeSearchIndex); + + iCodeStartIndex = indexOfCode + Const_CODE.length() + 1; + iCodeEndIndex = inString.indexOf("\\", iCodeStartIndex); + + String sBarcode = inString.substring(iCodeStartIndex, iCodeEndIndex); + + iCodeSearchIndex = iCodeEndIndex + 1; + + indexOfTitle = inString.indexOf(Const_TITLE, iTitleSearchIndex); + + iTitleStartIndex = indexOfTitle + Const_TITLE.length() + 3; + iTitleEndIndex = inString.indexOf("\\", iTitleStartIndex); + + String title = inString.substring(iTitleStartIndex, iTitleEndIndex); + title = title.replace(" ", " "); + + productHashMap.put(sBarcode, new Product(sBarcode, sProductName, title)); + + iTitleSearchIndex = iTitleEndIndex + 1; + } + } + return productHashMap; + } +} \ No newline at end of file diff --git a/Monireh-PronounceableNames-Rot b/Monireh-PronounceableNames-Rot new file mode 100644 index 0000000..3600958 --- /dev/null +++ b/Monireh-PronounceableNames-Rot @@ -0,0 +1,43 @@ +public class LkpCli { + + public static final int PL_SZ = 100; + private final ExecutorService plServ; + + private LinkedList prdts; + + private Map prdtsBN; + + private String grcyPt = "resources//grocery.txt"; + + private String lkpPt = "resources//lookupData.json"; + + + public LkpCli(int plSz) { + + prdts = FileUtility.rdTxtF(grcyPt); + prdtsBN = Collections.synchronizedMap(new HashMap()); + + plServ = Executors.newFixedThreadPool(plSz); + } + + + public static void main(String[] args) { + + LkpCli concrLkp = new LkpCli(PL_SZ); + + while(concrLkp.prdts.size() > 0){ + + String prdNm = concrLkp.prdts.removeFirst(); + + CHttpConnection httpConn = new CHttpConnection(prdNm, concrLkp.prdtsBN); + + concrLkp.plServ.submit(httpConn); + } + concrLkp.plServ.shutdown(); + + while (!concrLkp.plServ.isTerminated()) { + } + + FileUtility.mp2Json(concrLkp.prdtsBN, concrLkp.lkpPt); + } +} diff --git a/Monireh-pronounceableNames-Clean b/Monireh-pronounceableNames-Clean new file mode 100644 index 0000000..814ae41 --- /dev/null +++ b/Monireh-pronounceableNames-Clean @@ -0,0 +1,43 @@ +public class LookupClient { + + + public static final int POOL_SIZE = 100; + private final ExecutorService poolService; + + private LinkedList products; + + private Map productsByName; + + private String groceryPath = "resources//grocery.txt"; + + private String lookupPath = "resources//lookupData.json"; + + public LookupClient(int poolSize) { + + products = FileUtility.readTextFile(groceryPath); + productsByName = Collections.synchronizedMap(new HashMap()); + + poolService = Executors.newFixedThreadPool(poolSize); + } + + + public static void main(String[] args) { + + LookupClient concurrentLookup = new LookupClient(POOL_SIZE); + + while(concurrentLookup.products.size() > 0){ + + String productName = concurrentLookup.products.removeFirst(); + + HttpConnection httpConnection = new HttpConnection(productName, concurrentLookup.productsByName); + + concurrentLookup.poolService.submit(httpConnection); + } + concurrentLookup.poolService.shutdown(); + + while (!concurrentLookup.poolService.isTerminated()) { + } + + FileUtility.mapToJson(concurrentLookup.productsByName, concurrentLookup.lookupPath); + } +} -- GitLab