Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove explicit link, store it as field, add optional key, fromEntryM…

…ap takes kind+key now
  • Loading branch information...
commit eeb598efa8a72ff5af862339e1b9ec932305d5ab 1 parent 173f7bf
@colder colder authored
View
31 src/main/scala/bibimbap/LuceneBackend.scala
@@ -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(_))
@@ -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)
@@ -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)
@@ -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)
}
}
View
2  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)
View
11 src/main/scala/bibimbap/bibtex/BibTeXEntry.scala
@@ -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 {
@@ -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 ")))
@@ -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 = (
@@ -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]()
@@ -273,7 +274,7 @@ object BibTeXEntry {
}
}
- Some(BibTeXEntry(tpe, fields, seqFields))
+ Some(BibTeXEntry(tpe, key, fields, seqFields))
} catch {
case InconsistentBibTeXEntry(msg) =>
onError(msg)
View
3  src/main/scala/bibimbap/bibtex/BibTeXParser.scala
@@ -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]] = {
View
8 src/main/scala/bibimbap/modules/Managed.scala
@@ -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)
}
}
}
@@ -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)
View
2  src/main/scala/bibimbap/modules/Search.scala
@@ -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)
View
2  src/main/scala/bibimbap/modules/SearchBibtex.scala
@@ -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
View
15 src/main/scala/bibimbap/modules/SearchDBLP.scala
@@ -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 {
@@ -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") => {
@@ -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),
@@ -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))
}
}
View
2  src/main/scala/bibimbap/modules/SearchLocal.scala
@@ -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)
}
}
View
8 src/main/scala/bibimbap/modules/Wizard.scala
@@ -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
@@ -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)

0 comments on commit eeb598e

Please sign in to comment.
Something went wrong with that request. Please try again.