nord vpnnord vpn
Ad

Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
3.2 kB
0
Indexable
Never
package com.hamedshahidi.wordfrequencyapp.controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import com.hamedshahidi.wordfrequencyapp.models.WordFrequencyRequest;
import com.hamedshahidi.wordfrequencyapp.models.WordFrequencyResponse;

import jakarta.servlet.http.Part;

@RestController
public class WordFrequencyController {

    @PostMapping(value = "/word-frequency", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<WordFrequencyResponse> processWordFrequency(
            @RequestPart("request") WordFrequencyRequest request) throws IOException {

        try {
            Part filePart = request.getFilePart();
            InputStream fileInputStream = filePart.getInputStream();

            Map<String, Integer> wordFrequencyMap = new HashMap<>();
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                // Process line by line here maybe?

                String[] words = line.split("\\s+");
                for (String word : words) {
                    wordFrequencyMap.put(word, wordFrequencyMap.getOrDefault(word, 0) + 1);
                }
            }
            WordFrequencyResponse response = new WordFrequencyResponse(wordFrequencyMap);
            return ResponseEntity.ok(response);

        } catch (IOException e) {

        }

        return ResponseEntity.badRequest().build();

        // try (BufferedReader reader = new BufferedReader(new
        // InputStreamReader(req.getInputStream()))) {
        // Map<String, Integer> wordFrequencyMap = new HashMap<>();

        // String line;
        // while ((line = reader.readLine()) != null) {
        // List<String> words = Arrays.asList(line.toLowerCase().replaceAll("[^a-z0-9
        // ]", "").split("\\s+"));
        // words.forEach(word -> wordFrequencyMap.put(word,
        // wordFrequencyMap.getOrDefault(word, 0) + 1));
        // }

        // List<Map.Entry<String, Integer>> sortedEntries = new
        // ArrayList<>(wordFrequencyMap.entrySet());
        // sortedEntries.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));

        // List<String> topWords = sortedEntries.stream()
        // .map(Map.Entry::getKey)
        // .collect(Collectors.toList());

        // WordFrequencyResponse response = new WordFrequencyResponse();
        // response.setTopWords(topWords);

        // return ResponseEntity.ok(response);
    }
}

nord vpnnord vpn
Ad