web/ldt/ldt_utils/projectindexer.py
author wakimd
Fri, 15 Oct 2010 12:38:52 +0200
changeset 95 9bae869b2146
parent 94 9927a619d2b5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import tempfile
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import os
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import os.path
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import shutil
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import ldt.utils.xml
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from ldt import settings
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import lucene
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from ldt.ldt_utils import STORE
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from ldt.ldt_utils import ANALYZER
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    10
import lxml.etree
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    11
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
def Property(func):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    return property(**func()) 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class ProjectIndexer(object):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    def __init__(self, projectList, writer, decoupage_blackList = settings.DECOUPAGE_BLACKLIST):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        self.__projectList = projectList
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        self.__decoupage_blacklist = decoupage_blackList
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        self.__writer = writer
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    @Property
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    def decoupage_blacklist(): #@NoSelf
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        doc = """get blacklist""" #@UnusedVariable
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
       
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        def fget(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
            if self.__decoupage_blacklist is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
                self.__decoupage_blacklist = ()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
            return self.__decoupage_blacklist
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        def fset(self, value):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            self.__decoupage_blacklist = value
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        def fdel(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            del self.__decoupage_blacklist
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
           
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        return locals()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
               
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    def index_all(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        for project in self.__projectList:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            self.index_project(project)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    def index_project(self, project):
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    43
        # ldt.utils.log.debug("Indexing project : "+str(project.ldt_id))
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    44
        
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    45
        ldt=project.ldt
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    46
        doc = lxml.etree.fromstring(ldt.encode( "utf-8" ))
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    47
 
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        self.__writer.deleteDocuments(lucene.Term("ldt_id", project.ldt_id))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    50
        res = doc.xpath("/iri/annotations/content")
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    51
        project.ldt.encode( "utf-8 " )
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        for content in res:
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    54
            contentId = content.get("id")
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    55
 
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    56
            res =content.xpath("ensemble")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            for ensemble in res:
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    58
                ensembleId = ensemble.get("id")
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    59
 
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    60
                for decoupageNode in ensemble.getchildren():
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    61
                    # ldt.utils.log.debug("Indexing project decoupage : "+ repr(decoupageNode.nodeType) + " in " + repr(self.decoupage_blacklist))
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    62
                    if decoupageNode.tag != "decoupage"  or decoupageNode.get("id") in self.decoupage_blacklist:
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
                        continue
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
                
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    65
                    decoupId = decoupageNode.get("id")
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    66
                    res = decoupageNode.xpath("elements/element")
91
9c83809fda01 migration static elements
wakimd
parents: 62
diff changeset
    67
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
                    for elementNode in res:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                        doc = lucene.Document()
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    70
                        elementId = elementNode.get("id")
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    71
                        tags = elementNode.get("tags")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
                        if tags is not None:                            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
                            tags.replace(",", ";")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
                        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
                        if tags is None or len(tags) == 0:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                            tags = ""
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    78
                            restagnode = elementNode.xpath("tag/text()")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
                            for tagnode in restagnode:
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    80
                                tags = tags + " ; " + tagnode.text()
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                        if tags is None or len(tags) == 0:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                            tags = ""
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    84
                            restagnode = elementNode.xpath("tags/tag/text()")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                            for tagnode in restagnode:
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    86
                                tags = tags + " ; " + tagnode.text()                          
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
                        title = ""
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    89
                        for txtRes in elementNode.xpath("title/text()"): 
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    90
                            title = title + txtRes.text()
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                        abstract = ""
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    93
                        for txtRes in elementNode.xpath("abstract/text()"): 
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    94
                            abstract = abstract + txtRes.text() 
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                            
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    96
                        doc.add(lucene.Field("ldt_id", project.ldt_id, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))              
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
    97
                        doc.add(lucene.Field("iri_id", contentId, lucene.Field.Store.YES, lucene.Field.Index.NOT_ANALYZED))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                        doc.add(lucene.Field("ensemble_id", ensembleId, lucene.Field.Store.YES, lucene.Field.Index.NO))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                        doc.add(lucene.Field("decoupage_id", decoupId, lucene.Field.Store.YES, lucene.Field.Index.NO))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                        doc.add(lucene.Field("element_id", elementId, lucene.Field.Store.YES, lucene.Field.Index.NO))
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
   101
                        doc.add(lucene.Field("tags", tags, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
   102
                        doc.add(lucene.Field("title", title, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
   103
                        doc.add(lucene.Field("abstract", abstract, lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
   104
                        doc.add(lucene.Field("all", " ".join([tags, title, abstract]), lucene.Field.Store.NO, lucene.Field.Index.ANALYZED))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                        self.__writer.addDocument(doc)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 91
diff changeset
   108
        self.__writer.commit()