Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2039: Relevance based scoring filter contributed by Sujen Shah <[email protected]> this closes #30.
Chris Mattmanndfc87f35ded6
diff --git a/CHANGES.txt b/CHANGES.txtindex c90e9400d..e745e4fc2 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2039 Relevance based scoring filter (Sujen Shah via mattmann)+* NUTCH-2037 Job endpoint to support Indexing from the REST API (Sujen Shah via mattmann)* NUTCH-2017 Remove debug log from MimeUtil (snagel)diff --git a/build.xml b/build.xmlindex be49b4f52..9c79d6bca 100644--- a/build.xml+++ b/build.xml@@ -200,6 +200,7 @@<packageset dir="${plugins.dir}/protocol-httpclient/src/java"/><packageset dir="${plugins.dir}/protocol-selenium/src/java"/><packageset dir="${plugins.dir}/scoring-depth/src/java"/>+ <packageset dir="${plugins.dir}/scoring-similarity/src/java"/><packageset dir="${plugins.dir}/scoring-link/src/java"/><packageset dir="${plugins.dir}/scoring-opic/src/java"/><packageset dir="${plugins.dir}/subcollection/src/java"/>@@ -610,6 +611,7 @@<packageset dir="${plugins.dir}/protocol-httpclient/src/java"/><packageset dir="${plugins.dir}/protocol-selenium/src/java"/><packageset dir="${plugins.dir}/scoring-depth/src/java"/>+ <packageset dir="${plugins.dir}/scoring-similarity/src/java"/><packageset dir="${plugins.dir}/scoring-link/src/java"/><packageset dir="${plugins.dir}/scoring-opic/src/java"/><packageset dir="${plugins.dir}/subcollection/src/java"/>@@ -1019,6 +1021,7 @@<source path="${plugins.dir}/protocol-selenium/src/java"/><source path="${plugins.dir}/protocol-selenium/src/test"/><source path="${plugins.dir}/scoring-depth/src/java/" />+ <source path="${plugins.dir}/scoring-similarity/src/java/" /><source path="${plugins.dir}/scoring-link/src/java/" /><source path="${plugins.dir}/scoring-opic/src/java/" /><source path="${plugins.dir}/subcollection/src/java/" />diff --git a/default.properties b/default.propertiesindex 2068dc760..fc5a1bf5b 100644--- a/default.properties+++ b/default.properties@@ -87,7 +87,8 @@ plugins.protocol=\org.apache.nutch.protocol.file*:\org.apache.nutch.protocol.ftp*:\org.apache.nutch.protocol.http*:\- org.apache.nutch.protocol.httpclient*+ org.apache.nutch.protocol.httpclient*:\+ org.apache.nutch.protocol.selenium*## URL Filter Plugins@@ -118,6 +119,7 @@ plugins.scoring=\org.apache.nutch.scoring.depth*:\org.apache.nutch.scoring.link*:\org.apache.nutch.scoring.opic*:\+ org.apache.nutch.scoring.similarity*:\org.apache.nutch.scoring.tld*:\org.apache.nutch.scoring.urlmeta*diff --git a/src/plugin/build.xml b/src/plugin/build.xmlindex e2e8f5db7..a82878632 100755--- a/src/plugin/build.xml+++ b/src/plugin/build.xml@@ -62,6 +62,7 @@<ant dir="scoring-depth" target="deploy"/><ant dir="scoring-opic" target="deploy"/><ant dir="scoring-link" target="deploy"/>+ <ant dir="scoring-similarity" target="deploy"/><ant dir="subcollection" target="deploy"/><ant dir="tld" target="deploy"/><ant dir="urlfilter-automaton" target="deploy"/>@@ -165,6 +166,7 @@<ant dir="scoring-depth" target="clean"/><ant dir="scoring-opic" target="clean"/><ant dir="scoring-link" target="clean"/>+ <ant dir="scoring-similarity" target="clean"/><ant dir="subcollection" target="clean"/><ant dir="tld" target="clean"/><ant dir="urlfilter-automaton" target="clean"/>diff --git a/src/plugin/scoring-similarity/build.xml b/src/plugin/scoring-similarity/build.xmlnew file mode 100644index 000000000..66ac8f34a--- /dev/null+++ b/src/plugin/scoring-similarity/build.xml@@ -0,0 +1,27 @@+<?xml version="1.0"?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You 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.+-->+<project name="scoring-similarity" default="jar-core">++ <import file="../build-plugin.xml"/>++ <!-- Deploy Unit test dependencies -->+ <target name="deps-test">+ <ant target="deploy" inheritall="false" dir="../nutch-extensionpoints"/>+ </target>++</project>diff --git a/src/plugin/scoring-similarity/ivy.xml b/src/plugin/scoring-similarity/ivy.xmlnew file mode 100644index 000000000..1a86d6803--- /dev/null+++ b/src/plugin/scoring-similarity/ivy.xml@@ -0,0 +1,41 @@+<?xml version="1.0" ?>++<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You 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.+-->++<ivy-module version="1.0">+ <info organisation="org.apache.nutch" module="${ant.project.name}">+ <license name="Apache 2.0"/>+ <ivyauthor name="Apache Nutch Team" url="http://nutch.apache.org"/>+ <description>+ Apache Nutch+ </description>+ </info>++ <configurations>+ <include file="../../..//ivy/ivy-configurations.xml"/>+ </configurations>++ <publications>+ <!--get the artifact from our module name-->+ <artifact conf="master"/>+ </publications>++ <dependencies>+ </dependencies>++</ivy-module>diff --git a/src/plugin/scoring-similarity/plugin.xml b/src/plugin/scoring-similarity/plugin.xmlnew file mode 100644index 000000000..ee22a16c1--- /dev/null+++ b/src/plugin/scoring-similarity/plugin.xml@@ -0,0 +1,39 @@+<?xml version="1.0" encoding="UTF-8"?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You 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.+-->+<plugin+ id="scoring-similarity"+ name="Similarity based Scoring Plug-in"+ version="1.0.0"+ provider-name="nutch.org">+++ <runtime>+ <library name="scoring-similarity.jar">+ <export name="*"/>+ </library>+ </runtime>++ <extension id="org.apache.nutch.scoring.similarity"+ name="SimilarityScoring"+ point="org.apache.nutch.scoring.ScoringFilter">++ <implementation id="scoring-similarity"+ class="org.apache.nutch.scoring.similarity.SimilarityScoringFilter" />+ </extension>++</plugin>diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/Cosine/CosineSimilarityModel.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/Cosine/CosineSimilarityModel.javanew file mode 100644index 000000000..0b9fd9478--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/Cosine/CosineSimilarityModel.java@@ -0,0 +1,156 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.apache.nutch.scoring.similarity.cosine;++import java.io.IOException;+import java.io.Reader;+import java.util.Collection;+import java.util.Map;+import java.util.Map.Entry;++import org.apache.commons.io.IOUtils;+import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.io.Text;+import org.apache.hadoop.util.StringUtils;+import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.metadata.Nutch;+import org.apache.nutch.parse.Parse;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.protocol.Content;+import org.apache.nutch.scoring.similarity.SimilarityModel;+import org.apache.nutch.scoring.similarity.cosine.CosineSimilarityModel;+import org.apache.nutch.scoring.similarity.cosine.DocumentVector;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class CosineSimilarityModel implements SimilarityModel{++ private Configuration conf;+ private String goldStandardDocPath;+ private static DocumentVector goldStandardDocVect;+ private final static Logger LOG = LoggerFactory+ .getLogger(CosineSimilarityModel.class);++ private double calculateCosineSimilarity(String goldStandard, String document2){++ DocumentVector docVect1 = new DocumentVector(goldStandard, conf);+ DocumentVector docVect2 = new DocumentVector(document2, conf);++ double doc1Dist = getEuclideanDist(docVect1);+ double doc2Dist = getEuclideanDist(docVect2);++ double dotProduct = getDotProduct(docVect1, docVect2);+ if(doc1Dist*doc2Dist == 0){+ return 0.0;+ }+ return dotProduct/(doc1Dist*doc2Dist);+ }++ private double calculateCosineSimilarity(DocumentVector docVect1, DocumentVector docVect2){++ double doc1Dist = getEuclideanDist(docVect1);+ double doc2Dist = getEuclideanDist(docVect2);++ double dotProduct = getDotProduct(docVect1, docVect2);+ if(doc1Dist*doc2Dist == 0){+ return 0.0;+ }+ return dotProduct/(doc1Dist*doc2Dist);+ }++ private double getDotProduct(DocumentVector docVect1, DocumentVector docVect2) {+ double dotProduct = 0.0;+ Map<String, Integer> doc2TermFreqVect = docVect2.getTermFreqVect();+ for(Map.Entry<String, Integer> pair : docVect1.getTermFreqVect().entrySet()){+ double doc1value = pair.getValue();+ double doc2value = 0;++ if(doc2TermFreqVect.containsKey(pair.getKey()))+ doc2value = doc2TermFreqVect.get(pair.getKey());++ dotProduct += doc1value*doc2value;+ }++ return dotProduct;+ }++ private double getEuclideanDist(DocumentVector docVect) {+ float sum = 0f;+ for(Map.Entry<String, Integer> pair : docVect.getTermFreqVect().entrySet()){+ sum += pair.getValue() * pair.getValue();+ }+ return Math.sqrt(sum);+ }++ @Override+ public void setConf(Configuration conf) {+ this.conf = conf;+ goldStandardDocPath = conf.get("scoring.similarity.model.path");+ Reader reader = conf.getConfResourceAsReader(goldStandardDocPath);+ try {+ String fileContent = IOUtils.toString(reader);+ if(goldStandardDocVect == null) {+ goldStandardDocVect = new DocumentVector(fileContent, conf);+ LOG.info("Creating DocVector from path - {}",goldStandardDocPath);+ }+ } catch (IOException e) {+ LOG.error("Failed to create Document vector : {}",StringUtils.stringifyException(e));+ e.printStackTrace();+ }+ }+ @Override+ public float setURLScoreAfterParsing(Text url, Content content, Parse parse) {++ DocumentVector parseTextDocVect = new DocumentVector(parse.getText(), conf);+ double parseTextSimilarity = calculateCosineSimilarity(goldStandardDocVect, parseTextDocVect);++ DocumentVector parseMetaKeywordDocVect = new DocumentVector(parse.getData().getParseMeta().get("metatag.keyword"), conf);+ double metaKeywordSimilarity = calculateCosineSimilarity(goldStandardDocVect, parseMetaKeywordDocVect);++ DocumentVector parseMetaDescDocVect = new DocumentVector(parse.getData().getParseMeta().get("metatag.description"), conf);+ double metaDescriptionSimilarity = calculateCosineSimilarity(goldStandardDocVect, parseMetaDescDocVect);++ LOG.info("Calculating similarity between gold-standard and {}",url);+ int count = 0;+ if(parseTextSimilarity!=0)+ count++;+ if(metaDescriptionSimilarity!=0)+ count++;+ if(metaKeywordSimilarity!=0)+ count++;+ if(count==0)+ count++;++ float score = (float) ((parseTextSimilarity+metaDescriptionSimilarity + metaKeywordSimilarity)/count);+ LOG.info("Setting score of {} to {}",url, score);+ LOG.info("Score break down TextSimilarity : {}, metaKeywordSimilarity : {}, metaDescriptionSimilarity : {}",+ parseTextSimilarity, metaKeywordSimilarity, metaDescriptionSimilarity);+ return score;+ }++ @Override+ public CrawlDatum distributeScoreToOutlinks(Text fromUrl,+ ParseData parseData, Collection<Entry<Text, CrawlDatum>> targets,+ CrawlDatum adjust, int allCount) {+ float score = Float.parseFloat(parseData.getContentMeta().get(Nutch.SCORE_KEY));+ for (Entry<Text, CrawlDatum> target : targets) {+ target.getValue().setScore((float)score);+ LOG.info("Setting score of {} to {}",target.getKey(), score);+ }+ return adjust;+ }+}diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/Cosine/DocumentVector.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/Cosine/DocumentVector.javanew file mode 100644index 000000000..416253848--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/Cosine/DocumentVector.java@@ -0,0 +1,93 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.apache.nutch.scoring.similarity.cosine;++import java.io.IOException;+import java.io.Reader;+import java.util.HashMap;+import java.util.HashSet;+import java.util.Map;+import java.util.Set;++import org.apache.commons.io.IOUtils;+import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.util.StringUtils;+import org.apache.nutch.scoring.similarity.cosine.DocumentVector;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class DocumentVector {++ private Map<String, Integer> termFreqVect;+ private static Set<String> stopwordSet;+ private final static Logger LOG = LoggerFactory+ .getLogger(DocumentVector.class);++ public DocumentVector(String text, Configuration conf){++ termFreqVect = new HashMap<String, Integer>();+ if(stopwordSet == null){+ stopwordSet = new HashSet<String>();+ populateStopWordSet(conf);+ }+ createDocVect(text);+ removeStopWords(conf);+ }++ private void createDocVect(String text){+ if(text!=null){+ String[] tokens = text.replaceAll("[^a-zA-Z0-9 ]", " ").toLowerCase().split("\\s+");+ for(String token: tokens){+ if(termFreqVect.containsKey(token)){+ int count = termFreqVect.get(token)+1;+ termFreqVect.put(token, count);+ }+ else+ termFreqVect.put(token, 1);+ }+ }+ }++ public Map<String, Integer> getTermFreqVect(){+ return termFreqVect;+ }++ private void removeStopWords(Configuration conf){+ for(String stopWord: stopwordSet){+ stopWord = stopWord.trim();+ if(termFreqVect.containsKey(stopWord)){+ termFreqVect.remove(stopWord);+ }+ }+ }++ private void populateStopWordSet(Configuration conf){+ String stopWordFilePath = conf.get("scoring.similarity.stopword.file");+ Reader reader = conf.getConfResourceAsReader(stopWordFilePath);+ try {+ LOG.info("Populating stopwords from {}", stopWordFilePath);+ String[] stopWordList1 = IOUtils.toString(reader).split("\n");+ for(String stopWord: stopWordList1){+ stopWord = stopWord.trim();+ stopwordSet.add(stopWord);+ }+ } catch (IOException e) {+ LOG.error("Failed to populate stopwords : {}", StringUtils.stringifyException(e));+ e.printStackTrace();+ }+ }+}diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/CosineSimilarity.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/CosineSimilarity.javanew file mode 100644index 000000000..a6b85f2a6--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/CosineSimilarity.java@@ -0,0 +1,80 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.apache.nutch.scoring.similarity;++import java.util.Map;++import org.apache.hadoop.conf.Configuration;++public class CosineSimilarity {++ private Configuration conf;++ public CosineSimilarity(Configuration conf){+ this.conf = conf;+ }+ public double calculateCosineSimilarity(String goldStandard, String document2){++ DocumentVector docVect1 = new DocumentVector(goldStandard, conf);+ DocumentVector docVect2 = new DocumentVector(document2, conf);++ double doc1Dist = getEuclideanDist(docVect1);+ double doc2Dist = getEuclideanDist(docVect2);++ double dotProduct = getDotProduct(docVect1, docVect2);+ if(doc1Dist*doc2Dist == 0){+ return 0.0;+ }+ return dotProduct/(doc1Dist*doc2Dist);+ }++ public double calculateCosineSimilarity(DocumentVector docVect1, DocumentVector docVect2){++ double doc1Dist = getEuclideanDist(docVect1);+ double doc2Dist = getEuclideanDist(docVect2);++ double dotProduct = getDotProduct(docVect1, docVect2);+ if(doc1Dist*doc2Dist == 0){+ return 0.0;+ }+ return dotProduct/(doc1Dist*doc2Dist);+ }++ private double getDotProduct(DocumentVector docVect1, DocumentVector docVect2) {+ double dotProduct = 0.0;+ Map<String, Integer> doc2TermFreqVect = docVect2.getTermFreqVect();+ for(Map.Entry<String, Integer> pair : docVect1.getTermFreqVect().entrySet()){+ double doc1value = pair.getValue();+ double doc2value = 0;++ if(doc2TermFreqVect.containsKey(pair.getKey()))+ doc2value = doc2TermFreqVect.get(pair.getKey());++ dotProduct += doc1value*doc2value;+ }++ return dotProduct;+ }++ private double getEuclideanDist(DocumentVector docVect) {+ float sum = 0f;+ for(Map.Entry<String, Integer> pair : docVect.getTermFreqVect().entrySet()){+ sum += pair.getValue() * pair.getValue();+ }+ return Math.sqrt(sum);+ }+}diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/DocumentVector.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/DocumentVector.javanew file mode 100644index 000000000..208db671d--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/DocumentVector.java@@ -0,0 +1,72 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.apache.nutch.scoring.similarity;++import java.io.IOException;+import java.io.Reader;+import java.util.HashMap;+import java.util.Map;++import org.apache.commons.io.IOUtils;+import org.apache.hadoop.conf.Configuration;++public class DocumentVector {++ Map<String, Integer> termFreqVect;++ public DocumentVector(String text, Configuration conf){++ termFreqVect = new HashMap<String, Integer>();+ createDocVect(text);+ removeStopWords(conf);+ }++ private void createDocVect(String text){+ if(text!=null){+ String[] tokens = text.replaceAll("[^a-zA-Z0-9 ]", " ").toLowerCase().split("\\s+");+ for(String token: tokens){+ if(termFreqVect.containsKey(token)){+ int count = termFreqVect.get(token)+1;+ termFreqVect.put(token, count);+ }+ else+ termFreqVect.put(token, 1);+ }+ }+ }++ public Map<String, Integer> getTermFreqVect(){+ return termFreqVect;+ }++ private void removeStopWords(Configuration conf){+ String stopWordFilePath = conf.get("scoring.similarity.stopword.file");+ Reader reader = conf.getConfResourceAsReader(stopWordFilePath);+ try {+ String[] stopWordList1 = IOUtils.toString(reader).split("\n");+ for(String stopWord: stopWordList1){+ stopWord = stopWord.trim();+ if(termFreqVect.containsKey(stopWord)){+ termFreqVect.remove(stopWord);+ }+ }+ } catch (IOException e) {+ // TODO Auto-generated catch block+ e.printStackTrace();+ }+ }+}diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/ScoringFilterModel.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/ScoringFilterModel.javanew file mode 100644index 000000000..4d6c021ee--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/ScoringFilterModel.java@@ -0,0 +1,12 @@+package org.apache.nutch.scoring.similarity;++/**+ * This interface defines the methods to be implemented to+ * create a plugable model in the scoring-similarity based filter+ * @author Sujen Shah+ *+ */+public interface ScoringFilterModel {++ public int score();+}diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/SimilarityModel.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/SimilarityModel.javanew file mode 100644index 000000000..f44fabd76--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/SimilarityModel.java@@ -0,0 +1,38 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.apache.nutch.scoring.similarity;++import java.util.Collection;+import java.util.Map.Entry;++import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.io.Text;+import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.parse.Parse;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.protocol.Content;++public interface SimilarityModel {++ public void setConf(Configuration conf);++ public float setURLScoreAfterParsing(Text url, Content content, Parse parse);++ public CrawlDatum distributeScoreToOutlinks(Text fromUrl,+ ParseData parseData, Collection<Entry<Text, CrawlDatum>> targets,+ CrawlDatum adjust, int allCount);+}diff --git a/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/SimilarityScoringFilter.java b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/SimilarityScoringFilter.javanew file mode 100644index 000000000..622a62ec5--- /dev/null+++ b/src/plugin/scoring-similarity/src/java/org/apache/nutch/scoring/similarity/SimilarityScoringFilter.java@@ -0,0 +1,66 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.apache.nutch.scoring.similarity;++import java.util.Collection;+import java.util.Map.Entry;++import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.io.Text;+import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.metadata.Nutch;+import org.apache.nutch.parse.Parse;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.protocol.Content;+import org.apache.nutch.scoring.AbstractScoringFilter;+import org.apache.nutch.scoring.ScoringFilterException;+import org.apache.nutch.scoring.similarity.cosine.CosineSimilarityModel;++public class SimilarityScoringFilter extends AbstractScoringFilter {++ private Configuration conf;+ private SimilarityModel similarityModel;++ @Override+ public Configuration getConf() {+ return conf;+ }++ @Override+ public void setConf(Configuration conf) {+ this.conf = conf;+ similarityModel = new CosineSimilarityModel();+ similarityModel.setConf(conf);+ }++ @Override+ public void passScoreAfterParsing(Text url, Content content, Parse parse)+ throws ScoringFilterException {++ float score = similarityModel.setURLScoreAfterParsing(url, content, parse);+ parse.getData().getContentMeta()+ .set(Nutch.SCORE_KEY, score+"");+ }++ @Override+ public CrawlDatum distributeScoreToOutlinks(Text fromUrl,+ ParseData parseData, Collection<Entry<Text, CrawlDatum>> targets,+ CrawlDatum adjust, int allCount) throws ScoringFilterException {+ similarityModel.distributeScoreToOutlinks(fromUrl, parseData, targets, adjust, allCount);+ return adjust;+ }+}