Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

NL and AGF handling #143

Merged
merged 67 commits into from
Jul 17, 2018
Merged

NL and AGF handling #143

merged 67 commits into from
Jul 17, 2018

Conversation

FlxB2
Copy link
Contributor

@FlxB2 FlxB2 commented Jul 3, 2018

Most stuff is finished. Trees are able to be generated from the content of @grammar annotations (which are now called AGF - Amy Grammar Format - expressions) and checked if valid. The generated trees can also be transformed in valid JSGF rules. Right now i'm working on the NLParser, which categorizes the strings received from the web or speech recognition. The last step will be a NLInterpreter class which returns the right matching grammar.
Feel free to suggest changes.

AGF grammars are specified in the wiki

close #95
fixed #177

@FlxB2 FlxB2 requested review from buddy200 and Legion2 July 3, 2018 13:31
@FlxB2 FlxB2 mentioned this pull request Jul 3, 2018
4 tasks
@Legion2 Legion2 changed the title NL and AGF handling WIP: NL and AGF handling Jul 3, 2018
Copy link
Contributor

@buddy200 buddy200 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In itself, quite understandable. However, general things could be better described in the class Comments

import java.util.List;

/**
* AGF lexer implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to improve understanding in general it would be better to make more detailed Java class comments. (Not only in this class)


/**
*
* the AGF Parser implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

import de.unistuttgart.iaas.amyassist.amy.core.natlang.agf.nodes.AGFNode;

/**
* AGF Parselet interface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

for(int i = 0; i < node.getChilds().size(); i++) {
handleNode(b, node.getChilds().get(i));
if(i!=node.getChilds().size()-1) {
b.append("|");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it makes sense to pack this into a method. I think it is to much code for a case statement

for(int i = 0; i < node.getChilds().size(); i++) {
handleNode(b, node.getChilds().get(i));
if(i!=node.getChilds().size()-1) {
b.append("|");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link

@Amy-Bot Amy-Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube analysis found issues:
Bug Bugs: 0
Vulnerability Vulnerabilities: 0
Code Smell Code Smells: 27

See all issues in SonarCloud

@@ -37,6 +37,7 @@
*
* @author Leon Kiefer
*/
@Deprecated
public class NaturalLanguageInterpreterAnnotationReader {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Add the missing @deprecated Javadoc tag. (squid:MissingDeprecatedCheck)

See it in SonarCloud

@@ -38,7 +39,9 @@
@Target(java.lang.annotation.ElementType.METHOD)
public @interface Grammar {
/**
* @return the grammar
* TODO description
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Complete the task associated to this TODO comment. (squid:S1135)

See it in SonarCloud

@Override
public String process(String naturalLanguageText) {
this.logger.debug("input {}", naturalLanguageText);
NLLexer nlLexer = new NLLexer(naturalLanguageText);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Remove this useless assignment to local variable "nlLexer". (squid:S1854)

See it in SonarCloud

* The keyword
*/
@Deprecated
public void addRule(String ruleName, String[] keywords, String grammar) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Add the missing @deprecated Javadoc tag. (squid:MissingDeprecatedCheck)

See it in SonarCloud

import de.unistuttgart.iaas.amyassist.amy.core.io.Environment;
import de.unistuttgart.iaas.amyassist.amy.core.natlang.agf.AGFLexer;
import de.unistuttgart.iaas.amyassist.amy.core.natlang.agf.AGFParser;
import de.unistuttgart.iaas.amyassist.amy.core.natlang.agf.AGFToken;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Remove this unused import 'de.unistuttgart.iaas.amyassist.amy.core.natlang.agf.AGFToken'. (squid:UselessImportCheck)

See it in SonarCloud

/**
* contains regex with the corresponding WordTokenType
*/
Map<String, WordTokenType> regexTokenType = new HashMap<>();{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Move the contents of this initializer to a standard constructor or to field initializers. (squid:S1171)

See it in SonarCloud

.isAnnotationPresent(de.unistuttgart.iaas.amyassist.amy.core.plugin.api.SpeechCommand.class)) {
throw new IllegalArgumentException();
}
String[] speechKeyword = NLIAnnotationReader.getSpeechKeywords(natuaralLanguageInterpreter);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Remove this useless assignment to local variable "speechKeyword". (squid:S1854)

See it in SonarCloud

Object[] params = { input };
return (String) this.method.invoke(instance, params);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Tryed to invoke " + this.method.getName(), e);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Define and throw a dedicated exception instead of using a generic one. (squid:S00112)

See it in SonarCloud

@@ -27,7 +27,8 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//TODO move the class in a package in the natlang package
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Complete the task associated to this TODO comment. (squid:S1135)

See it in SonarCloud

@@ -51,14 +51,16 @@
* @author Leon Kiefer
*/
@Service
@Deprecated
public class SpeechCommandHandler {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Smell Code Smell: Add the missing @deprecated Javadoc tag. (squid:MissingDeprecatedCheck)

See it in SonarCloud

Legion2 added 5 commits July 6, 2018 14:02
# Conflicts:
#	core/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/speech/result/handler/SpeechCommandHandler.java
#	core/src/test/java/de/unistuttgart/iaas/amyassist/amy/core/natlang/TestNLIAnnotationReader.java
cleanup tests
moved completeSetup to GrammarObjectsCreator
+ "nine | ten | eleven | twelve | thirteen | fourteen | fifteen | "
+ "sixteen | seventeen | eighteen | nineteen | twenty | thirty | forty | "
+ "fifty | sixty | seventy | eighty | ninety | and )+; \n");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to do this, so the number 'and' is not allowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's saver for the speech recognition, i could make another rule for "and" but that would be kind of pointless

grammar.append("\n//pre defined rules \n");

// pre defined rules
grammar.append("<digit> = (one | two | three | four | five | six | seven |"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not digits but numers, because of the '+' at the end.

public String addRule(AGFNode node, String ruleName) {
StringBuilder b = new StringBuilder();
b.append("public <" + ruleName + "> = ");
String rule = handleNode(b, node) + ";\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code duplicate: Use private method publicRule().

try {
return Integer.valueOf(numberInt);
}catch(NumberFormatException e) {
this.logger.error(String.format("number in numbers file in wrong format %s" , numberInt));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logger already has the ability to add parameters into the string.

}catch(NumberFormatException e) {
this.logger.error(String.format("number in numbers file in wrong format %s" , numberInt));
}
return Integer.MAX_VALUE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense? If not it would be better to just rethrow a IllegalStateException.

*
* @param method
* the method that should be a NLIMethod
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @throws in JavaDoc.

@sonarqubecloud sonarqubecloud bot deleted a comment from FlxB2 Jul 14, 2018
@sonarqubecloud sonarqubecloud bot deleted a comment from Legion2 Jul 14, 2018
@sonarqubecloud sonarqubecloud bot deleted a comment from FlxB2 Jul 14, 2018
@Legion2 Legion2 changed the title WIP: NL and AGF handling NL and AGF handling Jul 14, 2018
grammar.append("<number> = (one | two | three | four | five | six | seven |"
+ "nine | ten | eleven | twelve | thirteen | fourteen | fifteen | "
+ "sixteen | seventeen | eighteen | nineteen | twenty | thirty | forty | "
+ "fifty | sixty | seventy | eighty | ninety | and )+; \n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only allow and if there is number before and behind.

Map<String, Integer> result = new HashMap<>();
String[] stringNmbRep;

InputStream grammarFile = this.getClass().getResourceAsStream("englishNumbers.natlang");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use getClass().getClassLoader().getResourceAsStream()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it strangely only works without .getClassLoader() now, no idea why

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ClassLoader have a absolute scope and relative paths don't work

BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {

// first line contains all numbers seperated by ',' and ending with ';'
String s = bufferedReader.readLine();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also have one rule in one line and iterate over all lines.


/**
*
* calls the given method and handles handles possible exceptions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

language.

}

/**
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line


private final List<PartialNLI> register = new ArrayList<>();

private final List<AGFNode> cachedNodeList = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is not describing the use very good. "cached" sounds like this list is not necessary, just for speed improvements.

//only allow normal SPACE codepoint 32, U+0020
case 32:
return this.next();
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong indentation.


}
}
throw new NoSuchElementException("thrown by AGFLexer");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where it is thrown, is automatically contained in this exception. If no good reason can be stated, use constructor without parameter.

/**
* serial version uid
*/
private static final long serialVersionUID = 1L;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate svuid, don't use 1L.

}

/**
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add method description.

@sonarqubecloud sonarqubecloud bot deleted a comment from FlxB2 Jul 16, 2018
@AmyAssist AmyAssist deleted a comment from FlxB2 Jul 17, 2018
@FlxB2 FlxB2 merged commit 52e2d55 into dev Jul 17, 2018
@FlxB2 FlxB2 deleted the NLandAGFparser branch July 17, 2018 08:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grammar file does not get overwritten correctly Natural Language to Plugin Function
5 participants