Skip to content

Commit

Permalink
feat(use-case): add SideEffectLocationHash
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jun 5, 2016
1 parent af310fd commit 997e55b
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 10 deletions.
5 changes: 4 additions & 1 deletion website/src/component/container/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const React = require("react");
import InputForm from "../../project/InputForm/InputForm";
import AnalyzedTable from "../../project/AnalyzedTable/AnalyzedTable";
import AnalyzedJSONField from "../../project/AnalyzedJSONField/AnalyzedJSONField";
import SideEffectLocationHash from "../../project/SideEffectLocationHash/SideEffectLocationHash";
import AppLocator from "../../../AppLocator";
import UpdateAnalyzedTableUseCase from "../../../js/use-case/analyzer/UpdateAnalyzedTableUseCase";
export default class App extends React.Component {
Expand All @@ -28,13 +29,15 @@ export default class App extends React.Component {
* @type {AnalyzerState}
*/
const analyzer = this.state.analyzer;
const currentText = analyzer.currentText;
const outputJSON = analyzer.outputJSON;
const onSubmit = (text) => {
AppLocator.context.useCase(UpdateAnalyzedTableUseCase.create()).execute(text);
};
return <div className="App">
<SideEffectLocationHash text={currentText} />
<div className="App-InputForm">
<InputForm onSubmit={onSubmit}/>
<InputForm defaultValue={currentText} onSubmit={onSubmit}/>
</div>
<AnalyzedTable tokens={analyzer.tokens}/>
<AnalyzedJSONField outputJSON={outputJSON}/>
Expand Down
3 changes: 2 additions & 1 deletion website/src/component/container/AppBootStrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import InitializeUseCase from "../../js/use-case/InitializeUseCase";
const AppLoading = LoadingContainer(App);
export default class AppBootStrap extends React.Component {
render() {
const promise = AppLocator.context.useCase(InitializeUseCase.create()).execute();
const hash = location.hash.slice(1);
const promise = AppLocator.context.useCase(InitializeUseCase.create()).execute({hash});
return <AppLoading promise={promise} />;
}
}
3 changes: 2 additions & 1 deletion website/src/component/project/InputForm/InputForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default class InputForm extends React.Component {
<div className="control has-addons">
<input ref={(c) => this.input = c}
className="InputForm-input input is-expanded"
type="text" name="name" size="20"/>
type="text" name="name" size="20"
defaultValue={this.props.defaultValue ? this.props.defaultValue: null}/>
<p className="control">
<input className="button is-info" type="submit" value="解析"/>
</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// LICENSE : MIT
"use strict";
const React = require("react");
export default class SideEffectLocationHash extends React.Component {

shouldComponentUpdate(nextProps, nextState) {
return nextProps.text !== undefined && nextProps.text.length > 0;
}

render() {
this._updateHash(this.props.text);
return null;
}


_updateHash(text) {
if (text && text.length > 0) {
location.hash = text;
}
}
}
SideEffectLocationHash.propTypes = {
updateText: React.PropTypes.func,
text: React.PropTypes.string
};
5 changes: 4 additions & 1 deletion website/src/js/domain/analyzer/Analyzer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// LICENSE : MIT
"use strict";
import Token from "../token/Token";
export default class Analyzer {
constructor({tokenizer}) {
this.currentText = "";
Expand All @@ -9,6 +10,8 @@ export default class Analyzer {

analyze(text) {
this.currentText = text;
this.analyzedTokens = this.tokenizer.tokenize(text);
this.analyzedTokens = this.tokenizer.tokenize(text).map(rawToken => {
return new Token(rawToken);
});
}
}
34 changes: 34 additions & 0 deletions website/src/js/domain/token/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,38 @@ export default class Token {
// 発音
this.pronunciation = object.pronunciation;
}

toJSON() {
const basicResults = {
// 表層形
surface_form: this.surface_form,
// 品詞
pos: this.pos,
// 品詞細分類1
pos_detail_1: this.pos_detail_1,
// 品詞細分類2
pos_detail_2: this.pos_detail_2,
// 品詞細分類3
pos_detail_3: this.pos_detail_3,
// 活用型
conjugated_type: this.conjugated_type,
// 活用形
conjugated_form: this.conjugated_form,
// 基本形
basic_form: this.basic_form,
// 読み
reading: this.reading,
// 発音
pronunciation: this.pronunciation
};
const filteredResult = {};
Object.keys(basicResults).forEach(key => {
const value = basicResults[key];
if (!value || value.length === 0) {
return;
}
filteredResult[key] = value;
});
return filteredResult;
}
}
1 change: 1 addition & 0 deletions website/src/js/store/analyzer/AnalyzerState.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class AnalyzerState extends ReduceState {
*/
constructor({analyzer = {}} = {}) {
super();
this.currentText = analyzer.currentText;
this.tokens = analyzer.analyzedTokens || [];
}

Expand Down
13 changes: 7 additions & 6 deletions website/src/js/use-case/InitializeUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import {UseCase} from "almin";
import analyzedRepository from "../infra/repository/AnalyzerRepository";
import AnalyzerFactory from "../domain/analyzer/AnalyzerFactory";
export default class UpdateAnalyzedTableUseCase extends UseCase {
import UpdateAnalyzedTableUseCase from "./analyzer/UpdateAnalyzedTableUseCase";
export default class InitializeUseCase extends UseCase {
static create() {
return new this({
analyzedRepository
Expand All @@ -18,12 +19,12 @@ export default class UpdateAnalyzedTableUseCase extends UseCase {
this.analyzedRepository = analyzedRepository;
}

/**
* @param {string} text
*/
execute(text) {
AnalyzerFactory.create().then((analyzer) => {
execute({hash}) {
const defaultText = hash || "日本語の文章を解析します。";
return AnalyzerFactory.create().then((analyzer) => {
this.analyzedRepository.save(analyzer);
const initializeAnalyzedText = new UpdateAnalyzedTableUseCase({analyzedRepository: this.analyzedRepository});
return this.context.useCase(initializeAnalyzedText).execute(defaultText);
});
}
}

0 comments on commit 997e55b

Please sign in to comment.