Mime Magic With Apache Tika
-
Upload
jukka-zitting -
Category
Technology
-
view
6.700 -
download
0
description
Transcript of Mime Magic With Apache Tika
MIME Magic withMIME Magic withApache TikaApache Tika
Jukka ZittingJukka Zitting
YOU don't know about me without you have read a book by the name of The Adventures of Tom Sawyer; but that ain't no matter. That book was made by Mr. Mark Twain, and he told the truth, mainly. There was things which he stretched, but mainly he told the truth. That is nothing. I never seen anybody but lied one time or another, without it was Aunt Polly, or the widow, or maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and Mary, and the Widow Douglas is all told about in that book, which is mostly a true book, with some stretchers, as I said before…
3 May. Bistritz.--Left Munich at 8:35 P.M., on 1st May, arriving at Vienna early next morning; should have arrived at 6:46, but train was an hour late. Buda-Pesth seems a wonderful place, from the glimpse which I got of it from the train and the little I could walk through the streets. I feared to go very far from the station, as we had arrived late and would start as near the correct time as possible. The impression I had was that we were leaving the West and entering the East; the most western of splendid bridges over the Danube, which is here of noble width and depth, took us among the traditions of Turkish rule…
To Sherlock Holmes she is always THE woman. I have seldom heard him mention her under any other name. In his eyes she eclipses and predominates the whole of her sex. It was not that he felt any emotion akin to love for Irene Adler. All emotions, and that one particularly, were abhorrent to his cold, precise but admirably balanced mind. He was, I take it, the most perfect reasoning and observing machine that the world has seen, but as a lover he would have placed himself in a false position. He never spoke of the softer passions, save with a gibe and a sneer. They were admirable things for the observer--excellent for drawing the veil from men's motives and actions. But for the trained reasoner to admit such intrusions into his own delicate and finely adjusted temperament was to introduce a distracting factor which might throw a doubt upon all his mental results. Grit in a sensitive instrument, or a crack in one of his own high-power lenses, would not be more disturbing than a strong emotion in a nature such as his. And yet there was but one woman to him, and that woman was the late Irene Adler, of dubious and questionable memory…
Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her…
It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife. However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of some one or other of their daughters. "My dear Mr. Bennet," said his lady to him one day, "have you heard that Netherfield Park is let at last?" Mr. Bennet replied that he had not. "But it is," returned she; "for Mrs. Long has just been here, and she told me all about it." Mr. Bennet made no answer…
TypeType
TextText
MetadataMetadata
import org.apache.tika.Tika;
unreleased code alertunreleased code alert
// Type detection
String type = Tika.detect(InputStream);
String type = Tika.detect(File);
String type = Tika.detect(URL);
String type = Tika.detect(String);
// Text extraction
String content = Tika.parseToString(InputStream);
String content = Tika.parseToString(File);
String content = Tika.parseToString(URL);
// Streaming text extraction
Reader reader = Tika.parse(InputStream);
Reader reader = Tika.parse(File);
Reader reader = Tika.parse(URL);
// … with metadata
String type = Tika.detect(InputStream, Metadata);
String content = Tika.parseToString( InputStream, Metadata);
Reader reader = Tika.parse(InputStream, Metadata);
How does the magic work?
// Metadata handling
import org.apache.tika.metadata.Metadata;
Metadata metadata = new Metadata();
metadata.set( Metadata.RESOURCE_NAME_KEY, “…”);
String title = metadata.get(Metadata.TITLE);
XMP?
// Type detection and the type registry
import org.apache.tika.detect.Detector;
Detector detector = TikaConfig.getDefaultConfig() .getMimeTypeRepository();
MediaType type = detector.detect( InputStream, Metadata);
// 1274 known types, 627 detectable// (927 globs, 280 magic byte patterns)
// The parser API
import org.apache.tika.parser.Parser;
Parser parser = new AutoDetectParser();
parser.parse( InputStream, ContentHandler, Metadata, Map<String, Object>);// IOException, SAXException, TikaException
// asm, audio, epub, html, image, mbox,// ms, mp3, odf, pdf, pkg, rtf, txt, xml
Parser API in detail
// Document input stream
InputStream stream = …;try { parser.parse(stream, …);} finally { stream.close(); // Remember!}
// Streaming if possible, otherwise a// temporary file is automatically used
// XHTML SAX events for structured text
ContentHandler handler = new BodyContentHandler(…); // etc.try { parser.parse(…, handler, …);} catch (SAXException e) { …;}
// <html>// <head>…<title>…</title>…</head>// <body>…</body>// </html>
// Input and output metadata
Metadata metadata = new Metadata();metadata.set( Metadata.RESOURCE_NAME_KEY, “…”);
parser.parse(…, metadata, …);
String title = metadata.get(Metadata.TITLE);
// See parser implementations for// supported metadata keys (TODO: XMP, DC)
// Parse context
Map<String, Object> context = new HashMap<String, Object>();context.put( Parser.class.getName(), new MyCustomComponentParser()); // hierarchy while streaming!
parser.parse(…, context);
// Also for Locale, XML parser, etc.
Putting it all together
// Tika components
tika-core - API/core classes, no depstika-parsers - parser librariestika-app - all in one jar + cli/gui
$ java –jar tika-app-0.x.jar < …$ java –jar tika-app-0.x.jar --text$ java –jar tika-app-0.x.jar --metadata$ java –jar tika-app-0.x.jar --help
// Tika GUI
$ java –jar tika-app-0.x.jar --gui
// Customizing Tika
org/apache/tika/tika-config.xmlorg/apache/tika/mime/tika-mimetypes.xml
// … or add custom components at runtime
org.apache.tika.detect.CompositeDetectororg.apache.tika.parser.CompositeParser
// … or submit a patch!
// In the future: OSGi support?
// Tika integration
Lucene Javadocument.add( new Field(“content”, Tika.parse(…)); // see also the ParsingReader class
Solr Cell, in Solr 1.4 http://wiki.apache.org/solr/ExtractingRequestHandler
TODO: Nutch, ports (but see Tika cli)
Questions?http://lucene.apache.org/tika/