OpenLayers OpenLayers

Changeset 7097

Show
Ignore:
Timestamp:
05/08/08 04:27:54 (2 months ago)
Author:
tschaub
Message:

create word index from examples and use it to filter example list

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/doc/examples.html

    r7092 r7097  
    22<html> 
    33    <head> 
    4         <title>OL Docs</title> 
     4        <title>OpenLayers Examples</title>         
     5        <link rel="stylesheet" href="../examples/style.css" type="text/css" /> 
    56        <style type="text/css"> 
    67            html, body { 
     
    910                margin: 0; 
    1011                padding: 0; 
    11                 font: 0.9em Verdana, Arial, sans serif
     12                line-height: 1.25em
    1213            } 
    1314            .ex_container{ 
     
    1516            } 
    1617            .ex_container a { 
     18                text-decoration: none; 
    1719                padding: 5px 1em; 
    1820                display: block; 
     
    2527                font-weight: bold; 
    2628                color: #333; 
     29            } 
     30            .ex_filename { 
     31                font-weight: normal; 
     32                font-size: 0.8em; 
     33                color: #ccc 
    2734            } 
    2835            .ex_description{ 
     
    5764        <script type="text/javascript"> 
    5865            // import 
    59             var uri = "http://jugl.tschaub.net/trunk/lib/Jugl.js"; 
    60             var Jugl = window[uri]; 
    61             // this part does the actual template processing 
     66            var Jugl = window["http://jugl.tschaub.net/trunk/lib/Jugl.js"]; 
     67            var template, target; 
     68 
     69            function listExamples(examples) { 
     70                var node = template.process({examples: examples}, true); 
     71                target.innerHTML = ""; 
     72                target.appendChild(node); 
     73                document.getElementById("count").innerHTML = "(" + examples.length + ")"; 
     74            } 
     75             
     76            var timerId; 
     77            function inputChange() { 
     78                if(timerId) { 
     79                    window.clearTimeout(timerId); 
     80                } 
     81                var text = this.value; 
     82                timerId = window.setTimeout(function() { 
     83                    filterList(text); 
     84                }, 500); 
     85            } 
     86             
     87            function filterList(text) { 
     88                var examples; 
     89                if(text.length < 2) { 
     90                    examples = info.examples; 
     91                } else { 
     92                    var words = text.split(/\W+/); 
     93                    var scores = {}; 
     94                    for(var i=0; i<words.length; ++i) { 
     95                        var word = words[i].toLowerCase() 
     96                        var dict = info.index[word]; 
     97                        if(dict) { 
     98                            for(exIndex in dict) { 
     99                                var count = dict[exIndex]; 
     100                                if(scores[exIndex]) { 
     101                                    scores[exIndex] += count; 
     102                                } else { 
     103                                    scores[exIndex] = count; 
     104                                } 
     105                            } 
     106                        } 
     107                    } 
     108                    examples = []; 
     109                    for(var j in scores) { 
     110                        var ex = info.examples[j]; 
     111                        ex.score = scores[j]; 
     112                        examples.push(ex); 
     113                    } 
     114                    // sort examples by descending score 
     115                    examples.sort(function(a, b) { 
     116                        return (b.score - a.score); 
     117                    }); 
     118                } 
     119                listExamples(examples); 
     120            } 
     121             
     122            function showAll() { 
     123                document.getElementById("keywords").value = ""; 
     124                listExamples(info.examples); 
     125            } 
    62126            window.onload = function() { 
    63                 var template = new Jugl.Template("template"); 
    64                 var node = template.process(null, true); 
    65                 document.getElementById("examples").appendChild(node); 
     127                template = new Jugl.Template("template"); 
     128                target = document.getElementById("examples"); 
     129                listExamples(info.examples); 
    66130                document.getElementById("exwin").src = "../examples/example.html"; 
    67             } 
     131                document.getElementById("keywords").onkeyup = inputChange 
     132            }; 
    68133        </script> 
    69134    </head> 
    70135    <body> 
    71136        <div id="toc"> 
    72             <p id="instructions">Click an example link below to view on the right.</p> 
     137            <p id="instructions"> 
     138                <label for="keywords">Filter by keywords</label> 
     139                <input type="text" id="keywords" /><br /> 
     140                <span id="count"></span> 
     141                <a href="javascript:void showAll();">show all</a> 
     142            </p> 
    73143            <div id="examples"></div> 
    74144        </div> 
    75         <iframe id="exwin" name="exwin"></iframe> 
    76          
     145        <iframe id="exwin" name="exwin"></iframe>         
    77146        <div style="display: none;"> 
    78147            <div id="template"> 
    79148                <div class="ex_container" jugl:repeat="example examples"> 
    80149                    <a jugl:attributes="href example.link" target="exwin"> 
    81                         <h3 class="ex_title"> 
     150                        <h5 class="ex_title"> 
    82151                            <span jugl:replace="example.title">title</span><br /> 
    83                             <span class="exampleName" jugl:content="'(' + example.example + ')'">filename</span> 
    84                         </h3
     152                            <span class="ex_filename" jugl:content="'(' + example.example + ')'">filename</span> 
     153                        </h5
    85154                        <div class="ex_description" jugl:content="example.shortdesc"> 
    86155                            Short Description goes here 
  • trunk/openlayers/tools/exampleparser.py

    r5362 r7097  
    8282    return d 
    8383     
    84  
     84def wordIndex(examples): 
     85    """ 
     86    Create an inverted index based on words in title and shortdesc.  Keys are 
     87    lower cased words.  Values are dictionaries with example index keys and 
     88    count values. 
     89    """ 
     90    index = {} 
     91    unword = re.compile("\\W+") 
     92    keys = ["shortdesc", "title"] 
     93    for i in range(len(examples)): 
     94        for key in keys: 
     95            text = examples[i][key] 
     96            if text: 
     97                words = unword.split(text) 
     98                for word in words: 
     99                    if word: 
     100                        word = word.lower() 
     101                        if index.has_key(word): 
     102                            if index[word].has_key(i): 
     103                                index[word][i] += 1 
     104                            else: 
     105                                index[word][i] = 1 
     106                        else: 
     107                            index[word] = {i: 1} 
     108    return index 
     109     
    85110if __name__ == "__main__": 
    86111 
     
    115140         
    116141    exampleList.sort(key=lambda x:x['example'].lower()) 
    117     json = simplejson.dumps(exampleList) 
     142     
     143    index = wordIndex(exampleList) 
     144 
     145    json = simplejson.dumps({"examples": exampleList, "index": index}) 
    118146    #give the json a global variable we can use in our js.  This should be replaced or made optional. 
    119     json = 'var examples=' + json  
     147    json = 'var info=' + json  
    120148    outFile.write(json) 
    121149    outFile.close()