-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStep0615.java
More file actions
169 lines (151 loc) · 7.79 KB
/
Step0615.java
File metadata and controls
169 lines (151 loc) · 7.79 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
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Integer;
import java.lang.System;
import java.util.Collections;
public class Step0615{
public static void main(String[] args){
Scanner scanner =new Scanner(System.in);
System.out.println("調べたい単語を入力してください:");
String inputWord= scanner.next();
int inputWordIndex = wikiWordsSearch(inputWord);
ArrayList<Integer> relatedWordsIndex=relatedWordsSearch(inputWordIndex);
ArrayList<Integer> reRelatedWords=reRelatedWordsSearch(relatedWordsIndex);
double percent=reRelatedPerRelated(relatedWordsIndex,reRelatedWords);
System.out.println("--------------------------------------------------");
System.out.println(inputWord+"の'友達の友達'に含まれる'友達'の割合は"+ percent+"%です");
}
public static String[] readWikiWords() {
String wikiWords [] = new String[1500000];
int paramForWikiWords=0;
try{
Scanner scanner = new Scanner(new File("pages.txt"));
System.out.println("open File, 'pages.txt'");
while(scanner.hasNextLine()){
String pageLine =scanner.nextLine();
String[] indexWikiWord =pageLine.split("\t");
wikiWords[paramForWikiWords]=indexWikiWord[1];
paramForWikiWords++;
}
} catch (FileNotFoundException e){
System.out.println("can't open File, 'pages.txt'");
}
return wikiWords;
// # wikiWords=['アンパサンド', '言語', '日本語',...]
}
public static int wikiWordsSearch(String inputWord){
String wikiWords[] = readWikiWords();
// # wikiWords=['アンパサンド', '言語', '日本語',...]
if(Arrays.asList(wikiWords).contains(inputWord)){
System.out.println("--------------------------------------------------");
System.out.println("'"+inputWord+"'についてのサイトはWikipediaに存在します");
return Arrays.asList(wikiWords).indexOf(inputWord);
// # return index of inputWord -> relatedWords_Search
}else{
System.out.println("'"+inputWord+"'についてのサイトはWikipediaに存在しません");
System.out.println("Error");
return -1;
}
}
public static ArrayList<Integer> relatedWordsSearch(int inputWordIndex){
if(inputWordIndex==-1){
ArrayList<Integer> nullList = new ArrayList<Integer>(1);
return nullList;
}else{
int wordIndexs[] = new int[60000000];
int wikiLinks[] = new int[60000000];
ArrayList<String> relatedWords = new ArrayList<String>(150000);
ArrayList<Integer> relatedWordsIndex = new ArrayList<Integer>(150000);
String[] wikiWords = readWikiWords();
try{
Scanner scanner = new Scanner(new File("links.txt"));
System.out.println("open File, 'links.txt'");
int paramForWikiLinks=0;
while(scanner.hasNextLine()){
//long before = System.nanoTime();
String linkLine =scanner.nextLine();
String[] wordIndexWikiLink = linkLine.split("\t");
wordIndexs[paramForWikiLinks]=Integer.valueOf(wordIndexWikiLink[0]);
wikiLinks[paramForWikiLinks]=Integer.valueOf(wordIndexWikiLink[1]);
paramForWikiLinks++;
//long after = System.nanoTime();
//System.out.println((after - before));
// #wordIndexs=[0,0,0,...]
// #WikiLinks=[284171, 955, 591, ...]
}
}catch(FileNotFoundException e){
System.out.println("can't read File, 'links.txt'");
}
int paramForWordIndexs = 0;
int paramForRelatedWordsIndex=0;
while (paramForWordIndexs < wordIndexs.length) {
if (wordIndexs[paramForWordIndexs] > inputWordIndex) {
break;
} else if (wordIndexs[paramForWordIndexs] == inputWordIndex) {
relatedWords.add(Arrays.asList(wikiWords).get(wikiLinks[paramForWordIndexs]));
relatedWordsIndex.add(wikiLinks[paramForWordIndexs]);
paramForRelatedWordsIndex++;
}
paramForWordIndexs++;
}
System.out.println("--------------------------------------------------");
System.out.println(Arrays.asList(wikiWords).get(inputWordIndex) + "のサイトから飛べるリンクの一覧を表示します");
System.out.println(Arrays.asList(relatedWords));
Collections.sort(relatedWordsIndex);
return relatedWordsIndex;
}
}
public static ArrayList<Integer> reRelatedWordsSearch(ArrayList<Integer> relatedWordsIndex) {
//for relatedWordIndex in relatedWordsIndex;
ArrayList<Integer> reRelatedWords = new ArrayList<Integer>(30000000);
int wordIndexs[] = new int[60000000];
int wikiLinks[] = new int[60000000];
String[] wikiWords = readWikiWords();
try {
Scanner scanner = new Scanner(new File("links.txt"));
System.out.println("open File, 'links.txt'");
int paramForWikiLinks = 0;
while (scanner.hasNextLine()) {
// long before = System.nanoTime();
String linkLine = scanner.nextLine();
String[] wordIndexWikiLink = linkLine.split("\t");
wordIndexs[paramForWikiLinks] = Integer.valueOf(wordIndexWikiLink[0]);
wikiLinks[paramForWikiLinks] = Integer.valueOf(wordIndexWikiLink[1]);
paramForWikiLinks++;
// long after = System.nanoTime();
// System.out.println((after - before));
// #wordIndexs=[0,0,0,...]
// #WikiLinks=[284171, 955, 591, ...]
}
} catch (FileNotFoundException e) {
System.out.println("can't read File, 'links.txt'");
}
System.out.println("Finish reading File, 'links.txt'");
//ここの中身確認する!
for (int paramForReRelatedWords = 0; paramForReRelatedWords < relatedWordsIndex.size(); paramForReRelatedWords++) {
for(int paramForReRelatedWordsSearch=0; paramForReRelatedWordsSearch<wordIndexs.length;paramForReRelatedWordsSearch++){
if(wordIndexs[paramForReRelatedWordsSearch]==relatedWordsIndex.get(paramForReRelatedWords)){
if(!(reRelatedWords.contains(wikiLinks[paramForReRelatedWordsSearch]))){
reRelatedWords.add(wikiLinks[paramForReRelatedWordsSearch]);
}
}else if(wordIndexs[paramForReRelatedWordsSearch]> relatedWordsIndex.get(relatedWordsIndex.size()-1)){
break;
}
}
}
Collections.sort(reRelatedWords);
return reRelatedWords;
}
public static double reRelatedPerRelated( ArrayList<Integer> relatedWordsIndex, ArrayList<Integer> reRelatedWords ){
int matchedCount=0;
for(int paramForCalPer=0;paramForCalPer< relatedWordsIndex.size();paramForCalPer++){
if(reRelatedWords.contains(relatedWordsIndex.get(paramForCalPer))){
matchedCount++;
}
}
return (double)matchedCount*100/ reRelatedWords.size();
}
}