This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKeyFinder.java
152 lines (138 loc) · 3.55 KB
/
KeyFinder.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.io.*;
import java.util.ArrayList;
public class KeyFinder {
private File nounFile;
private File verbFile;
private File conjFile;
private File prepFile;
private File adjFile;
private File advFile;
private ArrayList<String> nouns;
private ArrayList<String> verbs;
private ArrayList<String> conjs;
private ArrayList<String> advs;
private ArrayList<String> adjs;
private ArrayList<String> preps;
public KeyFinder() {
nounFile = new File( "src/nouns.txt" );
verbFile = new File( "src/verbs.txt" );
nouns = new ArrayList<String>();
verbs = new ArrayList<String>();
uploadText();
}
private void uploadText() {
String thisLine = "";
try
{
BufferedReader br = new BufferedReader( new FileReader( nounFile ) );
while( (thisLine = br.readLine()) != null ) {
nouns.add( thisLine );
}
br = new BufferedReader( new FileReader( verbFile ) );
while( (thisLine = br.readLine()) != null ) {
verbs.add( thisLine );
}
br.close();
}
catch( IOException ex ) {
ex.printStackTrace();
}
}
public ArrayList<String> findKeys2( String sentence ) {
//System.out.println( "Finding keys to sentence: [" + sentence + "]" );
String[] words = sentence.split(" ");
//find the subject, main verb and the object if any
ArrayList<String> nv = new ArrayList<String>();
for( int i = 0; i < words.length; i++ ) {
String wordCopy = words[i];
wordCopy = wordCopy.replaceAll( "[^A-Za-z]", "" );
boolean noun = isNoun( wordCopy, i == 0 );
boolean verb = isVerb( wordCopy );
if( noun && verb ) {
nv.add( wordCopy );
}
else {
if( noun || verb ) {
nv.add( wordCopy );
}
else {
continue;
}
}
}
return nv;
}
private boolean isNoun( String test, boolean isFirstWord ) {
for( String noun: nouns ) {
if( test.toLowerCase().equals(noun) ) {
return true;
}
}
if( Character.isUpperCase( test.charAt(0) ) && !isFirstWord ) {
return true;
}
return false;
}
private boolean isPrep( String test ) {
for( String prep: preps ) {
if( test.toLowerCase().equals(prep) ) {
return true;
}
}
return false;
}
private boolean isVerb( String test ) {
for( String verb: verbs ) {
if( test.toLowerCase().equals(verb) ) {
return true;
}
}
return false;
}
private boolean isConj( String test ) {
for( String conj: conjs ) {
if( test.toLowerCase().equals(conj) ) {
return true;
}
}
return false;
}
private boolean isAdv( String test ) {
for( String adv: advs ) {
if( test.toLowerCase().equals(adv) ) {
return true;
}
}
return false;
}
private boolean isAdj( String test ) {
for( String adj: adjs ) {
if( test.toLowerCase().equals(adj) ) {
return true;
}
}
return false;
}
public ArrayList<String> findKeys( String sentence ) {
//System.out.println( "Finding keys to sentence: [" + sentence + "]" );
String[] words = sentence.split(" ");
ArrayList<String> nounList = new ArrayList<String>();
for( int i = 0; i < words.length; i++ ) {
String wordCopy = words[i];
wordCopy = wordCopy.replaceAll( "[^A-Za-z]", "" );
for( String noun: nouns ) {
if( wordCopy.equals(noun) ) {
String word = words[i].replaceAll("[^A-Za-z']", "").toLowerCase();
nounList.add( word );
nounList.add(Character.toUpperCase(word.charAt(0)) + word.substring(1));
}
}
}
return nounList;
}
public static void main( String[] args ) {
KeyFinder kf = new KeyFinder();
ArrayList<String> keys = kf.findKeys2( "That boy ate my sandwich; he is so mean." );
System.out.println( keys );
}
}