टेक्स्ट एनालिसिस एप्लिकेशन इम्प्लीमेंट करना
आप एक टेक्स्ट एनालिसिस एप्लिकेशन बना रहे हैं जिसे किसी डॉक्युमेंट में शब्दों की फ्रीक्वेंसी गिननी है. आपको शब्द लुकअप्स के लिए ऑप्टिमल टाइम कॉम्प्लेक्सिटी के साथ समाधान इम्प्लीमेंट करना है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में कोड ऑप्टिमाइज़ करना
अभ्यास निर्देश
- जब शब्द पहले से फ्रीक्वेंसी मैप में हो, तो उसका
currentCountप्राप्त करें. - इसे इन्क्रीमेंट करें और अपडेटेड वैल्यू के साथ फ्रीक्वेंसी मैप अपडेट करें.
- अगर शब्द अभी तक फ्रीक्वेंसी मैप में नहीं था, तो उसे जोड़ें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
public class Main {
public static void main(String[] args) {
TextAnalyzer analyzer = new TextAnalyzer();
List words = Arrays.asList(
"Java", "is", "a", "programming", "language",
"Java", "is", "widely", "used", "for", "building", "applications",
"Many", "programmers", "use", "Java", "for", "web", "development", "and", "Android", "apps"
);
Map wordFrequency = analyzer.buildWordFrequencyMap(words);
System.out.println("Word frequency analysis:");
for (Map.Entry entry : wordFrequency.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " occurrences");
}
}
}
class TextAnalyzer {
public Map buildWordFrequencyMap(List words) {
Map frequencyMap = new HashMap();
for (String word : words) {
if (word.isEmpty()) {
continue;
}
word = word.toLowerCase();
if (frequencyMap.containsKey(word)) {
// Retrieve the frequency of the word
int currentCount = ____.get(____);
// Increment the frequency of the word
frequencyMap.put(____, currentCount + 1);
} else {
// If the frequency map does not have the word, add it.
____.____(word, 1);
}
}
return frequencyMap;
}
}