Skip to content

Commit

Permalink
working version of Bing
Browse files Browse the repository at this point in the history
  • Loading branch information
searchivarius committed Aug 29, 2014
1 parent 4348b85 commit 4330e70
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 3 deletions.
26 changes: 26 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="var" path="ECLIPSE_HOME"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>brmson-cmu</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
92 changes: 92 additions & 0 deletions src/main/java/edu/cmu/lti/oaqa/util/BingSearcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2014 Carnegie Mellon University
*
* 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 edu.cmu.lti.oaqa.util;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import edu.cmu.lti.oaqa.util.WebSearchCache;

public class BingSearcher {
private String mCacheId = "Bing";
private WebSearchCache mRetrievalCache = null;
private Hashtable<String, ArrayList<SearchResult>> mCacheStorage = null;;
private int mResNum = 100;
private String mAccountKey;


/**
*
* Initialize a Bing retrieval class.
*
* @param accountKey A Bing API key for the Web search,
* can be retrieved at https://datamarket.azure.com/dataset/bing/searchweb.
* @param cachePath A path to the file, where we cache retrieved results.
* @param resNum A maximum number of results to return.
*
*/
BingSearcher(String accountKey,
String cachePath,
int resNum) {
mAccountKey = accountKey;
mResNum = resNum;
mRetrievalCache = new WebSearchCache(cachePath);
mCacheStorage = mRetrievalCache.loadCache(mCacheId);
if (mCacheStorage == null)
mCacheStorage = new Hashtable<String, ArrayList<SearchResult>>();

}

public ArrayList<SearchResult> retrieveDocuments(String query) throws Exception {
ArrayList<SearchResult> resultL = new ArrayList<SearchResult>();
query = query.trim();
if (query.isEmpty()) return resultL;

String requestURL = BingSearcherUtil.BuildRequest(query, mResNum);

System.out.println("Bing Search : " + query);
BingSearcherUtil.getResults(mAccountKey,
mRetrievalCache, mCacheStorage,
resultL, query, requestURL, mCacheId);

System.out.println("Result size: " + resultL.size());


return resultL;
}


public static void main(String[] args) throws Exception {
String accountId = args[0];
String cachePath = args[1];
String query = args[2];

BingSearcher web = new BingSearcher(accountId, cachePath, 50);

System.out.println("Query: '" + query + "'");
System.out.println("Cache path: " + cachePath);

ArrayList<SearchResult> res = web.retrieveDocuments(query);

for (int i = 0; i < res.size(); ++i) {
SearchResult o = res.get(i);
System.out.println(i + " " + o.getDocID() + " " + o.getAnswer());
}
}

}
5 changes: 2 additions & 3 deletions src/main/java/edu/cmu/lti/oaqa/util/BingSearcherUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package edu.cmu.lti.oaqa.util;

import java.io.BufferedReader;
Expand Down Expand Up @@ -123,7 +122,7 @@ public static Document GetResponse(String requestURL, String AccountKey)
* Parses XML and extract results
*/

public static List<SearchResult> ProcessResponse(Document doc, String query)
public static ArrayList<SearchResult> ProcessResponse(Document doc, String query)
throws XPathExpressionException {
factory = XPathFactory.newInstance();
xpath = factory.newXPath();
Expand Down Expand Up @@ -155,7 +154,7 @@ public static List<SearchResult> ProcessResponse(Document doc, String query)

NodeList nodes = (NodeList) xpath.evaluate(
"/default:feed/default:entry", doc, XPathConstants.NODESET);
List<SearchResult> Reply = new ArrayList<SearchResult>();
ArrayList<SearchResult> Reply = new ArrayList<SearchResult>();

for (int i = 0; i < nodes.getLength(); i++) {
try {
Expand Down

0 comments on commit 4330e70

Please sign in to comment.