Skip to content

Commit 26acff9

Browse files
committed
新增成语接龙功能
1 parent ad29fb0 commit 26acff9

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

App.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "react-native";
1111

1212
const getSuggestions = require("./trie-service.js");
13+
const keysWithPrefix = require("./trie-from-idiom.js").keysWithPrefix;
1314

1415
type Props = {};
1516
export default class App extends Component<Props> {
@@ -30,6 +31,22 @@ export default class App extends Component<Props> {
3031
}
3132
};
3233

34+
onChangeText2 = text => {
35+
text = text.trim().toLowerCase();
36+
if (text) {
37+
const suggestions = keysWithPrefix(text).map(item => {
38+
return { word: item };
39+
});
40+
this.setState({
41+
suggestions: suggestions
42+
});
43+
} else {
44+
this.setState({
45+
suggestions: []
46+
});
47+
}
48+
};
49+
3350
keyExtractor = item => item.word;
3451

3552
render() {
@@ -45,14 +62,22 @@ export default class App extends Component<Props> {
4562
autoCapitalize="none"
4663
placeholder="Input the word..."
4764
/>
65+
<TextInput
66+
style={styles.input}
67+
onChangeText={this.onChangeText2}
68+
autoFocus={true}
69+
autoCorrect={false}
70+
autoCapitalize="none"
71+
placeholder="成语接龙..."
72+
/>
4873
<FlatList
4974
style={styles.list}
5075
data={suggestions}
5176
keyExtractor={this.keyExtractor}
5277
renderItem={({ item }) => (
5378
<View style={styles.item}>
5479
<Text>
55-
{item.word}: {item.ipa ? "[ " + item.ipa + " ]" : ""}
80+
{item.word} {item.ipa ? " [ " + item.ipa + " ]" : " "}
5681
{item.translation && " " + item.translation.join(" ")}
5782
</Text>
5883
</View>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Built by react-native.
77
1. instant translate english to chinese when typing
88
2. instant spell-check when typing
99
3. show IPA of inputed word
10+
4. 成语接龙
1011

1112
### android dev
1213

idiom.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"dawg-lookup": "^2.2.1",
16+
"node-ternary-search-trie": "^5.4.3",
1617
"react": "16.8.3",
1718
"react-native": "0.59.8",
1819
"typo-js": "^1.0.3"

trie-from-idiom.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Trie = require("node-ternary-search-trie");
2+
const trie = new Trie();
3+
const data = require("./idiom.json");
4+
data.forEach(element => {
5+
trie.set(element, 0);
6+
});
7+
// console.time("time");
8+
// console.log("keysWithPrefix pref", trie.keysWithPrefix("渊"));
9+
// console.timeEnd("time");
10+
11+
module.exports = trie;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,6 +4269,11 @@ node-pre-gyp@^0.12.0:
42694269
semver "^5.3.0"
42704270
tar "^4"
42714271

4272+
node-ternary-search-trie@^5.4.3:
4273+
version "5.4.3"
4274+
resolved "https://registry.yarnpkg.com/node-ternary-search-trie/-/node-ternary-search-trie-5.4.3.tgz#452dd2549a7d70ed2802016e7a6987ca4f1a3011"
4275+
integrity sha512-DbCM1O2qNShPmWHqno+z39GNgk6LNfFImjkjn6jZM6lgyAwWrFNiOzIQb2wGJ2NprjRlJWS6GAufkuvr+s+pBA==
4276+
42724277
nopt@^4.0.1:
42734278
version "4.0.1"
42744279
resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d"

0 commit comments

Comments
 (0)