fixed computing of entities when the article does not have a description
[pnews.git] / war / src / main / java / pnews / servlet / ArticleProvider.java
index c2d8f59..bdba835 100644 (file)
@@ -3,6 +3,8 @@ package pnews.servlet;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -74,7 +76,20 @@ public class ArticleProvider {
                 return false;
         }
         
-        private static Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang) {
+        private Instant getArticleInstant(SyndEntry entry) {
+                Date date;
+                
+                date = entry.getUpdatedDate();       
+                if (date == null)
+                        date = entry.getPublishedDate();
+
+                if (date == null)
+                        return Instant.now();
+                
+                return date.toInstant();
+        }
+        
+        private Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang, Instant instant) {
                 String desc, title, thumbnail, feedTitle, str;
                 Date date;
                 List<String> entities;
@@ -100,24 +115,18 @@ public class ArticleProvider {
                         desc = null;
                         LOG.severe("No description for " + feedTitle + " - " + title);
                 }
-                
-                date = entry.getPublishedDate();
-                if (date == null)
-                        date = entry.getUpdatedDate();
-                if (date == null)
-                        LOG.severe("The article " + feedTitle + " - " + title + " does not have a date");
-                                     
-                
+                                
                 entities = new ArrayList<>();
-                if (desc != null && lang.equals("en"))
+                if (lang.equals("en"))
                         try {
-                                NER.classify(title, entities);
-                                NER.classify(desc, entities);
+                                NER.classify(title, entities, config);
+                                if (desc != null)
+                                        NER.classify(desc, entities, config);
                         } catch (ClassCastException | ClassNotFoundException | IOException e1) {
                                 LOG.log(Level.SEVERE, "Cannot classify " + feedTitle, e1);                         
                         }
                 
-                return new Article(link, title, desc, thumbnail, date, feedTitle, entities.toArray(new String[0]));
+                return new Article(link, title, desc, thumbnail, instant, feedTitle, entities.toArray(new String[0]));
         }
         
         private void addArticles(Category cat, SyndFeed feed) {
@@ -137,7 +146,12 @@ public class ArticleProvider {
                                 continue ;
                         }
                         
-                        a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage()));
+                        final Instant instant = getArticleInstant(entry);
+                        
+                        if (config.isObsolete(instant))
+                                continue ;
+                        
+                        a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage(), instant));
                         
                         synchronized (articles) {
                                 articles.add(a);
@@ -208,14 +222,17 @@ public class ArticleProvider {
                 final String FUNCTION_NAME = "getEntities";
                 EntityStat s;
                 List<EntityStat> stats;
+                Instant minInstant;
                 
                 LOG.entering(CLASS_NAME, FUNCTION_NAME, cat);
                 
                 articles = getArticles(cat, null);
                 
+                minInstant = Instant.now().minus(15, ChronoUnit.DAYS);
+                
                 entities = new HashMap<>();
-                for (Article a: articles) 
-                        if (a.getEntities() != null) {
+                for (Article a: articles)
+                        if (a.getPublicationDate().isAfter(minInstant) && a.getEntities() != null)
                                 for (String e: a.getEntities()) {
                                         s = entities.get(e);
                                         if (s == null) {
@@ -224,8 +241,7 @@ public class ArticleProvider {
                                         }
                                         s.increment();
                                 }                
-                        }
-                
+               
                 stats = new ArrayList<>(entities.values());
                 stats.sort(new Comparator<EntityStat>() {