← back to Wine Finder Next

lib/services/historicalWineData.js

158 lines

const axios = require('axios');

/**
 * Historical Wine Data Service
 *
 * Uses the "Learning to Taste" academic dataset from UC Davis/NeurIPS 2023
 * Contains 824k wine reviews with historical pricing data from Vivino
 * Dataset: https://thoranna.github.io/learning_to_taste/
 *
 * Fields available:
 * - vintage_id: Unique wine vintage identifier
 * - wine: Wine name
 * - year: Vintage year (1950-2021)
 * - winery_id: Winery identifier
 * - country: Production country
 * - region: Wine region
 * - price: Historical price in USD (collected 05/2023)
 * - rating: Average rating
 * - grape: Grape composition
 * - alcohol: Alcohol percentage
 * - review: User review text
 */

class HistoricalWineData {
  constructor() {
    // HuggingFace dataset URL
    this.baseUrl = 'https://huggingface.co/datasets/Dakhoo/L2T-NeurIPS-2023';

    // In-memory cache for historical data
    this.dataCache = null;
    this.cacheTimestamp = null;
    this.cacheDuration = 24 * 60 * 60 * 1000; // 24 hours
  }

  /**
   * Search historical wine data by name
   * Returns wines from academic dataset with historical pricing
   */
  async searchHistorical(query, options = {}) {
    try {
      const {
        limit = 50,
        minYear = 1950,
        maxYear = 2023,
        minRating = 0,
        maxPrice = 10000
      } = options;

      // For now, return indication that this feature requires dataset download
      // In production, this would query the downloaded CSV data

      return {
        success: true,
        wines: [],
        count: 0,
        source: 'Historical Academic Data (UC Davis/NeurIPS)',
        message: 'Historical data integration requires dataset download. Using live Vivino data instead.',
        datasetInfo: {
          name: 'Learning to Taste: A Multimodal Wine Dataset',
          size: '824k reviews, 350k+ unique vintages',
          vintageRange: '1950-2021',
          dataCollected: '05/2023',
          url: 'https://thoranna.github.io/learning_to_taste/'
        }
      };

    } catch (error) {
      console.error('Historical wine data error:', error.message);
      return {
        success: false,
        wines: [],
        error: error.message,
        source: 'Historical Academic Data'
      };
    }
  }

  /**
   * Get price trends for a specific wine over time
   * Uses historical dataset to show price evolution
   */
  async getPriceTrends(wineName, options = {}) {
    try {
      const { startYear = 1950, endYear = 2023 } = options;

      return {
        success: true,
        wine: wineName,
        trends: [],
        message: 'Price trends require historical dataset integration',
        source: 'Historical Academic Data'
      };

    } catch (error) {
      console.error('Price trends error:', error.message);
      return {
        success: false,
        error: error.message
      };
    }
  }

  /**
   * Get vintage comparisons for the same wine across years
   */
  async getVintageComparison(wineName, vintageYears = []) {
    try {
      return {
        success: true,
        wine: wineName,
        vintages: [],
        message: 'Vintage comparison requires historical dataset integration',
        source: 'Historical Academic Data'
      };

    } catch (error) {
      console.error('Vintage comparison error:', error.message);
      return {
        success: false,
        error: error.message
      };
    }
  }

  /**
   * Get dataset statistics
   */
  getDatasetInfo() {
    return {
      name: 'Learning to Taste: A Multimodal Wine Dataset',
      source: 'UC Davis / NeurIPS 2023',
      reviews: 824000,
      images: 897000,
      uniqueVintages: 350000,
      vintageRange: '1950-2021',
      dataCollectionDate: 'May 2023',
      fields: [
        'vintage_id',
        'wine name',
        'year',
        'winery_id',
        'country',
        'region',
        'price (USD)',
        'rating',
        'grape composition',
        'alcohol %',
        'user reviews'
      ],
      citation: 'Bender et al. (2023). Learning to Taste: A Multimodal Wine Dataset. arXiv:2308.16900',
      url: 'https://thoranna.github.io/learning_to_taste/',
      license: 'CC BY-NC-ND 4.0'
    };
  }
}

module.exports = new HistoricalWineData();