이전에 해봤던 인덱스 메이커의 문제점
1. 소수점, 쉼표, 괄호 등의 특수 기호가 단어에 포함된다
2. 숫자 등이 단어로 취급된다
3. 대문자와 소문자가 다른 단어로 취급된다
4. 단어들이 알파벳 순으로 정렬되면 좋겠다
인덱스 메이커 출력값
DEBUG 3
[com 3
project 3
bookmark.mapper 1
BookmarkMapper 1
selectBookmarkByMtIdUserId] 1
<== 1
Total: 1
0 1
review 2
mapper 2
ReviewMapper 2
selectReviewListByMtId] 2
==> 2
Preparing: 1
SELECT 1
`id` 2
, 7
`mtId` 2
`userId` 1
`content` 1
`imagePath` 1
`view` 1
`createdAt` 1
`updatedAt` 1
FROM 1
`review` 1
WHERE 1
= 1
? 1
ORDER 1
BY 1
DESC 1
LIMIT 1
3 1
Parameters: 1
1(Integer) 1
String 클래스 기본 메소드

특수문자 제거하기 기능 추가
// 23
static String [] words = new String [100000]; // 단어들의 목록을 저장할 배열
static int [] count = new int [100000]; // 단어의 등장 횟수
static int n = 0; // 저장된 단어의 개수
public static void main(String[] args) {
//생략
}//
static void makeIndex(String fileName) {
try {
Scanner inFile = new Scanner(new File(fileName));
while (inFile.hasNext()) { //hasnext가 true인 동안에 while문을 돈다
String str = inFile.next();
//trimming 메소드 (조건 추가)
String trimmed = trimming(str);
if (trimmed != null) {
addWord(trimmed);
}
}
inFile.close();
} catch (FileNotFoundException e) {
System.out.println("No file");
return;
}
}
//trimming 함수 (조건 추가)
static String trimming(String str) {
int i = 0; // 문장에서 알파벳이 처음 나온 위치
int j = str.length() -1; // 문장에서 알파벳의 마지막 위치
while (i < str.length() && !Character.isLetter(str.charAt(i))) { // while의 i번째 글자가 알파벳이 아니라면 while문이 계속 돌면서 i의 값을 증가시킨다
// 234%^&$# 처럼 letter가 하나도 없는 경우에는 -1
i++;
}
while (j >= 0 && !Character.isLetter(str.charAt(j))) {
j--;
}
if (i > j) { //j가 i 보다 작은 경우 대비
return null;
}
return str.substring(i, j+1); // i <= ...< j+1
}
//생략
}//
$ read sample.txt
$ exit
DEBUG 3
com 3
project 3
bookmark.mapper 1
BookmarkMapper 1
selectBookmarkByMtIdUserId 1
Total 1
review 3
mapper 2
ReviewMapper 2
selectReviewListByMtId 2
Preparing 1
SELECT 1
id 2
mtId 2
userId 1
content 1
imagePath 1
view 1
createdAt 1
updatedAt 1
FROM 1
WHERE 1
ORDER 1
BY 1
DESC 1
LIMIT 1
Parameters 1
Integer 1
그 다음 알파벳 순서대로 정렬하기
단어들을 알파벳 순으로 정렬하기 위해서는 일단 모든 단어들을 읽어서 인덱스를 만든 후에 한번에 정렬할 수도 있고
항상 정렬된 상태를 유지하도록 삽입하는 방법도 있다
이번에는 항상 정렬된 상태를 유지하도록 하는 방법을 사용한다
//23
static String [] words = new String [100000]; // 단어들의 목록을 저장할 배열
static int [] count = new int [100000]; // 단어의 등장 횟수
static int n = 0; // 저장된 단어의 개수
public static void main(String[] args) {
//생략
}//
static void addWord(String str) {
int index = findWord(str); // 단어 못 찾으면 return -1
if (index != -1) { // 단어가 존재한다면 = words[index] == str일 때
count[index]++;
} else { // 단어가 존재하지 않는다면 == 새로운 단어가 등장했다는 뜻
//알파벳 순서로 정렬 추가
int i = n - 1;
while (i >= 0 && words[i].compareToIgnoreCase(str) > 0) { //뒤에서부터 검사
words[i+1] = words[i];
count[i+1] = count[i];
i--;
}
words[i+1] = str;
count[i+1] = 1;
n++;
}
}
//생략
}//
$ read sample.txt
$ exit
bookmark.mapper 1
BookmarkMapper 1
BY 1
com 3
content 1
createdAt 1
DEBUG 3
DESC 1
FROM 1
id 2
imagePath 1
Integer 1
LIMIT 1
mapper 2
mtId 2
ORDER 1
Parameters 1
Preparing 1
project 3
review 3
ReviewMapper 2
SELECT 1
selectBookmarkByMtIdUserId 1
selectReviewListByMtId 2
Total 1
updatedAt 1
userId 1
view 1
WHERE 1