2016-02-15 8 views
6

क्या कोई जावास्क्रिप्ट कोड विश्लेषक है जिसे कोड पैटर्न का विश्लेषण करने के लिए क्लाइंट साइड पर उपयोग किया जा सकता है? मुझे निम्न मिला है लेकिन ऐसा लगता है कि यह केवल नियमित पाठ के लिए है और आपको = साइन इत्यादि देता है। मुझे कुछ कोड विश्लेषण की आवश्यकता है जो क्लाइंट साइड (जेएस कोड) पर चल सकती है, क्या कोई भी इसका उपयोग किया जा सकता है?क्लाइंट साइड जावास्क्रिप्ट कोड विश्लेषक

function parseData() { 
 
    var rawData = document.getElementById('data').value.trim(), 
 
     result, 
 
     output = $('#output'), 
 
     table = $('table').remove(), 
 
     header, 
 
     row, 
 
     cell, 
 
     ul, 
 
     slice, 
 
     wpm = [],   
 
     wpmAvg = []; 
 

 
    output.empty(); 
 
    table.find('thead, tbody').empty(); 
 

 
    if ($('[name="format"]:checked').val() === 'text') { 
 
    // Simple text   
 
    result = analyzeText(rawData); 
 
    output.append('Word count: ' + result.count + '<br><br>Frequent words:<br>'); 
 
    ul = $('<ul>'); 
 
    _.forEach(result.frequentWords, function(value, key) { 
 
     ul.append('<li>' + value.word + ': ' + value.count + '</li>'); 
 
    }); 
 
    output.append(ul);  
 
    } 
 
    else { 
 
    // JSON 
 
    try { 
 
     data = JSON.parse(rawData); 
 
    } 
 
    catch(e) { 
 
     console.log('Error parsing JSON', e); 
 
    } 
 
    header = table.find('thead'); 
 
    body = table.find('tbody'); 
 
    row = $('<tr>'); 
 
    body.append(row); 
 
    // Loop over slices 
 
    _.forEach(data, function(value, key) { 
 
     slice = ''; 
 
     // Loop over statements 
 
     _.forEach(value, function(value, key) { 
 
     slice += value.words + ' '; 
 
     }); 
 

 
     result = analyzeText(slice); 
 

 
     addCell(slice, key); 
 

 
    }); 
 
    $.plot('#wpm', [wpm], { 
 
     xaxes: [{ 
 
     axisLabel: 'Time index (1-minute increments)', 
 
     }], 
 
     yaxes: [{ 
 
     position: 'left', 
 
     axisLabel: 'Words per minute', 
 
     }] 
 
    }); 
 
    output.append(table); 
 
    } 
 

 
    function addCell(data, index) { 
 
    var cell1, cell2, ul1, ul2, result; 
 
    cell1 = $('<td>'); 
 
    cell2 = $('<td>'); 
 
    ul1 = $('<ul>'); 
 
    ul2 = $('<ul>'); 
 
    cell1.append(ul1); 
 
    cell2.append(ul2); 
 
    row.append(cell1, cell2); 
 
    result = analyzeText(data); 
 
    header.append('<th>' + index + '</th><th class="subText">(' + result.count + ')</th>'); 
 
    wpm.push([index, result.count]); 
 
    _.forEach(result.frequentWords, function(value, key) { 
 
     ul1.append('<li>' + value.word + '</li>'); 
 
     ul2.append('<li>' + value.count + '</li>'); 
 
    }); 
 
    } 
 
} 
 

 
function analyzeText(rawData) { 
 
    var result = { 
 
    count: 0, 
 
    frequentWords: [] 
 
    }, 
 
     data = rawData.split(/[\s.,]+/g) 
 
    counts = {}, 
 
    countsArray = [], 
 
    commonWords = [ 
 
    0,1,2,3,4,5,6,7,8,9,    
 
    '-', 
 
    'a', 
 
    'about', 
 
    'function', 
 
    'object' 
 
    ]; 
 

 
    if (!data[data.length]) { 
 
    data.splice(-1, 1); 
 
    } 
 

 
    // Word count 
 
    result.count = data.length; 
 

 
    // Word frequency (filtered for common words, sorted descending by count) 
 
    for (var i = 0; i < data.length; i++) { 
 
    if (!counts.hasOwnProperty(data[i].toLowerCase())) { 
 
     counts[data[i].toLowerCase()] = 1; 
 
    } 
 
    else { 
 
     counts[data[i].toLowerCase()] += 1; 
 
    } 
 
    }  
 
    _.forEach(counts, function(value, key) { 
 
    if (commonWords.indexOf(key.toLowerCase()) === -1) { 
 
     countsArray.push({ 
 
     word: key.toLowerCase(), 
 
     count: value 
 
     }); 
 
    } 
 
    }); 
 
    countsArray = _.sortBy(countsArray, 'count').reverse();  
 
    _.forEach(countsArray, function(value, index) { 
 
    if (value.count > 1) { 
 
     result.frequentWords.push(value);    
 
    } 
 
    }); 
 

 
    return result; 
 
}
body { 
 
    font-family: arial; 
 
} 
 
table, tr, td, th { 
 
    border-collapse: collapse; 
 
    border: solid 1px #ddd; 
 
} 
 
th, td { 
 
    padding: 4px 8px;  
 
} 
 
.subText { 
 
    color:#999; 
 
    font-style: italic; 
 
} 
 
#wpm { 
 
    width:600px; 
 
    height: 400px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.js"></script> 
 

 
<textarea id="data" cols="80" rows="20" placeholder="Paste text or JSON here"></textarea><br /> 
 
<label for="text"><input type="radio" name="format" checked value="text" id="text"> Simple text</label> 
 
<button type="button" onclick="parseData()">Analyze text</button> 
 
<br><br> 
 
<div id="output"></div><br><br> 
 
<div id="wpm"></div> 
 
<table> 
 
    <thead> 
 
    </thead> 
 
    <tbody> 
 
    </tbody> 
 
</table>

https://jsfiddle.net/fxn5q8y0/6/

+0

आप नियमित अभिव्यक्ति के बारे में बात कर रहे हैं? – Jai

+0

@ जय - शायद हुड के तहत मैं कुछ कोड प्रदान करना चाहता हूं जिसका विश्लेषण कुछ कीवर्ड संरचना या शायद कुछ पैटर्न आदि –

+0

के संबंध में किया जाएगा कृपया अधिक विशिष्ट हो .. आपको क्या चाहिए ** बिल्कुल ** 'टेक्स्ट विश्लेषण 'पर्याप्त नहीं है । –

उत्तर

0

बस एक दायरे में प्रत्येक तत्व के माध्यम से लूप और उसके प्रकार का पता लगाने।

यहाँ एक त्वरित उदाहरण:

var analyse = function(obj){ 
 
    var results = {} 
 
    
 
    for(var b in obj) { // for each element 
 
    if(obj.hasOwnProperty(b)){ 
 
     var detectedType = (typeof obj[b]) // detect the type 
 
     if (!(detectedType in results)){ results[detectedType] = 0; } // if the type was detected for the first time, add its name to the results 
 
     
 
     results[detectedType]++; // increase the counter for the detected type 
 
    } 
 
    } 
 
    
 
    return results; 
 
} 
 

 

 

 
/** 
 
    example for a testObject 
 
**/ 
 

 
testObject = { 
 
    a: undefined, // type undefined 
 
    b: { xx: "xx", yy: 123 }, // type object 
 
    c: false, // type boolean 
 
    d: 987, // type number 
 
    e: "abc", // type string 
 
    f: Symbol("foo"), // type symbol 
 
    g: function(){}, // type function 
 
    h: "jkl", // type number 
 
    i: 654 // type number 
 
}; 
 

 
console.log(analyse(testObject));

+0

हालांकि यह कोड स्निपेट प्रश्न हल कर सकता है, [एक स्पष्टीकरण सहित] (http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) वास्तव में आपकी पोस्ट की गुणवत्ता में सुधार करने में मदद करता है। याद रखें कि आप भविष्य में पाठकों के लिए प्रश्न का उत्तर दे रहे हैं, और वे लोग आपके कोड सुझाव के कारणों को नहीं जानते हैं। स्पष्टीकरण टिप्पणियों के साथ अपने कोड को भीड़ न करने का प्रयास करें, इससे कोड और स्पष्टीकरण दोनों की पठनीयता कम हो जाती है! – Rizier123

संबंधित मुद्दे