-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord.java
More file actions
157 lines (108 loc) · 3.66 KB
/
Word.java
File metadata and controls
157 lines (108 loc) · 3.66 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Java Assignment 2nd Year
Program description: This program receives text from a text file and determines whether the text uses formal
or informal language. It works by looking at the amount of formal and informal words used and also some
elements of grammar.
OS: Windows 10
Date: 16/04/2018
James Hughes
*/
package com.languageanalyser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
//This class splits the sentence object into words, counts them, number of letters, bad and formal words and calculates average word length
public class Word
{
public String sentence;
public String[] words;
public static float noOfLetters, avgWordLength, badWords, formalWords;
private Path badLangPath, formalLangPath; //Paths for text files that hold formal and bad language so that they can be edited
private Charset charset;
private List<String> badLangList, formalLangList;
public Word(String sentence) throws IOException
{
this.sentence = sentence;
//Splits the sentence at each space
words = sentence.split(" ");
for (String word : words)
{
//Removes any commas or fullstops attached to word
word = word.replace(",", "");
word = word.replace(".", "");
//Adds length of word to overall letterCount
noOfLetters += word.length();
badLanguage(word);//Checks word for bad language
formal(word);//Checks word for formal language
System.out.println(word);
}
//Calculates average word length
avgWordLength = noOfLetters/Line.getNoOfWords();
}
public void badLanguage(String word) throws IOException
{
//Checks if word is in badLanguge.txt file
badLangPath = new File("badLanguage.txt").toPath();
charset = Charset.defaultCharset();
//Creates a list to store bad words in
badLangList = Files.readAllLines(badLangPath, charset);
//Makes word lower case so it can be compared to words in text file (all lower case)
word = word.toLowerCase();
for(String badWord : badLangList)
{
if(word.equals(badWord))//Compares word to list of bad words
{
badWords++;
}
}
}
public void formal(String word) throws IOException
{
//Checks if word is in fromalLanguge.txt file
formalLangPath = new File("formalLanguage.txt").toPath();
charset = Charset.defaultCharset();
//Create list to store ofrmla words in
formalLangList = Files.readAllLines(formalLangPath, charset);
//Makes word lower case so it can be compared to words in text file (all lower case)
word = word.toLowerCase();
for(String formalWord : formalLangList)
{
if(word.equals(formalWord))//Compares word to list of formal words
{
formalWords++;
}
}
}
//Getters and Setters
public static float getNoOfLetters() {
return noOfLetters;
}
public static void setNoOfLetters(float noOfLetters) {
Word.noOfLetters = noOfLetters;
}
public static float getAvgWordLength() {
return avgWordLength;
}
public static void setAvgWordLength(float avgWordLength) {
Word.avgWordLength = avgWordLength;
}
public static float getBadWords() {
return badWords;
}
public static void setBadWords(float badWords) {
Word.badWords = badWords;
}
public static float getFormalWords() {
return formalWords;
}
public static void setFormalWords(float formalWords) {
Word.formalWords = formalWords;
}
}