Skip to content

Commit

Permalink
Remove explicit link, store it as field, add optional key, fromEntryM…
Browse files Browse the repository at this point in the history
…ap takes kind+key now
  • Loading branch information
colder committed Aug 29, 2012
1 parent 173f7bf commit eeb598e
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 34 deletions.
31 changes: 19 additions & 12 deletions src/main/scala/bibimbap/LuceneBackend.scala
Expand Up @@ -38,16 +38,15 @@ trait LuceneBackend {
def searchLucene(query: String): List[SearchResult] =
searchEntries(query).flatMap{ case (doc, score) => documentToSearchResult(doc, score) }.toList

def addEntry(entry : BibTeXEntry, link : Option[String]) : Unit = {
def addEntry(entry : BibTeXEntry) : Unit = {
val doc = new Document()

for((k,v) <- entry.entryMap) {
doc.add(new Field(k, v.toJava, Field.Store.YES, Field.Index.NO))
}

for(url <- link) {
doc.add(new Field("url", url, Field.Store.YES, Field.Index.NO))
}
doc.add(new Field("__key", entry.key.getOrElse(""), Field.Store.YES, Field.Index.NO))
doc.add(new Field("__type", entry.tpe.toString, Field.Store.YES, Field.Index.NO))

val sb = new StringBuilder()
entry.title.foreach(sb.append(_))
Expand All @@ -60,7 +59,7 @@ trait LuceneBackend {
entry.booktitle.foreach(b => sb.append(b.toJava))
entry.year.foreach(sb.append(_))

doc.add(new Field("blob", sb.toString, Field.Store.NO, Field.Index.ANALYZED))
doc.add(new Field("__blob", sb.toString, Field.Store.NO, Field.Index.ANALYZED))

val config = new IndexWriterConfig(Version.LUCENE_36, analyzer)
val writer = new IndexWriter(index, config)
Expand All @@ -69,7 +68,7 @@ trait LuceneBackend {
}

private def searchEntries(query : String) : Iterable[(Document, Double)] = {
val q = new QueryParser(Version.LUCENE_36, "blob", analyzer).parse(query)
val q = new QueryParser(Version.LUCENE_36, "__blob", analyzer).parse(query)
val hitsPerPage = 10
val reader = IndexReader.open(index)
val searcher = new IndexSearcher(reader)
Expand All @@ -83,13 +82,21 @@ trait LuceneBackend {

private def documentToSearchResult(document : Document, score: Double) : Option[SearchResult] = {
import scala.collection.JavaConversions._
val em : Map[String,MString] = document.getFields().map(f =>
(f.name -> MString.fromJava(f.stringValue))
).toMap
val em : Map[String,MString] = document.getFields().collect{
case f if !f.name.startsWith("__") =>
(f.name -> MString.fromJava(f.stringValue))
}.toMap

val optKey = document.get("__key") match {
case null => None
case "" => None
case s => Some(s)
}

val kind = BibTeXEntryTypes.withName(document.get("__type"))

for(entry <- BibTeXEntry.fromEntryMap(em, console ! Error(_))) yield {
val url = Option(document.get("url"))
SearchResult(entry, url, Set(source), score)
for(entry <- BibTeXEntry.fromEntryMap(kind, optKey, em, console ! Error(_))) yield {
SearchResult(entry, Set(source), score)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/bibimbap/SearchResult.scala
@@ -1,3 +1,3 @@
package bibimbap

case class SearchResult(entry: bibtex.BibTeXEntry, link: Option[String], sources: Set[String], relevance: Double)
case class SearchResult(entry: bibtex.BibTeXEntry, sources: Set[String], relevance: Double)
11 changes: 6 additions & 5 deletions src/main/scala/bibimbap/bibtex/BibTeXEntry.scala
Expand Up @@ -73,6 +73,7 @@ case class InconsistentBibTeXEntry(msg: String) extends Exception(msg)
// This datatypes and all the following ones assume crossrefs have been
// "resolved" into all entries.
case class BibTeXEntry(tpe: BibTeXEntryTypes.BibTeXEntryType,
key: Option[String],
fields: Map[String, MString],
seqFields: Map[String, Seq[MString]]) extends Serializable {

Expand Down Expand Up @@ -105,6 +106,8 @@ case class BibTeXEntry(tpe: BibTeXEntryTypes.BibTeXEntryType,
val url : Option[MString] = fields.get("url")
val volume : Option[MString] = fields.get("volume")
val year : Option[MString] = fields.get("year")
val link : Option[MString] = fields.get("link")
val doi : Option[MString] = fields.get("doi")

lazy val entryMap = {
Map("type" -> MString.fromJava(tpe.toString)) ++ fields ++ seqFields.mapValues(seq => MString.fromJava(seq.map(_.toJava).mkString(" and ")))
Expand All @@ -119,7 +122,7 @@ case class BibTeXEntry(tpe: BibTeXEntryTypes.BibTeXEntryType,
missingReqFields.isEmpty
}

def getKey: String = {
def getKey: String = key.getOrElse {
val commonWords = Set("", "in", "the", "a", "an", "of", "for", "and", "or", "by", "on", "with")

def isBibTeXFriendly(c : Char) : Boolean = (
Expand Down Expand Up @@ -255,10 +258,8 @@ case class BibTeXEntry(tpe: BibTeXEntryTypes.BibTeXEntryType,
}

object BibTeXEntry {
def fromEntryMap(map : Map[String,MString], onError: String => Unit) : Option[BibTeXEntry] = {
def fromEntryMap(tpe: BibTeXEntryTypes.BibTeXEntryType, key: Option[String], map : Map[String,MString], onError: String => Unit) : Option[BibTeXEntry] = {
try {
val tpe = BibTeXEntryTypes.withName(map.get("type").map(_.toJava).getOrElse(throw new InconsistentBibTeXEntry("Missing type information")))

val isSeqField = Set("authors", "editors")

var fields = Map[String, MString]()
Expand All @@ -273,7 +274,7 @@ object BibTeXEntry {
}
}

Some(BibTeXEntry(tpe, fields, seqFields))
Some(BibTeXEntry(tpe, key, fields, seqFields))
} catch {
case InconsistentBibTeXEntry(msg) =>
onError(msg)
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/bibimbap/bibtex/BibTeXParser.scala
Expand Up @@ -35,7 +35,8 @@ class BibTeXParser(src : Source, error : String=>Unit) {

def entries : Stream[BibTeXEntry] = rawEntries.flatten.flatMap { raw =>
val newMap : Map[String,MString] = raw.pairs.mapValues(s => MString.fromJava(s))
BibTeXEntry.fromEntryMap(newMap.updated("type", MString.fromJava(raw.kind)), error)
val kind = BibTeXEntryTypes.withName(raw.kind)
BibTeXEntry.fromEntryMap(kind, Some(raw.key), newMap, error)
}

private def rawEntries : Stream[Option[RawEntry]] = {
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/bibimbap/modules/Managed.scala
Expand Up @@ -26,7 +26,7 @@ class Managed(val repl: ActorRef, val console: ActorRef, val settings: Settings)
if(managedFile.exists && managedFile.isFile && managedFile.canRead) {
val parser = new BibTeXParser(Source.fromFile(managedFile), console ! Error(_))
for (entry <- parser.entries) {
addEntry(entry, None)
addEntry(entry)
}
}
}
Expand All @@ -46,14 +46,16 @@ class Managed(val repl: ActorRef, val console: ActorRef, val settings: Settings)
sender ! CommandSuccess

case ImportedResult(res) =>
addEntry(res.entry, res.link)
// no message back
// NOOP: we sent this.

case msg =>
super[Module].receive(msg)
}

private def doImport(res: SearchResult) {

addEntry(res.entry)

import java.io.{FileWriter,File}

val fw = new FileWriter(new File(managedPath), true)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/bibimbap/modules/Search.scala
Expand Up @@ -53,7 +53,7 @@ class Search(val repl: ActorRef, val console: ActorRef, val settings: Settings,
val groupedByEntry = resultss.flatMap(_.entries).groupBy(_.entry.getKey).values

val combined = for (res <- groupedByEntry) yield {
SearchResult(res.head.entry, res.head.link, res.flatMap(_.sources).toSet, res.map(_.relevance).min)
SearchResult(res.head.entry, res.flatMap(_.sources).toSet, res.map(_.relevance).min)
}

val sorted = combined.toList.sortBy(- _.relevance)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/bibimbap/modules/SearchBibtex.scala
Expand Up @@ -20,7 +20,7 @@ class SearchBibtex(val repl: ActorRef, val console: ActorRef, val settings: Sett
val parser = new BibTeXParser(Source.fromFile(path), console ! Error(_))

for (entry <- parser.entries) {
addEntry(entry, None)
addEntry(entry)
}

sender ! CommandSuccess
Expand Down
15 changes: 9 additions & 6 deletions src/main/scala/bibimbap/modules/SearchDBLP.scala
Expand Up @@ -87,6 +87,8 @@ class SearchDBLP(val repl: ActorRef, val console: ActorRef, val settings: Settin
case _ => 0d
}

val optKey = None;

(record \ "title") match {
case obj : JObject => {
val authors : MString = ((obj \ "dblp:authors" \ "dblp:author") match {
Expand Down Expand Up @@ -120,16 +122,17 @@ class SearchDBLP(val repl: ActorRef, val console: ActorRef, val settings: Settin
case _ => (None, None, None)
}

val entry = BibTeXEntry.fromEntryMap(Map[String, MString](
"type" -> BibTeXEntryTypes.Proceedings.toString,
val entry = BibTeXEntry.fromEntryMap(BibTeXEntryTypes.InProceedings, optKey,
Map[String, MString](
"title" -> title,
"authors" -> authors,
"booktitle" -> venue.map(MString.fromJava).getOrElse(unknown),
"year" -> yr2yr(venueYear).getOrElse(year.getOrElse(unknown)),
"pages" -> pages.map(MString.fromJava).getOrElse(unknown)
), console ! Error(_))
), console ! Error(_))

entry.map(SearchResult(_, link, Set(source), score))
// ADD Link
entry.map(SearchResult(_, Set(source), score))
}

case JString("article") => {
Expand All @@ -147,7 +150,6 @@ class SearchDBLP(val repl: ActorRef, val console: ActorRef, val settings: Settin
None
} else {
var map = Map[String, MString](
"type" -> BibTeXEntryTypes.Article.toString,
"authors" -> authors,
"title" -> title,
"journal" -> jour.map(MString.fromJava).getOrElse(unknown),
Expand All @@ -164,7 +166,8 @@ class SearchDBLP(val repl: ActorRef, val console: ActorRef, val settings: Settin
map += "pages" -> MString.fromJava(pgs.get)
}

BibTeXEntry.fromEntryMap(map, console ! Error(_)).map(SearchResult(_, link, Set(source), score))
//TODO: Add Link
BibTeXEntry.fromEntryMap(BibTeXEntryTypes.Article, optKey, map, console ! Error(_)).map(SearchResult(_, Set(source), score))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/bibimbap/modules/SearchLocal.scala
Expand Up @@ -15,6 +15,6 @@ class SearchLocal(val repl: ActorRef, val console: ActorRef, val settings: Setti
protected val cacheDir = new File(settings("general", "dir.cache"))

override def onImport(res: SearchResult) {
addEntry(res.entry, res.link)
addEntry(res.entry)
}
}
8 changes: 5 additions & 3 deletions src/main/scala/bibimbap/modules/Wizard.scala
Expand Up @@ -30,8 +30,10 @@ class Wizard(val repl: ActorRef, val console: ActorRef, val settings: Settings)
}

def doEdit(res: SearchResult): SearchResult = {
val entry = res.entry;
var map = entry.entryMap
val entry = res.entry
var map = entry.entryMap
var key = entry.key
var kind = entry.tpe

val allStdFields = BibTeXEntryTypes.allStdFields
val meaningFulFields = entry.stdFields
Expand Down Expand Up @@ -111,7 +113,7 @@ class Wizard(val repl: ActorRef, val console: ActorRef, val settings: Settings)
console ! Success("Edit cancelled!")
res
} else {
BibTeXEntry.fromEntryMap(map, console ! Error(_)) match {
BibTeXEntry.fromEntryMap(kind, key, map, console ! Error(_)) match {
case Some(entry) =>
console ! Success("Entry edited!")
res.copy(entry = entry)
Expand Down

0 comments on commit eeb598e

Please sign in to comment.