/*
 * Copyright 2016 org.NLP4L
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.nlp4l.solr.ltr;

import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LinearWeightScorer extends Scorer {

  private final List<FieldFeatureExtractor[]> featuresSpec;
  private final List<Float> weights;
  private final DocIdSetIterator iterator;

  public LinearWeightScorer(Weight luceneWeight, List<FieldFeatureExtractor[]> featuresSpec,
                            List<Float> weights, DocIdSetIterator iterator) {
    super(luceneWeight);
    this.featuresSpec = featuresSpec;
    this.weights = weights;
    this.iterator = iterator;
  }

  @Override
  public int docID() {
    // TODO: implement
    return iterator.docID();
  }

  @Override
  public float score() throws IOException {
    // 現在の docID() に関して w1*x1 + w2*x2 + ... + wd*xd を計算して返す。
    final int target = docID();
    float score = 0;
    int idx = 0;
    for(FieldFeatureExtractor[] extractors: featuresSpec){
      float feature = 0;
      for(FieldFeatureExtractor extractor: extractors){
        feature += extractor.feature(target);
      }

      score += weights.get(idx) * feature;
      idx++;
    }

    return score;
  }

  @Override
  public int freq() throws IOException {
    // TODO: implement
    // 主にexplain()で利用？
    return 1;
  }

  @Override
  public DocIdSetIterator iterator() {
    return iterator;
  }
}

@@ -1512,69 +1512,8 @@
 n %7B%0A
-    // %E7%8F%BE%E5%9C%A8%E3%81%AE docID() %E3%81%AB%E9%96%A2%E3%81%97%E3%81%A6 w1*x1 + w2*x2 + ... + wd*xd %E3%82%92%E8%A8%88%E7%AE%97%E3%81%97%E3%81%A6%E8%BF%94%E3%81%99%E3%80%82%0A
     
@@ -1939,31 +1939,8 @@
 ent%0A
-    // %E4%B8%BB%E3%81%ABexplain()%E3%81%A7%E5%88%A9%E7%94%A8%EF%BC%9F%0A
     

remove unnecessary comment