You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.5 KiB
65 lines
2.5 KiB
package com.example.datacollect.command;
|
|
import com.example.datacollect.model.Article;
|
|
import com.example.datacollect.repository.ArticleRepository;
|
|
import com.example.datacollect.strategy.CrawlStrategy;
|
|
import com.example.datacollect.strategy.StrategyFactory;
|
|
import com.example.datacollect.view.ConsoleView;
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class AnalyzeCommand implements Command{
|
|
private final ConsoleView view;
|
|
private final StrategyFactory strategyFactory;
|
|
public AnalyzeCommand(ConsoleView view, StrategyFactory strategyFactory) {
|
|
this.view = view;
|
|
this.strategyFactory = strategyFactory;
|
|
}
|
|
@Override
|
|
public String getName() {
|
|
return "analyze";
|
|
}
|
|
@Override
|
|
public void execute(String []args,ArticleRepository repository){
|
|
if(args.length<2){
|
|
view.printError("Usage:analyze <url>");
|
|
}
|
|
String url=args[1];
|
|
CrawlStrategy strategy=strategyFactory.getStrategy(url);
|
|
if (strategy == null){
|
|
view.printError("No strategy found for: "+url);
|
|
return;
|
|
}
|
|
try{
|
|
view.printInfo("Analyzing: "+url);
|
|
Document doc=Jsoup.connect(url).get();
|
|
//调用策略解析,但不存入Repository
|
|
List<Article> articles=strategy.parse(url,doc);
|
|
//统计信息
|
|
int total=articles.size();
|
|
double avgTitleLen=articles.stream()
|
|
.mapToInt(a -> a.getTitle().length())
|
|
.average()
|
|
.orElse(0.0);
|
|
//Top 5 按标题长度排序
|
|
List<Article> top5 =articles.stream()
|
|
.sorted((a,b) -> Integer.compare(b.getTitle().length(),a.getTitle().length()))
|
|
.limit(5)
|
|
.collect(Collectors.toList());
|
|
//输出结果
|
|
view.printInfo("=== Analysis Result ===");
|
|
view.printInfo("Total Articles: " + total);
|
|
view.printInfo("Avg Title Length: " + String.format("%.2f", avgTitleLen));
|
|
view.printInfo("Top 5 Articles (by Title Length):");
|
|
int rank = 1;
|
|
for (Article a : top5) {
|
|
view.printInfo(rank + ". " + a.getTitle() + " (" + a.getTitle().length() + " chars)");
|
|
rank++;
|
|
}
|
|
view.printInfo("========================");
|
|
} catch (Exception e){
|
|
view.printError("Failed to analyze: "+e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|