algorithm - Calculate value of text from a dictionary of words in Java 8 -


i'm having trouble transforming algo in java 8 view.

i have arraylist composed of articles

arraylist<article> listarticles = new arraylist<>(); 

with article composed this

public class article {     private string titlearticle;     private string abstractarticle;     private string textarticle;     private long value; } 

and on other side have map of words each 1 value associated

hashmap<string, long> dictionary = new hashmap<>(); 

i want value of article. value of article calculated based on words in title, abstract , text (all added together)

in java 7 (i hope didn't make mistake here)

for(article article : dataarticles){     double valuearticle = 0;      for(map.entry<string, long> word : datadictionary.entryset()){           //looping through words in title          for(string text : article.gettitle().split(" ")){             if(text.equalsignorecase(word.getkey())){                 valuearticle += word.getvalue();             }          }          //looping through words in abstract          for(string text : article.getabstracttext().split(" ")){             if(text.equalsignorecase(word.getkey())){                 valuearticle += word.getvalue();             }          }          //looping through words in abstract          for(string text : article.gettext().split(" ")){             if(text.equalsignorecase(word.getkey())){                 valuearticle += word.getvalue();             }          }     }      article.setvalue(valuearticle); } 

how can calculate value of each article inside array reducing time process?
thinking of using lambdas maybe it's bad approach.
i'm new java 8 , trying learn it.

after developing

still looking around how make arraylist using streams. in meantime wanted, well, sort out list greatest article value lowest article value. imagined this

comparator<article> byarticlevalue = (a1, a2) -> integer.compare(a1.getvalue(), a2.getvalue()); dataarticles.stream()         .sorted(byarticlevalue); 

but list comes out unsorted. doing wrong in case ?

if dictionary keys not lower case, should create lower-cased version , re-use it:

/**  * create copy of dictionary keys in lower case.  * @param lc dictionary of lowercase words value  * @param article article evaluated  */ static map<string, double> convert(map<string, double> dictionary)  {   return       dictionary.entryset().stream()       .collect(collectors.tomap(e -> e.getkey().tolowercase(),                 map.entry::getvalue,                 (p, q) -> p + q)); } 

then, each article, can compute value using stream pipeline:

/**  * compute value of article.  * @param lc dictionary of lowercase words value  * @param article article evaluated  */ static double evaluate(map<string, double> lc, article article) {   return       stream.of(article.gettitle(), article.getabstracttext(), article.gettext())       .flatmap(s -> arrays.stream(s.tolowercase().split(" ")))       .maptodouble(k -> lc.getordefault(k, 0d))       .sum(); } 

for more flexibility in folding words together, use collator index collationkey rather lowercase words. similar enhancement made tokenizing text, rather splitting on spaces.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -