public static void viewAd(String id) { Ad ad = Ad.findById(Long.parseLong(id)); List<Category> cats = Category.find("categorytype_id=?1 order by id", "1").fetch(); EntityManager entityManager = play.db.jpa.JPA.em(); List<BigInteger> bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by maxCount") .getResultList(); int min = bCounts.get(0).intValue(); int max = bCounts.get(bCounts.size() - 1).intValue(); bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by category_id ") .getResultList(); List<String> fonts = new ArrayList<String>(); for (int i = 0; i < bCounts.size(); i++) { BigInteger count = bCounts.get(i); int x = Ads.getFontSize(count.intValue(), min, max); fonts.add(String.valueOf(x)); } render(ad, fonts, min, max, cats); }
public void deleteLearnCategories() { ArrayList<Category> learnCategories = learnGraph.getCategories(); for (Category c : learnCategories) { Nodemapper n = brain.findNode(c); System.out.println("Found node " + n + " for " + c.inputThatTopic()); if (n != null) n.category = null; } learnGraph = new Graphmaster(this); }
/** Gathers and propagates the unique values of each distributive attribute. */ public void propagateDistributiveAttrs(Category cat, Category cat2) { if (_distributiveAttrs == null) return; resetDistrAttrVals(); cat.forall(gatherDistrAttrVals); if (cat2 != null) { cat2.forall(gatherDistrAttrVals); } cat.forall(propagateUniqueDistrAttrVals); if (cat2 != null) { cat2.forall(propagateUniqueDistrAttrVals); } }
/** Expands inheritsFrom links to feature equations for those features not explicitly listed. */ public void expandInheritsFrom(Category cat, Category cat2) { // index feature structures featStrucMap.clear(); cat.forall(indexFeatStrucs); if (cat2 != null) { cat2.forall(indexFeatStrucs); } // add feature eqs cat.forall(doInheritsFrom); if (cat2 != null) { cat2.forall(doInheritsFrom); } }
/** Write all AIML files. Adds categories for BUILD and DEVELOPMENT ENVIRONMENT */ public void writeAIMLFiles() { if (MagicBooleans.trace_mode) System.out.println("writeAIMLFiles"); HashMap<String, BufferedWriter> fileMap = new HashMap<String, BufferedWriter>(); Category b = new Category(0, "BRAIN BUILD", "*", "*", new Date().toString(), "update.aiml"); brain.addCategory(b); // b = new Category(0, "PROGRAM VERSION", "*", "*", MagicStrings.program_name_version, // "update.aiml"); // brain.addCategory(b); ArrayList<Category> brainCategories = brain.getCategories(); Collections.sort(brainCategories, Category.CATEGORY_NUMBER_COMPARATOR); for (Category c : brainCategories) { if (!c.getFilename().equals(MagicStrings.null_aiml_file)) try { // System.out.println("Writing "+c.getCategoryNumber()+" "+c.inputThatTopic()); BufferedWriter bw; String fileName = c.getFilename(); if (fileMap.containsKey(fileName)) bw = fileMap.get(fileName); else { String copyright = Utilities.getCopyright(this, fileName); bw = new BufferedWriter(new FileWriter(aiml_path + "/" + fileName)); fileMap.put(fileName, bw); bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" + "<aiml>\n"); bw.write(copyright); // bw.newLine(); } bw.write(Category.categoryToAIML(c) + "\n"); // bw.newLine(); } catch (Exception ex) { ex.printStackTrace(); } } Set set = fileMap.keySet(); for (Object aSet : set) { BufferedWriter bw = fileMap.get(aSet); // Close the bw try { if (bw != null) { bw.write("</aiml>\n"); bw.flush(); bw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } File dir = new File(aiml_path); dir.setLastModified(new Date().getTime()); }
/** * write categories to AIMLIF file * * @param cats array list of categories * @param filename AIMLIF filename */ public void writeIFCategories(ArrayList<Category> cats, String filename) { // System.out.println("writeIFCategories "+filename); BufferedWriter bw = null; File existsPath = new File(aimlif_path); if (existsPath.exists()) try { // Construct the bw object bw = new BufferedWriter(new FileWriter(aimlif_path + "/" + filename)); for (Category category : cats) { bw.write(Category.categoryToIF(category)); bw.newLine(); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { // Close the bw try { if (bw != null) { bw.flush(); bw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } }
/** * read AIMLIF categories from a file into bot brain * * @param filename name of AIMLIF file * @return array list of categories read */ public ArrayList<Category> readIFCategories(String filename) { ArrayList<Category> categories = new ArrayList<Category>(); try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(filename); // Get the object BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; // Read File Line By Line while ((strLine = br.readLine()) != null) { try { Category c = Category.IFToCategory(strLine); categories.add(c); } catch (Exception ex) { System.out.println("Invalid AIMLIF in " + filename + " line " + strLine); } } // Close the input stream br.close(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); } return categories; }
// unify sem class with default nom var(s) private void unifySemClass(Category cat, String semClass) { if (semClass == null || cat.getLF() == null) return; SEMCLASS = grammar.types.getSimpleType(semClass); try { cat.getLF().deepMap(defaultNomvarUnifier); } catch (TypePropagationException tpe) { if (debugSemClasses) { System.err.println( "Warning: unable to unify types '" + tpe.st1 + "' and '" + tpe.st2 + "' in unifying sem class in cat: \n" + cat); } } }
public int hashCode() { int code = 17; code += object.hashCode(); code *= 37; code += category.hashCode(); code *= 37; return code; }
public class ItemMetaData implements Serializable, ItemMetaDataIfc { static Category errorLogger = Category.getInstance("errorLogger"); private static final long serialVersionUID = 7526471155622776147L; private Long id; private ItemDataIfc item; private String label; private String entry; public ItemMetaData() {} public ItemMetaData(ItemDataIfc item, String label, String entry) { this.item = item; this.label = label; this.entry = entry; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public ItemDataIfc getItem() { return item; } public void setItem(ItemDataIfc item) { this.item = item; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getEntry() { return entry; } public void setEntry(String entry) { this.entry = entry; } private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.defaultWriteObject(); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); } }
/** Propagates types of nomvars in the given categories. */ public void propagateTypes(Category cat, Category cat2) { try { nomvarMap.clear(); cat.deepMap(nomvarTypePropagater); if (cat2 != null) cat2.deepMap(nomvarTypePropagater); cat.deepMap(nomvarTypePropagater); if (cat2 != null) cat2.deepMap(nomvarTypePropagater); } catch (TypePropagationException tpe) { if (debugSemClasses) { System.err.println( "Warning: unable to unify types '" + tpe.st1 + "' and '" + tpe.st2 + "' in cat: \n" + cat); if (cat2 != null) System.err.println("and cat: \n" + cat2); } } }
/** Write all AIMLIF files from bot brain */ public void writeAIMLIFFiles() { if (MagicBooleans.trace_mode) System.out.println("writeAIMLIFFiles"); HashMap<String, BufferedWriter> fileMap = new HashMap<String, BufferedWriter>(); Category b = new Category(0, "BRAIN BUILD", "*", "*", new Date().toString(), "update.aiml"); brain.addCategory(b); ArrayList<Category> brainCategories = brain.getCategories(); Collections.sort(brainCategories, Category.CATEGORY_NUMBER_COMPARATOR); for (Category c : brainCategories) { try { BufferedWriter bw; File aimlifFile; String fileName = c.getFilename(); if (fileMap.containsKey(fileName)) bw = fileMap.get(fileName); else { aimlifFile = new File(aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix); aimlifFile.getParentFile().mkdirs(); bw = new BufferedWriter(new FileWriter(aimlifFile)); fileMap.put(fileName, bw); } bw.write(Category.categoryToIF(c)); bw.newLine(); } catch (Exception ex) { ex.printStackTrace(); } } Set set = fileMap.keySet(); for (Object aSet : set) { BufferedWriter bw = fileMap.get(aSet); // Close the bw try { if (bw != null) { bw.flush(); bw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } File dir = new File(aimlif_path); dir.setLastModified(new Date().getTime()); }
public boolean equals(Object o) { if (!(o instanceof Assignment)) { return false; } Assignment a = (Assignment) o; if (!category.equals(a.category)) { return false; } if (!object.equals(a.object)) { return false; } return true; }
public void forall(Category c) { if (!(c instanceof AtomCat)) return; String type = ((AtomCat) c).getType(); FeatureStructure fs = c.getFeatureStructure(); if (fs == null) return; for (Iterator<String> it = fs.getAttributes().iterator(); it.hasNext(); ) { String att = it.next(); _catsToAttrs.put(type, att); if (fs.getValue(att) instanceof LF) { _lfAttrs.add(att); } } }
@SuppressWarnings("unchecked") public void forall(Category c) { if (!(c instanceof AtomCat)) return; FeatureStructure fs = c.getFeatureStructure(); if (fs == null) return; for (int i = 0; i < _distributiveAttrs.length; i++) { String attr = _distributiveAttrs[i]; Object val = fs.getValue(attr); if (val != null && !distrAttrVals[i].contains(val)) { distrAttrVals[i].add(val); } } }
public void forall(Category c) { if (!(c instanceof AtomCat)) return; FeatureStructure fs = c.getFeatureStructure(); if (fs == null) return; for (int i = 0; i < _distributiveAttrs.length; i++) { if (distrAttrVals[i].size() != 1) continue; Object distVal = distrAttrVals[i].get(0); String attr = _distributiveAttrs[i]; Object val = fs.getValue(attr); if (val == null) { fs.setFeature(attr, UnifyControl.copy(distVal)); } } }
public void forall(Category c) { // get feature structures if (!(c instanceof AtomCat)) return; String type = ((AtomCat) c).getType(); FeatureStructure fs = c.getFeatureStructure(); GFeatStruc gfs = (GFeatStruc) fs; if (gfs == null || gfs.getInheritsFrom() == 0) return; int inhf = gfs.getInheritsFrom(); FeatureStructure inhfFS = (FeatureStructure) featStrucMap.get(inhf); if (inhfFS != null) { // copy values of features from inhfFS not already present for (Iterator<String> it = inhfFS.getAttributes().iterator(); it.hasNext(); ) { String att = it.next(); if (gfs.hasAttribute(att)) continue; gfs.setFeature(att, UnifyControl.copy(inhfFS.getValue(att))); } // for each possible attr used with this type and not already present, // add feature equation Collection<String> attrs = (Collection<String>) _catsToAttrs.get(type); if (attrs == null) return; for (Iterator<String> it = attrs.iterator(); it.hasNext(); ) { String att = it.next(); if (gfs.hasAttribute(att)) continue; String varName = att.toUpperCase() + inhf; if (_lfAttrs.contains(att)) { gfs.setFeature(att, new HyloVar(varName)); inhfFS.setFeature(att, new HyloVar(varName)); } else { gfs.setFeature(att, new GFeatVar(varName)); inhfFS.setFeature(att, new GFeatVar(varName)); } } } else { System.err.println( "Warning: no feature structure with inheritsFrom index of " + inhf + " found in category " + c); } }
public static void convertInsToCat(String baseDir, ArrayList<String> infoboxFileList) throws IOException { BufferedWriter writer; Map<String, String> insToCat; for (String fileName : infoboxFileList) { String[] strArr = fileName.split("/"); String output = "output/" + strArr[0] + "/" + strArr[2]; if (App.checkFile(output)) continue; writer = new BufferedWriter(new FileWriter(new File(output))); // get Map of instance to categories insToCat = Category.getInsToCat(strArr[0]); // convert convertInsToCatInner(baseDir, fileName, writer, insToCat); insToCat = null; writer.close(); System.out.println("File is created: " + output); System.out.println(); } }
public class PublishedItemText implements Serializable, ItemTextIfc, Comparable<ItemTextIfc> { static Category errorLogger = Category.getInstance("errorLogger"); private static final long serialVersionUID = 7526471155622776147L; private Long id; private ItemDataIfc item; private Long sequence; private String text; private Set answerSet; private Set itemTextAttachmentSet; private Integer requiredOptionsCount; public PublishedItemText() {} public PublishedItemText(PublishedItemData item, Long sequence, String text, Set answerSet) { this.item = item; this.sequence = sequence; this.text = text; this.answerSet = answerSet; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public ItemDataIfc getItem() { return item; } public void setItem(ItemDataIfc item) { this.item = item; } public Long getSequence() { return sequence; } public void setSequence(Long sequence) { this.sequence = sequence; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Set getAnswerSet() { return answerSet; } public void setAnswerSet(Set answerSet) { this.answerSet = answerSet; } private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.defaultWriteObject(); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); } public ArrayList getAnswerArray() { ArrayList list = new ArrayList(); Iterator iter = answerSet.iterator(); while (iter.hasNext()) { list.add(iter.next()); } return list; } public ArrayList getAnswerArraySorted() { ArrayList list = getAnswerArray(); Collections.sort(list); return list; } public int compareTo(ItemTextIfc o) { return sequence.compareTo(o.getSequence()); } public Set getItemTextAttachmentSet() { return itemTextAttachmentSet; } public List getItemTextAttachmentList() { return new ArrayList(itemTextAttachmentSet); } public void setItemTextAttachmentSet(Set itemTextAttachmentSet) { this.itemTextAttachmentSet = itemTextAttachmentSet; } // for EMI - Attachments at Answer Level public boolean getHasAttachment() { if (itemTextAttachmentSet != null && itemTextAttachmentSet.size() > 0) return true; else return false; } // This is an actual EMI Question Item // (i.e. not Theme or Lead In Text or the complete Answer Options list) public boolean isEmiQuestionItemText() { return getSequence() > 0; } public Integer getRequiredOptionsCount() { if (requiredOptionsCount == null) { return Integer.valueOf(1); } return requiredOptionsCount; } public void setRequiredOptionsCount(Integer requiredOptionsCount) { this.requiredOptionsCount = requiredOptionsCount; } public String getEmiCorrectOptionLabels() { if (!item.getTypeId().equals(TypeD.EXTENDED_MATCHING_ITEMS)) return null; if (!this.isEmiQuestionItemText()) return null; if (answerSet == null) return null; String correctOptionLabels = ""; Iterator iter = getAnswerArraySorted().iterator(); while (iter.hasNext()) { AnswerIfc answer = (AnswerIfc) iter.next(); if (answer.getIsCorrect()) { correctOptionLabels += answer.getLabel(); } } return correctOptionLabels; } }
/** @param category the category to be registered */ public void registerCategory(Category category) { if (!categories.keySet().contains(category.getName())) categories.put(category.getName(), category); }
public static void list( String search, int category, Integer size, Integer page, int firstPage, int lastPage) { List<Ad> ads = null; List<Category> cats = Category.find("categorytype_id=?1", "1").fetch(); EntityManager entityManager = play.db.jpa.JPA.em(); List<BigInteger> bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by maxCount") .getResultList(); int min = bCounts.get(0).intValue(); int max = bCounts.get(bCounts.size() - 1).intValue(); bCounts = entityManager .createNativeQuery( "select count(*) as maxCount from Ad as a group by category_id order by category_id ") .getResultList(); List<String> fonts = new ArrayList<String>(); for (int i = 0; i < bCounts.size(); i++) { BigInteger count = bCounts.get(i); int x = Ads.getFontSize(count.intValue(), min, max); fonts.add(String.valueOf(x)); } int pagesCount = 0; page = page != null ? page : 1; if (search.trim().length() == 0) { Long l = null; if (category == 0) { ads = Ad.find("order by createDate desc").fetch(page, size); l = Ad.count(); } else { ads = Ad.find(" category_id=?1 order by createDate desc", category).fetch(page, size); l = Ad.count(" category_id=?1 ", category); } Long l2 = (l / 10); if ((l % 10) > 0) l2 = (long) (Math.floor(l2) + 1); pagesCount = Integer.valueOf(l2.intValue()); } else { search = search.toLowerCase(); Long l = null; if (category == 0) { ads = Ad.find( "(lower(headline) like ?1 OR lower(description) like ?2)", "%" + search + "%", "%" + search + "%") .fetch(page, size); l = Ad.count( "(lower(headline) like ?1 OR lower(description) like ?2)", "%" + search + "%", "%" + search + "%"); } else { ads = Ad.find( " category_id=?1 and (lower(headline) like ?2 OR lower(description) like ?3)", category, "%" + search + "%", "%" + search + "%") .fetch(page, size); l = Ad.count( "category_id=?1 and (lower(headline) like ?2 OR lower(description) like ?3)", category, "%" + search + "%", "%" + search + "%"); } Long l2 = (l / 10); if ((l % 10) > 0) l2 = (long) (Math.floor(l2) + 1); pagesCount = Integer.valueOf(l2.intValue()); } if ((lastPage - page) <= 2) { firstPage = page - 2; lastPage = page + 7; if (lastPage > pagesCount) lastPage = pagesCount; } else if ((page - firstPage) <= 2) { firstPage = page - 7; lastPage = page + 2; if (firstPage < 1) { firstPage = 1; lastPage = 10; } } if (lastPage > pagesCount) lastPage = pagesCount; render(ads, search, size, page, pagesCount, firstPage, lastPage, cats, fonts); }
/** * Given concept vector which represents a text, calculate the category probabilities. * * @param cv * @param conceptLimit only conceptLimit concepts are used to calculate the category path * distribution * @return category id -> scores */ public SortedMap<Integer, Double> getCategoryDistribution(ConceptVector cv, int conceptLimit) throws WikitException { ConceptIterator conceptIterator = cv.orderedIterator(); // Category ID --> Category Map<Integer, Category> bags = new HashMap<>(); int count = 0; while (conceptIterator.next() && count++ < conceptLimit) { int conceptId = conceptIterator.getId(); ConceptItem conceptItem = new ConceptItem(conceptId, conceptIterator.getValue()); Set<Integer> catIds = treeCache.getCategoryIdsByConceptId(conceptId); conceptItem.catIds = catIds; for (int catId : catIds) { if (bags.containsKey(catId)) { bags.get(catId).addItem(conceptItem); } else { Category category = new Category(catId); category.addItem(conceptItem); bags.put(catId, category); } } } double totalScore = 0; SortedMap<Integer, Double> sortedScores = null; // category id -> score Map<Integer, Double> scores = new HashMap<>(); // LOG.info("Method 1:"); // for (Map.Entry<Integer, Category> entry : bags.entrySet()) { // double categoryScore = 0; // Category category = entry.getValue(); // for (ConceptItem item : category.concepts()) { // categoryScore += item.value; // } // double normalizedScore = categoryScore/entry.getValue().size(); // scores.put(entry.getKey(), normalizedScore); // totalScore += normalizedScore; // } // // sortedScores = new TreeMap<Integer,Double>(new ValueComparator(scores)); // sortedScores.putAll(scores); // Method 2, take link into account" double LAMBDA = 0.6; totalScore = 0; scores = new HashMap<>(); for (Map.Entry<Integer, Category> entry : bags.entrySet()) { double categoryScore = 0; Category category = entry.getValue(); for (ConceptItem conceptItem : category.concepts()) { double v1 = conceptItem.value; double v2 = 0.0; Set<Integer> inlinkIds = conceptCache.getInlinkIds(conceptItem.id); for (int inlinkId : inlinkIds) { if (category.hasConcept(inlinkId)) { v2 += category.getConcept(inlinkId).value; // System.out.println(inlink + "==>" + item.id + "\t" + item.title); } } if (inlinkIds.size() > 0) { v2 = v2 / inlinkIds.size(); // normalize } // if item connected with double v = LAMBDA * v1 + (1 - LAMBDA) * v2; categoryScore += v; } double normalizedScore = categoryScore / category.size(); scores.put(category.id, normalizedScore); totalScore += normalizedScore; } sortedScores = new TreeMap<Integer, Double>(new ValueComparator<>(scores)); boolean normalize = true; if (normalize) { // normalized the value for (Map.Entry<Integer, Double> entry : scores.entrySet()) { sortedScores.put(entry.getKey(), entry.getValue() / totalScore); } } else { sortedScores.putAll(scores); } return sortedScores; }
// given EntriesItem private void getWithEntriesItem( Word w, MorphItem mi, String stem, String pred, String targetPred, String targetRel, EntriesItem item, MacroAdder macAdder, Map<String, Double> supertags, Set<String> supertagsFound, SignHash result) { // ensure apropos if (targetPred != null && !targetPred.equals(pred)) return; if (targetRel != null && !targetRel.equals(item.getIndexRel()) && !targetRel.equals(item.getCoartRel())) return; if (!item.getActive().booleanValue()) return; if (mi.excluded(item)) return; try { // copy and add macros Category cat = item.getCat().copy(); macAdder.addMacros(cat); // replace DEFAULT_VAL with pred, after first // unifying type of associated nom var(s) with sem class unifySemClass(cat, mi.getWord().getSemClass()); REPLACEMENT = pred; cat.deepMap(defaultReplacer); // check supertag // TODO: think about earlier checks for efficiency, for grammars where macros and preds don't // matter // Double lexprob = null; // nb: skipping lex log probs, don't seem to be helpful if (supertags != null) { // skip if not found String stag = cat.getSupertag(); if (!supertags.containsKey(stag)) return; // otherwise update found supertags supertagsFound.add(stag); // get lex prob // lexprob = supertags.get(stag); } // propagate types of nom vars propagateTypes(cat); // handle distrib attrs and inherits-from propagateDistributiveAttrs(cat); expandInheritsFrom(cat); // merge stem, pos, sem class from morph item, plus supertag from cat Word word = Word.createFullWord(w, mi.getWord(), cat.getSupertag()); // set origin and lexprob Sign sign = new Sign(word, cat); sign.setOrigin(); // if (lexprob != null) { // sign.addData(new SupertaggerAdapter.LexLogProb((float) Math.log10(lexprob))); // } // return sign result.insert(sign); } catch (RuntimeException exc) { System.err.println( "Warning: ignoring entry: " + item.getName() + " of family: " + item.getFamilyName() + " for stem: " + stem + " b/c: " + exc.toString()); } }
public void forall(Category c) { FeatureStructure fs = c.getFeatureStructure(); if (fs != null && fs.getIndex() != 0) featStrucMap.put(fs.getIndex(), fs); }
public String toString() { return "\"" + object + "\" --> " + category.toString(); }