mattman (owner)

Revisions

gist: 102479 Download_button fork
public
Public Clone URL: git://gist.github.com/102479.git
Embed All Files: show embed
TextAnalyser.java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Write a description of class TextAnalyser here.
*
* @author Matt Didcoe
* @version 0.1
*/
import java.lang.*;
import java.util.*;
 
public class TextAnalyser {
 
    // instance variables - replace the example below with your own
    private String text;
    private int totalAnalysed;
    private int textLength;
    private char[] charArray;
    private int count;
    private Map<Character, Integer> m = new HashMap<Character, Integer>();
    private HashMap map = new LinkedHashMap();
    private String workable;
 
    /**
* Constructor for objects of class TextAnalyser
*/
    public TextAnalyser(String txt) {
        // initialise instance variables
        text = txt.toLowerCase();
        textLength = text.length();
        analyse(txt);
    }
    
    public TextAnalyser(){
        totalAnalysed = 0;
    }
    
    public void analyse(String s) {
        if(text != null) {
            makeArray(text);
        } else {
            text = s.toLowerCase();
            textLength = text.length();
            makeArray(s);
        }
    }
    
    public int charsAnalysed() {
        return totalAnalysed;
    }
    
    public int frequency(char c) {
        int occurence=0;
        for (int i = 0; i < charArray.length; i++) {
        if (charArray[i] == Character.toLowerCase(c))
            occurence++;
        }
        return occurence;
    }
    
    public double percentage(char c) {
        if(totalAnalysed != 0)
            return 100*(new Double(frequency(c)) / new Double(totalAnalysed));
        else
            return 0.0;
    }
    
    public char mostFrequent() {
        if(totalAnalysed == 0){
           return '?';
        } else {
            count_letters();
            List keyList = Arrays.asList(map.keySet().toArray());
            return (keyList.get(keyList.size()-1)).toString().charAt(0);
        }
    }
 
    public void clear() {
        totalAnalysed = 0;
    }
    
    private void makeArray(String str){
        workable = str.replaceAll("[^a-zA-Z]","");
        char[] arr = workable.toLowerCase().toCharArray();
        totalAnalysed += arr.length;
        charArray = arr;
    }
    
    private void count_letters() {
        char[] ca = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int [] CounterArrays = new int [ca.length];
       
        // pass thru every character in the line
        for (int i = 0; i < charArray.length; i++) {
            // test for letter of the alphabet
            for(int j = 0; j < ca.length; j++) {
                // if match
                if(charArray[i] == ca[j]) {
                    CounterArrays[j]++;
                    break;
                }
            }
        }
        
        // print the result
        for(int z = 1; z < CounterArrays.length; z++) {
            if (CounterArrays[z] != 0) {
                m.put(ca[z], CounterArrays[z]);
            }
           
        }
        
        List mapKeys = new ArrayList(m.keySet());
        List mapValues = new ArrayList(m.values());
        TreeSet sortedSet = new TreeSet(mapValues);
        Object[] sortedArray = sortedSet.toArray();
        int size = sortedArray.length;
 
        for (int i=0; i<size; i++) {
            map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),sortedArray[i]);
        }
 
    }
    
    public void stupid() {
        System.out.println(workable);
    }
    
}