admin
2024-09-14 5356cfea4f3238be5fd14a2d8ea8108d22a1b25e
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
package com.everyday.word;
 
import com.everyday.word.dao.EnglishWordsMapper;
import com.everyday.word.entity.EnglishWords;
import com.everyday.word.service.EnglishWordsService;
import com.everyday.word.utils.YouDaoWebApi;
import com.everyday.word.utils.YouDaoWebUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
 
/**
 * @author hxh
 * @title: WordsTest
 * @description: TODO
 * @date 2024/9/14 13:34
 */
@SpringBootTest
public class WordsTest {
 
    @Resource
    private EnglishWordsService englishWordsService;
 
 
    private Set<String> getFromMOMOFiles() {
        Set<String> words = new HashSet<>();
        File dir = new File("D:\\项目\\单词\\词库\\墨墨单词");
        File[] fs = dir.listFiles();
        for (File f : fs) {
            try {
                StringBuffer text = new StringBuffer();
                Scanner scanner = new Scanner(new FileInputStream( f),"UTF-16");
                while (scanner.hasNextLine()) {
                    text.append(scanner.nextLine());
                }
                scanner.close();
 
                JSONObject root = JSONObject.fromObject(text.toString());
                JSONArray dicts = root.optJSONObject("data").optJSONObject("book").optJSONArray("vocabulary");
                for (int i = 0; i < dicts.size(); i++) {
                    String spelling = dicts.optJSONObject(i).optString("spelling");
                    if(Pattern.matches("^[a-zA-Z]+$", spelling)){
                        words.add(spelling);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
//            break;
        }
        return words;
    }
 
    @Test
    public void addFromMOMOFiles() {
        Set<String> sets = getFromMOMOFiles();
        for(String s:sets){
            englishWordsService.addEnglishWords(EnglishWords.builder().spelling(s).build());
        }
    }
 
    @Test
    public void pullDataFromYouDaoWeb() throws InterruptedException {
        EnglishWordsMapper.DaoQuery daoQuery=new EnglishWordsMapper.DaoQuery();
        int page=5;
        int pageSize = 100;
        List<EnglishWords> list = englishWordsService.list(daoQuery,page, pageSize);
        Set<String> oSpellings=new HashSet<>();
        for(EnglishWords e:list){
            oSpellings.add(e.getSpelling());
        }
        Set<String> spellings=new HashSet<>();
        spellings.addAll(oSpellings);
 
 
 
        Set<String>  infoSellings =   YouDaoWebUtil.getBaseInfoSpellings();
        spellings.removeAll(infoSellings);
        for(String s:spellings){
            try {
                String result = YouDaoWebApi.getInfoBySpelling(s);
                if (!StringUtil.isNullOrEmpty(result) && result.length() > 100) {
                    YouDaoWebUtil.saveBaseInfo(s,result);
                }
            }catch(Exception e){
                e.printStackTrace();
                break;
            }
            Thread.sleep(1000 + (int) (Math.random()*1000));
        }
 
 
        Set<String>  ljSellings =   YouDaoWebUtil.getLJSpellings();
        spellings=new HashSet<>();
        spellings.addAll(oSpellings);
        spellings.removeAll(ljSellings);
        for(String s:spellings){
            try {
                String result = YouDaoWebApi.getLJBySpelling(s);
                if (!StringUtil.isNullOrEmpty(result) && result.length() > 100) {
                    YouDaoWebUtil.saveLJ(s,result);
                }
            }catch(Exception e){
                e.printStackTrace();
                break;
            }
 
            Thread.sleep(1000 + (int) (Math.random()*1000));
 
        }
 
 
    }
 
 
}