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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Anya
  2. A news headline aggregator that clusters near-duplicate headlines into **stories**
  3. and reports only the stories covered by a minimum number of distinct outlets.
  4. Instead of printing every pairwise "duplicate found" alert, Anya groups matching
  5. headlines transitively (union-find over cosine similarity) and answers the useful
  6. question: *which stories are multiple independent sources reporting right now?*
  7. ## How it works
  8. 1. **Load** source URLs and stopwords from `resources/`.
  9. 2. **Fetch** each source and extract candidate headline text from `<a>`/`<span>`
  10. tags, associating each headline with the nearest `<time>` publication timestamp.
  11. 3. **Normalize** each headline (lowercase, strip punctuation, remove stopwords).
  12. 4. **Cluster** headlines into stories using pairwise cosine similarity, linking
  13. matches transitively so a chain of near-duplicates collapses into one story.
  14. 5. **Filter & report** stories with at least `--min-sources` distinct outlets,
  15. optionally restricted to a date window.
  16. ## Requirements
  17. - Python 3.11+ (developed and tested on 3.14)
  18. - `requests`
  19. ```bash
  20. pip install requests
  21. ```
  22. ## Setup
  23. ```bash
  24. git clone https://ikibani.com/jbell730/anya.git
  25. cd anya
  26. pip install requests
  27. ```
  28. Run from the project root — the resource paths are relative (`./resources/...`).
  29. ## Configuration
  30. - `resources/sources.txt` — one news source URL per line.
  31. - `resources/stopwords.txt` — one stopword per line.
  32. - `resources/excluded_phrases.txt` — boilerplate link text to ignore (see below).
  33. - Defaults live at the top of `main.py` (`SIMILARITY_THRESHOLD = 0.75`,
  34. `MIN_SOURCES = 2`) and can be overridden on the command line.
  35. ## Usage
  36. ```bash
  37. python main.py [options]
  38. ```
  39. | Option | Description | Default |
  40. | --- | --- | --- |
  41. | `--min-sources N` | Only output stories reported by at least N distinct sources | `2` |
  42. | `--threshold T` | Cosine similarity used to consider two headlines the same story | `0.75` |
  43. | `--since YYYY-MM-DD` | Only consider headlines published on or after this date | *(none)* |
  44. | `--until YYYY-MM-DD` | Only consider headlines published on or before this date | *(none)* |
  45. | `--verbose` | Enable debug logging | off |
  46. ### Examples
  47. ```bash
  48. # Stories reported by 3+ distinct outlets
  49. python main.py --min-sources 3
  50. # Stories from the last week, need 2+ outlets
  51. python main.py --since 2026-09-07
  52. # A specific range with a stricter similarity threshold
  53. python main.py --since 2026-09-01 --until 2026-09-14 --threshold 0.8
  54. ```
  55. Sample output:
  56. ```
  57. News stories covered by at least 2 distinct sources (published on or after 2026-09-13)
  58. ============================================================
  59. [3 source(s)] Federal Reserve holds interest rates steady (2026-09-14)
  60. cnn.com, foxnews.com, reuters.com
  61. [2 source(s)] Senate passes major infrastructure bill (2026-09-14)
  62. npr.org, nbcnews.com
  63. ```
  64. ## Design notes
  65. - **A "source" is a distinct domain**, not a distinct URL. `www.cnn.com/us` and
  66. `cnn.com/politics` both normalize to `cnn.com`, so one outlet counts once even
  67. when listed under multiple sections.
  68. - **Date windowing drops undated headlines.** When `--since` or `--until` is set,
  69. any headline whose page carries no parseable timestamp is excluded because its
  70. recency can't be established (the count is logged). Without a date flag,
  71. everything is included.
  72. - **Publication dates** are drawn from `<time>` elements (their `datetime`/`title`
  73. attributes or inner text) and associated with headlines in document order; the
  74. association resets at each `<article>`/`<li>` boundary so undated headlines don't
  75. inherit a neighboring story's date.
  76. - **Boilerplate link text is filtered.** Navigation/footer/legal links like
  77. "skip to content" or "your privacy choices" are dropped by matching against
  78. `excluded_phrases.txt` (case- and punctuation-insensitive; an entry matches when
  79. it appears anywhere in the link text as a run of words, so `privacy choices`
  80. also catches "Your Privacy Choices"). Extend the file rather than editing code.
  81. ## Tests
  82. ```bash
  83. python -m unittest discover -s tests -p 'test_*.py'
  84. ```
  85. ## Project structure
  86. ```
  87. anya/
  88. ├── main.py # CLI entry point and orchestration
  89. ├── services/
  90. │ ├── headlines.py # fetch + parse headlines (and timestamps)
  91. │ ├── normalization.py # stopword loading and headline normalization
  92. │ ├── similarity.py # cosine similarity over token lists
  93. │ ├── sources.py # load source URLs
  94. │ ├── stories.py # cluster headlines into stories
  95. │ └── dates.py # date parsing and window filtering
  96. ├── structs/
  97. │ ├── headline.py # Headline model (text, domain, published_at)
  98. │ └── story.py # Story model (sources, representative, latest_date)
  99. ├── resources/
  100. │ ├── sources.txt # one source URL per line
  101. │ ├── stopwords.txt # one stopword per line
  102. │ └── excluded_phrases.txt # boilerplate link text to ignore
  103. └── tests/ # unit tests
  104. ```