Created
November 16, 2016 10:18
-
-
Save geakstr/c9d26c1d16f6307e43d341f5e80d9685 to your computer and use it in GitHub Desktop.
Task 27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.swing.JButton; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
public class Main extends JFrame { | |
public Main() { | |
// Build GUI | |
// Form title | |
super("Task 27 - Text processing"); | |
setBounds(100, 100, 640, 400); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// `Open file` button | |
JButton openFileButton = new JButton("Process file"); | |
add(openFileButton, BorderLayout.NORTH); | |
JTextArea resultTextArea = new JTextArea(); | |
// Don't allow text editing | |
resultTextArea.setEditable(false); | |
// Set height | |
resultTextArea.setRows(20); | |
// Allow scroll | |
JScrollPane scroll = new JScrollPane (resultTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); | |
add(scroll, BorderLayout.SOUTH); | |
// Handle click on button | |
openFileButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
try { | |
// Show file chooser | |
JFileChooser fileopen = new JFileChooser(); | |
if (fileopen.showDialog(null, "Open file") == JFileChooser.APPROVE_OPTION) { | |
File file = fileopen.getSelectedFile(); | |
// Open file for read | |
BufferedReader in = new BufferedReader(new FileReader(file.getAbsolutePath())); | |
// Final max word for output | |
String maxWord = null; | |
// Final min word for output | |
String minWord = null; | |
// Map for words counting | |
Map<String, Integer> words = new HashMap<String, Integer>(); | |
// Map for letters counting | |
Map<Character, Integer> letters = new HashMap<Character, Integer>(); | |
// Map for digits counting | |
Map<Character, Integer> digits = new HashMap<Character, Integer>(); | |
// Map for punctuation counting | |
Map<Character, Integer> punctuation = new HashMap<Character, Integer>(); | |
// Read file while not end | |
while (in.ready()) { | |
// Read file line by line | |
String line = in.readLine(); | |
// Split line by space | |
String[] splitedLine = line.split(" "); | |
// Iterate over words in line | |
// `word` — current word in line | |
for (String word : splitedLine) { | |
// Trim spaces on start/end of current word | |
word = word.trim(); | |
if (word.length() > 0) { | |
// Compare with current max word | |
if (maxWord == null || word.length() > maxWord.length()) { | |
// New max word found | |
maxWord = word; | |
} | |
// Compare with current min word | |
if (minWord == null || word.length() < minWord.length()) { | |
// New min word found | |
minWord = word; | |
} | |
// Update word counter | |
if (words.containsKey(word)) { | |
// If we already have word in map — | |
// increment | |
// counter | |
words.put(word, words.get(word) + 1); | |
} else { | |
words.put(word, 1); | |
} | |
// Split current word to letters | |
// We delete all non letter chars and | |
// translate | |
// String | |
// to char[] array | |
char[] lettersInWord = word.replaceAll("[^a-zA-Zа-яА-ЯеЁ]", "").toCharArray(); | |
// Iterate over all letters in current word | |
for (char letter : lettersInWord) { | |
// Update letter counter | |
if (letters.containsKey(letter)) { | |
// If we already have letter in map | |
// — | |
// increment | |
// counter | |
letters.put(letter, letters.get(letter) + 1); | |
} else { | |
letters.put(letter, 1); | |
} | |
} | |
// Split current word to digits | |
// We delete all non digit chars and | |
// translate | |
// String to | |
// char[] array | |
char[] digitsInWord = word.replaceAll("[^0-9]", "").toCharArray(); | |
// Iterate over all digits in current word | |
for (char digit : digitsInWord) { | |
// Update digit counter | |
if (digits.containsKey(digit)) { | |
// If we already have digit in map — | |
// increment | |
// counter | |
digits.put(digit, digits.get(digit) + 1); | |
} else { | |
digits.put(digit, 1); | |
} | |
} | |
// Split current word to puntiation | |
// We delete all letter and digit chars and | |
// translate | |
// String to char[] array | |
char[] punctuationInWord = word.replaceAll("[a-zA-Zа-яА-ЯеЁ0-9]", "").toCharArray(); | |
// Iterate over all punctuation in current | |
// word | |
for (char punct : punctuationInWord) { | |
// Update punctuation counter | |
if (punctuation.containsKey(punct)) { | |
// If we already have punctuation in | |
// map | |
// — | |
// increment counter | |
punctuation.put(punct, punctuation.get(punct) + 1); | |
} else { | |
punctuation.put(punct, 1); | |
} | |
} | |
} | |
} | |
} | |
// Close opened file | |
in.close(); | |
String result = ""; | |
// Output max word | |
result += (maxWord == null || maxWord.length() == 0) ? "Max word not found" : "Max word: " + maxWord; | |
result += "\n"; | |
// Output min word | |
result += (minWord == null || minWord.length() == 0) ? "Min word not found" : "Min word: " + minWord; | |
result += "\n"; | |
// Output words counters | |
result += "\n"; | |
result += "--- Words counters ---"; | |
result += "\n"; | |
for (Map.Entry<String, Integer> e : words.entrySet()) { | |
result += e.getKey() + ": " + e.getValue(); | |
result += "\n"; | |
} | |
// Output letters counters | |
result += "\n"; | |
result += "--- Letters counters ---"; | |
result += "\n"; | |
for (Map.Entry<Character, Integer> e : letters.entrySet()) { | |
result += e.getKey() + ": " + e.getValue(); | |
result += "\n"; | |
} | |
// Output digits counter | |
result += "\n"; | |
result += "--- Digits counters ---"; | |
result += "\n"; | |
for (Map.Entry<Character, Integer> e : digits.entrySet()) { | |
result += e.getKey() + ": " + e.getValue(); | |
result += "\n"; | |
} | |
// Output punctuation counters | |
result += "\n"; | |
result += "--- Punctuation counters ---"; | |
result += "\n"; | |
for (Map.Entry<Character, Integer> e : punctuation.entrySet()) { | |
result += e.getKey() + ": " + e.getValue(); | |
result += "\n"; | |
} | |
// Show result on form | |
resultTextArea.setText(result); | |
resultTextArea.setCaretPosition(0); | |
} | |
} catch (IOException ex) { | |
System.err.println(ex.toString()); | |
} | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
// Create app window | |
Main app = new Main(); | |
app.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment