import pnews.Article;
import pnews.Category;
+import pnews.EntityStat;
import pnews.Feed;
+import pnews.NER;
public class ArticleProvider {
- private static final Logger LOG = Logger.getLogger(ArticleProvider.class.getName());
+ private static final String CLASS_NAME = ArticleProvider.class.getName();
+ private static final Logger LOG = Logger.getLogger(CLASS_NAME);
private final Map<Category, List<Article>> articlesByCategory = new HashMap<>();
- private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
+ private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
private final Config config;
public ArticleProvider(Config config) {
return false;
}
- private static Article toArticle(String link, SyndEntry entry, SyndFeed feed) {
+ private static Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang) {
String desc, title, thumbnail, feedTitle, str;
Date date;
+ List<String> entities;
feedTitle = feed.getTitle();
if (feedTitle != null) {
thumbnail = e.getUrl();
break;
}
-
- if (thumbnail == null && feed.getImage() != null)
- thumbnail = feed.getImage().getUrl();
-
-
+
title = entry.getTitle().trim();
if (entry.getDescription() != null) {
if (date == null)
LOG.severe("The article " + feedTitle + " - " + title + " does not have a date");
- return new Article(link, title, desc, thumbnail, date, feedTitle);
+
+ entities = new ArrayList<>();
+ if (desc != null && lang.equals("en"))
+ try {
+ NER.classify(title, entities);
+ NER.classify(desc, entities);
+ } 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]));
}
private void addArticles(Category cat, SyndFeed feed) {
continue ;
}
- a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed));
+ a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage()));
synchronized (articles) {
articles.add(a);
/**
* Returns a copy.
*/
- public List<Article> getArticles(Category cat)
+ public List<Article> getArticles(Category cat, String entity)
throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
- List<Article> articles;
+ List<Article> articles, result;
synchronized (articlesByCategory) {
articles = getArticlesForUpdate(cat);
}
- synchronized (articles) {
- return new ArrayList<>(articles);
+ synchronized (articles) {
+ if (entity == null)
+ return new ArrayList<>(articles);
+
+ result = new ArrayList<>(articles.size());
+ for (Article a: articles)
+ if (a.hasEntity(entity))
+ result.add(a);
+
+ return result;
}
}
+ public List<EntityStat> getEntityStats(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+ List<Article> articles;
+ Map<String, EntityStat> entities;
+ final String FUNCTION_NAME = "getEntities";
+ EntityStat s;
+ List<EntityStat> stats;
+
+ LOG.entering(CLASS_NAME, FUNCTION_NAME, cat);
+
+ articles = getArticles(cat, null);
+
+ entities = new HashMap<>();
+ for (Article a: articles)
+ if (a.getEntities() != null) {
+ for (String e: a.getEntities()) {
+ s = entities.get(e);
+ if (s == null) {
+ s = new EntityStat(e);
+ entities.put(e, s);
+ }
+ s.increment();
+ }
+ }
+
+ stats = new ArrayList<>(entities.values());
+ stats.sort(new Comparator<EntityStat>() {
+
+ @Override
+ public int compare(EntityStat o1, EntityStat o2) {
+ return Integer.compare(o2.getCount(), o1.getCount());
+ }
+
+ });
+
+ LOG.exiting(CLASS_NAME, FUNCTION_NAME, stats);
+
+ return stats;
+ }
+
private class Refresher implements Runnable {
private final Category category;