-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordInSentance.java
73 lines (65 loc) · 1.8 KB
/
WordInSentance.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class WordInSentance
{
List<String> wordList = new ArrayList<>();
static Map<Integer, String> sentanceMap = new HashMap<>();
static List<String> ignoreWord = new ArrayList<>();
public static void populateIgnoreWord()
{
ignoreWord.add(".i.e");
}
public static void main(String args[]) throws IOException
{
String fileStr = Files.readAllLines(Paths.get("D:\\test.txt")).stream().collect(Collectors.joining(" "));
int i=1;
boolean endOfSentance = false;
String sentance="";
for(String word:fileStr.split(" "))
{
if(!ignoreWord.contains(word) && (word.endsWith(".") || word.endsWith("?")))
{
word = word.substring(0, word.length()-1);
sentance+=word+" ";
endOfSentance=false;
sentanceMap.put(i, sentance);
i++;
sentance = new String();
}
else
{
sentance+=word+" ";
}
}
findOccurence(fileStr);
}
private static void findOccurence(String fileStr)
{
fileStr = sentanceMap.values().stream().collect(Collectors.joining(" "));
for(String word:fileStr.split(" "))
{
if(word.equals(""))
continue;
System.out.print(word+" present in: ");
for(Integer key:sentanceMap.keySet())
{
if(sentanceMap.get(key).contains(word+" ") || sentanceMap.get(key).contains(word+".") || sentanceMap.get(key).contains(word+"?"))
{
System.out.print(key+" ");
}
}
System.out.println("\n");
}
}
}