-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentence.java
More file actions
119 lines (88 loc) · 2.86 KB
/
Sentence.java
File metadata and controls
119 lines (88 loc) · 2.86 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
/*
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.IOException;
import java.util.ArrayList;
//This class takes the line object and splits it into sentences and counting them
public class Sentence
{
public ArrayList<String> sentence;
private String line;
private int last;
public String part1;
public String part2;
private static String joinHolder = ""; //used to store end of line but not end of sentence
private static float noOfSentences;
public Sentence(String line) throws IOException
{
this.line = line;
if (line.endsWith("."))
{
//Line is split into sentences stored in sSentence
String[] sSentence = line.split("(?<=[a-z])\\.\\s+");
for (String sentence : sSentence)
{
//if first letter of sentence is lower case then it is continued from previous line
if(Character.isLowerCase(sentence.codePointAt(0)))
{
//Join unfinished sentence from previous line with first sentence in new line
sentence = String.join(" ",joinHolder, sentence);
}
//System.out.println(sentence);
sentenceCount();
//creates a word object using the sentence which splits the sentence into words and process them
Word word = new Word(sentence);
}
}
else //if line does not end with full stop and sentence continues onto next line
{
//Line is split into sentences stored in sSentence
String[] sSentence = line.split("(?<=[a-z])\\.\\s+");
/*
* If line has more than one sentence in it
*/
if(sSentence.length > 1)
{
//last part of the line that is split is stored in case sentence runs onto next line
last = sSentence.length - 1;
joinHolder = sSentence[last];
sSentence[last] = " ";
}
else //if line consists of just one sentence
{
//The whole line is stored in case sentence runs onto next line
last = 0;
joinHolder = sSentence[last];
}
//counts sentences
for (String sentence : sSentence)
{
if (sentence != " ") //if line is not blank
{
//System.out.println(sentence);
sentenceCount();
}
//Creates word object to split sentence into words and process words
Word word = new Word(sentence);
}
}
}
public void sentenceCount()
{
noOfSentences++;
}
//Getters and Setters
public static float getNoOfSentences() {
return noOfSentences;
}
public static void setNoOfSentences(float noOfSentences) {
Sentence.noOfSentences = noOfSentences;
}
}