Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from ZhaoDingfan/basic-structure
Browse files Browse the repository at this point in the history
Basic structure built up
  • Loading branch information
dfz2019 authored Jun 2, 2018
2 parents a42c0c3 + c5e234f commit 331860f
Show file tree
Hide file tree
Showing 23 changed files with 238 additions and 42 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/org/mifos/chatbot/core/ChatService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mifos.chatbot.core;

import org.mifos.chatbot.core.model.Message;

/**
* User input
*/

public interface ChatService {
void connect(ChatCallBack chatCallBack);
void disconnect();
void send(Message msg);

interface ChatCallBack {
void onMessage(Message msg);
}
}
4 changes: 0 additions & 4 deletions core/src/main/java/org/mifos/chatbot/core/Intent.java

This file was deleted.

12 changes: 12 additions & 0 deletions core/src/main/java/org/mifos/chatbot/core/MifosService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.mifos.chatbot.core;

import org.mifos.chatbot.core.model.MifosRequest;
import org.mifos.chatbot.core.model.MifosResponse;

/**
* Connection with Mifos backend
*/

public interface MifosService {
MifosResponse process(MifosRequest request);
}
11 changes: 11 additions & 0 deletions core/src/main/java/org/mifos/chatbot/core/NLPService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mifos.chatbot.core;

import org.mifos.chatbot.core.model.Intent;

/**
* This interface will handle the OpenNLP engine currently
* If there are other NLP engines, simply add more interfaces for additional NLP engines
*/
public interface NLPService {
Intent recognize(String text);
}
5 changes: 5 additions & 0 deletions core/src/main/java/org/mifos/chatbot/core/model/Intent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mifos.chatbot.core.model;

public class Intent {
// the data holder
}
7 changes: 7 additions & 0 deletions core/src/main/java/org/mifos/chatbot/core/model/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.mifos.chatbot.core.model;

public class Message {
private String from;
private String to;
private String text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mifos.chatbot.core.model;

public class MifosRequest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.mifos.chatbot.core.model;

public class MifosResponse {
}
8 changes: 0 additions & 8 deletions core/src/main/java/org/mifos/chatbot/core/model/OpenNLP.java

This file was deleted.

4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon May 21 22:10:44 SGT 2018
#Thu May 24 23:47:34 SGT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
36 changes: 35 additions & 1 deletion nlp/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
description = "Mifos Chatbot Client"
// https://mvnrepository.com/artifact/org.apache.opennlp/opennlp-tools

buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"

repositories {
mavenCentral()
}

dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:1.1.1.RELEASE'
}
}

dependencies {
compile group: 'org.apache.opennlp', name: 'opennlp-tools', version: '1.5.2-incubating'

compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'junit', name: 'junit', version: '4.4'
compile 'org.apache.opennlp:opennlp-tools:1.8.4'
compile 'org.springframework:spring-core'
compile project(':core')
}


8 changes: 0 additions & 8 deletions nlp/src/main/java/org/mifos/chatbot/nlp/KeywordFinder.java

This file was deleted.

5 changes: 0 additions & 5 deletions nlp/src/main/java/org/mifos/chatbot/nlp/OpenNLPHandler.java

This file was deleted.

21 changes: 21 additions & 0 deletions nlp/src/main/java/org/mifos/chatbot/nlp/OpenNLPService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.mifos.chatbot.nlp;

import org.mifos.chatbot.core.NLPService;
import org.mifos.chatbot.core.model.Intent;

public class OpenNLPService implements NLPService {

@Override
public Intent recognize(String text) {
return null;
}

// Refer to OpenNLP framework as much as I want

// OpenNLP may has to be trained by myself to be financial focused
// May start with 10 keywords first, let framework understand different sentence structure.



// create training set and training model by myself instead of using pre-trained model
}
4 changes: 0 additions & 4 deletions nlp/src/main/java/org/mifos/chatbot/nlp/POSTagger.java

This file was deleted.

4 changes: 0 additions & 4 deletions nlp/src/main/java/org/mifos/chatbot/nlp/SentenceDetector.java

This file was deleted.

4 changes: 0 additions & 4 deletions nlp/src/main/java/org/mifos/chatbot/nlp/Tokenizer.java

This file was deleted.

16 changes: 16 additions & 0 deletions nlp/src/test/java/org/mifos/chatbot/nlp/OpenNLPServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.mifos.chatbot.nlp;

import org.junit.Test;
import org.mifos.chatbot.core.NLPService;
import org.mifos.chatbot.core.model.Intent;
import org.springframework.beans.factory.annotation.Autowired;

public class OpenNLPServiceTest {
@Autowired
private NLPService nlpService;

@Test
public void recognizeTest() {
Intent result = nlpService.recognize("");
}
}
81 changes: 81 additions & 0 deletions nlp/src/test/java/org/mifos/chatbot/nlp/OpenNLPTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.mifos.chatbot.nlp;

import opennlp.tools.cmdline.PerformanceMonitor;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.InputStreamFactory;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import org.junit.Test;

import java.io.*;

public class OpenNLPTest {
@Test
public void detectSentenceTest() throws Exception {
String sentence = "";
InputStream is = new FileInputStream("en-sent.bin");
SentenceModel model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model);

String sentences[] = sdetector.sentDetect(sentence);
is.close();
}

@Test
public void tokenize() throws Exception {
InputStream is = new FileInputStream("en-token.bin");

TokenizerModel model = new TokenizerModel(is);

Tokenizer tokenizer = new TokenizerME(model);

String tokens[] = tokenizer.tokenize("Hi. How are you? This is Mike.");

for (String a : tokens)
System.out.println(a);

is.close();
}

@Test
public void posTag() throws Exception {
POSModel model = new POSModelLoader()
.load(new File("en-pos-maxent.bin"));
PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
POSTaggerME tagger = new POSTaggerME(model);

String input = "Hi. How are you? This is Mike.";
ObjectStream<String> lineStream = new PlainTextByLineStream(new InputStreamFactory() {
@Override
public InputStream createInputStream() throws IOException {
return new ByteArrayInputStream(input.getBytes());
}
}, "UTF-8");

perfMon.start();
String line;
while ((line = lineStream.read()) != null) {

String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE
.tokenize(line);
String[] tags = tagger.tag(whitespaceTokenizerLine);

POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
System.out.println(sample.toString());

perfMon.incrementCounter();
}
perfMon.stopAndPrintFinalResult();
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.mifos.chatbot.server;

import org.mifos.chatbot.core.ChatService;
import org.mifos.chatbot.core.model.Message;

public class ChatWithUserService implements ChatService {

@Override
public void connect(ChatCallBack chatCallBack) {

}

@Override
public void disconnect() {

}

@Override
public void send(Message msg) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@SpringBootApplication
public class MifosChatbotApplication {

public static void main(String[] args) {
SpringApplication.run(MifosChatbotApplication.class, args);
}
Expand Down
1 change: 1 addition & 0 deletions server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.port=10000
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ include ":core"
include ":nlp"
include ":protocol"
include ":server"

0 comments on commit 331860f

Please sign in to comment.