Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/query/QueryConsole.java |
— | — | @@ -14,6 +14,8 @@ |
15 | 15 | |
16 | 16 | import de.brightbyte.data.LabeledVector; |
17 | 17 | import de.brightbyte.data.cursor.DataSet; |
| 18 | +import de.brightbyte.data.measure.ScalarVectorSimilarity; |
| 19 | +import de.brightbyte.data.measure.Similarity; |
18 | 20 | import de.brightbyte.db.DatabaseUtil; |
19 | 21 | import de.brightbyte.io.LeveledOutput; |
20 | 22 | import de.brightbyte.rdf.RdfException; |
— | — | @@ -25,6 +27,7 @@ |
26 | 28 | import de.brightbyte.wikiword.disambig.StoredFeatureFetcher; |
27 | 29 | import de.brightbyte.wikiword.disambig.StoredMeaningFetcher; |
28 | 30 | import de.brightbyte.wikiword.disambig.Term; |
| 31 | +import de.brightbyte.wikiword.disambig.CoherenceDisambiguator.CoherenceDisambiguation; |
29 | 32 | import de.brightbyte.wikiword.model.AbstractConceptOutput; |
30 | 33 | import de.brightbyte.wikiword.model.ConceptFeatures; |
31 | 34 | import de.brightbyte.wikiword.model.ConceptOutput; |
— | — | @@ -47,6 +50,7 @@ |
48 | 51 | public class QueryConsole extends ConsoleApp<WikiWordConceptStore> { |
49 | 52 | |
50 | 53 | protected Disambiguator disambiguator; |
| 54 | + protected Similarity<LabeledVector<Integer>> proximityMeasure; |
51 | 55 | protected ConceptQuerySpec minimalConceptSpec; |
52 | 56 | protected ConceptQuerySpec resolvedConceptSpec; |
53 | 57 | protected ConceptQuerySpec detailedConceptSpec; |
— | — | @@ -63,6 +67,8 @@ |
64 | 68 | detailedConceptSpec.setIncludeDefinition(true); |
65 | 69 | detailedConceptSpec.setIncludeResources(true); |
66 | 70 | detailedConceptSpec.setIncludeRelations(true); |
| 71 | + |
| 72 | + proximityMeasure = ScalarVectorSimilarity.<Integer>getInstance(); |
67 | 73 | } |
68 | 74 | |
69 | 75 | protected static class ConsoleOutput { |
— | — | @@ -375,11 +381,11 @@ |
376 | 382 | if (isGlobalThesaurus()) { |
377 | 383 | String lang = params.get(1).toString(); |
378 | 384 | String term = params.get(2).toString(); |
379 | | - listMeaningsGlobal(lang, term, out); |
| 385 | + listMeaningsGlobal(lang, term, out, minimalConceptSpec); |
380 | 386 | } |
381 | 387 | else { |
382 | 388 | String term = params.get(1).toString(); |
383 | | - listMeaningsLocal(term, out); |
| 389 | + listMeaningsLocal(term, out, minimalConceptSpec); |
384 | 390 | } |
385 | 391 | } |
386 | 392 | else if (cmd.equals("s") || cmd.equals("cat") || cmd.equals("show")) { |
— | — | @@ -408,12 +414,22 @@ |
409 | 415 | int id = DatabaseUtil.asInt(params.get(1)); |
410 | 416 | showFeatureVector(id, out); |
411 | 417 | } |
| 418 | + else if (cmd.equals("r") || cmd.equals("rel") || cmd.equals("relatedness") || cmd.equals("proximity")) { |
| 419 | + int id1 = DatabaseUtil.asInt(params.get(1)); |
| 420 | + int id2 = DatabaseUtil.asInt(params.get(2)); |
| 421 | + showProximity(id1, id2, out); |
| 422 | + } |
| 423 | + else if (cmd.equals("nr") || cmd.equals("nrel") || cmd.equals("naiverelatedness") || cmd.equals("naiveproximity")) { |
| 424 | + String n1 = params.get(1).toString(); |
| 425 | + String n2 = params.get(2).toString(); |
| 426 | + showProximity(n1, n2, out); |
| 427 | + } |
412 | 428 | else if (cmd.equals("d") || cmd.equals("dis") || cmd.equals("disambig") || cmd.equals("disambiguate")) { |
413 | 429 | PhraseNode<? extends TermReference> root = getPhrases(params.get(1).toString()); |
414 | 430 | showDisambiguation(root, out); |
415 | 431 | } |
416 | 432 | else if (cmd.equals("ls") || cmd.equals("list")) { |
417 | | - listConcepts(out); |
| 433 | + listConcepts(out, minimalConceptSpec); |
418 | 434 | } |
419 | 435 | } |
420 | 436 | |
— | — | @@ -474,18 +490,18 @@ |
475 | 491 | } |
476 | 492 | } |
477 | 493 | |
478 | | - public void listConcepts(ConsoleOutput out) throws PersistenceException { |
479 | | - DataSet<? extends LocalConcept> meanings = getLocalConceptStore().getAllConcepts(minimalConceptSpec); |
| 494 | + public void listConcepts(ConsoleOutput out, ConceptQuerySpec spec) throws PersistenceException { |
| 495 | + DataSet<? extends LocalConcept> meanings = getLocalConceptStore().getAllConcepts(spec); |
480 | 496 | out.writeConcepts(meanings); |
481 | 497 | } |
482 | 498 | |
483 | | - public void listMeaningsLocal(String term, ConsoleOutput out) throws PersistenceException { |
484 | | - DataSet<? extends WikiWordConcept> meanings = getLocalConceptStore().getMeanings(term, resolvedConceptSpec); |
| 499 | + public void listMeaningsLocal(String term, ConsoleOutput out, ConceptQuerySpec spec) throws PersistenceException { |
| 500 | + DataSet<? extends WikiWordConcept> meanings = getLocalConceptStore().getMeanings(term, spec); |
485 | 501 | out.writeConcepts(meanings); |
486 | 502 | } |
487 | 503 | |
488 | | - public void listMeaningsGlobal(String lang, String term, ConsoleOutput out) throws PersistenceException { |
489 | | - DataSet<GlobalConcept> meanings = getGlobalConceptStore().getMeanings(lang, term, resolvedConceptSpec); |
| 504 | + public void listMeaningsGlobal(String lang, String term, ConsoleOutput out, ConceptQuerySpec spec) throws PersistenceException { |
| 505 | + DataSet<GlobalConcept> meanings = getGlobalConceptStore().getMeanings(lang, term, spec); |
490 | 506 | out.writeConcepts(meanings); |
491 | 507 | } |
492 | 508 | |
— | — | @@ -535,6 +551,35 @@ |
536 | 552 | out.writeFeatureVector(conceptFeatures.getFeatureVector()); |
537 | 553 | } |
538 | 554 | |
| 555 | + public void showProximity(String term1, String term2, ConsoleOutput out) throws PersistenceException { |
| 556 | + List<Term> terms = new ArrayList<Term>(2); |
| 557 | + |
| 558 | + Term t1 = new Term(term1); |
| 559 | + Term t2 = new Term(term2); |
| 560 | + terms.add(t1); |
| 561 | + terms.add(t2); |
| 562 | + |
| 563 | + CoherenceDisambiguation<Term, WikiWordConcept> r = |
| 564 | + (CoherenceDisambiguation<Term, WikiWordConcept>) getDisambiguator().disambiguate(terms, null); |
| 565 | + |
| 566 | + WikiWordConcept concept1 = (WikiWordConcept)r.getMeanings().get(t1); |
| 567 | + WikiWordConcept concept2 = (WikiWordConcept)r.getMeanings().get(t2); |
| 568 | + |
| 569 | + ConceptFeatures<WikiWordConcept, Integer> f1 = r.getFeatures().get(concept1.getId()); |
| 570 | + ConceptFeatures<WikiWordConcept, Integer> f2 = r.getFeatures().get(concept2.getId()); |
| 571 | + |
| 572 | + double d = proximityMeasure.similarity(f1.getFeatureVector(), f2.getFeatureVector()); |
| 573 | + out.write("proximity of " + concept1+" and " + concept2 + ": " + d); |
| 574 | + } |
| 575 | + |
| 576 | + public void showProximity(int id1, int id2, ConsoleOutput out) throws PersistenceException { |
| 577 | + ConceptFeatures f1 = getProximityStore().getConceptFeatures(id1); |
| 578 | + ConceptFeatures f2 = getProximityStore().getConceptFeatures(id2); |
| 579 | + |
| 580 | + double d = proximityMeasure.similarity(f1.getFeatureVector(), f2.getFeatureVector()); |
| 581 | + out.write(d); |
| 582 | + } |
| 583 | + |
539 | 584 | public void showDisambiguation(PhraseNode<? extends TermReference> root, ConsoleOutput out) throws PersistenceException { |
540 | 585 | Disambiguator.Disambiguation r = getDisambiguator().disambiguate(root, null); |
541 | 586 | out.writeInterpretation(r.getMeanings()); |