Skip to content

Commit

Permalink
新增成语接龙功能
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed May 24, 2019
1 parent ad29fb0 commit 26acff9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
27 changes: 26 additions & 1 deletion App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "react-native";

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

type Props = {};
export default class App extends Component<Props> {
Expand All @@ -30,6 +31,22 @@ export default class App extends Component<Props> {
}
};

onChangeText2 = text => {
text = text.trim().toLowerCase();
if (text) {
const suggestions = keysWithPrefix(text).map(item => {
return { word: item };
});
this.setState({
suggestions: suggestions
});
} else {
this.setState({
suggestions: []
});
}
};

keyExtractor = item => item.word;

render() {
Expand All @@ -45,14 +62,22 @@ export default class App extends Component<Props> {
autoCapitalize="none"
placeholder="Input the word..."
/>
<TextInput
style={styles.input}
onChangeText={this.onChangeText2}
autoFocus={true}
autoCorrect={false}
autoCapitalize="none"
placeholder="成语接龙..."
/>
<FlatList
style={styles.list}
data={suggestions}
keyExtractor={this.keyExtractor}
renderItem={({ item }) => (
<View style={styles.item}>
<Text>
{item.word}: {item.ipa ? "[ " + item.ipa + " ]" : ""}
{item.word} {item.ipa ? " [ " + item.ipa + " ]" : " "}
{item.translation && " " + item.translation.join(" ")}
</Text>
</View>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Built by react-native.
1. instant translate english to chinese when typing
2. instant spell-check when typing
3. show IPA of inputed word
4. 成语接龙

### android dev

Expand Down
1 change: 1 addition & 0 deletions idiom.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"dawg-lookup": "^2.2.1",
"node-ternary-search-trie": "^5.4.3",
"react": "16.8.3",
"react-native": "0.59.8",
"typo-js": "^1.0.3"
Expand Down
11 changes: 11 additions & 0 deletions trie-from-idiom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Trie = require("node-ternary-search-trie");
const trie = new Trie();
const data = require("./idiom.json");
data.forEach(element => {
trie.set(element, 0);
});
// console.time("time");
// console.log("keysWithPrefix pref", trie.keysWithPrefix("渊"));
// console.timeEnd("time");

module.exports = trie;
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4269,6 +4269,11 @@ node-pre-gyp@^0.12.0:
semver "^5.3.0"
tar "^4"

node-ternary-search-trie@^5.4.3:
version "5.4.3"
resolved "https://registry.yarnpkg.com/node-ternary-search-trie/-/node-ternary-search-trie-5.4.3.tgz#452dd2549a7d70ed2802016e7a6987ca4f1a3011"
integrity sha512-DbCM1O2qNShPmWHqno+z39GNgk6LNfFImjkjn6jZM6lgyAwWrFNiOzIQb2wGJ2NprjRlJWS6GAufkuvr+s+pBA==

nopt@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d"
Expand Down

0 comments on commit 26acff9

Please sign in to comment.