| 630 | print "pattern.vector.LSA" |
| 631 | |
| 632 | def test_lsa_concepts(self): |
| 633 | try: |
| 634 | import numpy |
| 635 | except ImportError: |
| 636 | return |
| 637 | # Assert LSA concept space. |
| 638 | model = vector.Model(( |
| 639 | vector.Document("cats purr"), |
| 640 | vector.Document("cats meow"), |
| 641 | vector.Document("dogs howl"), |
| 642 | vector.Document("dogs bark") |
| 643 | )) |
| 644 | model.reduce(2) |
| 645 | # Intuitively, we'd expect two concepts: |
| 646 | # 1) with cats + purr + meow grouped together, |
| 647 | # 2) with dogs + howl + bark grouped together. |
| 648 | i1, i2 = 0, 0 |
| 649 | for i, concept in enumerate(model.lsa.concepts): |
| 650 | self.assertTrue(isinstance(concept, dict)) |
| 651 | if concept["cats"] > 0.5: |
| 652 | self.assertTrue(concept["purr"] > 0.5) |
| 653 | self.assertTrue(concept["meow"] > 0.5) |
| 654 | self.assertTrue(concept["howl"] == 0.0) |
| 655 | self.assertTrue(concept["bark"] == 0.0) |
| 656 | i1 = i |
| 657 | if concept["dogs"] > 0.5: |
| 658 | self.assertTrue(concept["howl"] > 0.5) |
| 659 | self.assertTrue(concept["bark"] > 0.5) |
| 660 | self.assertTrue(concept["purr"] == 0.0) |
| 661 | self.assertTrue(concept["meow"] == 0.0) |
| 662 | i2 = i |
| 663 | # We'd expect the "cat" documents to score high on the "cat" concept vector. |
| 664 | # We'd expect the "dog" documents to score high on the "dog" concept vector. |
| 665 | v1 = model.lsa[model.documents[0].id] |
| 666 | v2 = model.lsa[model.documents[2].id] |
| 667 | self.assertTrue(v1[i1] > 0.7) |
| 668 | self.assertTrue(v1[i2] == 0.0) |
| 669 | self.assertTrue(v2[i1] == 0.0) |
| 670 | self.assertTrue(v2[i2] > 0.7) |
| 671 | # Assert LSA.transform() for unknown documents. |
| 672 | v = model.lsa.transform(vector.Document("cats dogs")) |
| 673 | self.assertAlmostEqual(v[0], 0.34, places=2) |
| 674 | self.assertAlmostEqual(v[1], 0.34, places=2) |
| 675 | print "pattern.vector.LSA.concepts" |
| 676 | print "pattern.vector.LSA.transform()" |
| 677 | |
| 678 | def test_model_reduce(self): |
| 679 | try: |