java中语法规则的实现

java中语法规则的实现,java,algorithm,logic,Java,Algorithm,Logic,我需要一些逻辑来找到一个语法模式,比如在一个句子中: [adjective]* [noun]+ [hyphen] [verb Past Participle | verb Present Participle | one of the special adjectives] [adjective]* [noun]+ 其中*表示任何数字(0或更多)?表示0或1,+表示1或更多,|表示或 如果我给出任何输入语句,逻辑必须搜索它是否包含上述模式。 我完全不知道如何开始。 如果有人能给我一些逻辑建议,

我需要一些逻辑来找到一个语法模式,比如在一个句子中:

[adjective]* [noun]+ [hyphen] [verb Past Participle | verb Present Participle | one of the special adjectives] [adjective]* [noun]+
其中*表示任何数字(0或更多)?表示0或1,+表示1或更多,|表示或

如果我给出任何输入语句,逻辑必须搜索它是否包含上述模式。 我完全不知道如何开始。
如果有人能给我一些逻辑建议,请告诉我。

这是伪代码。它对输入进行2次传递,在第一次传递中,它将输入字符串中的每个单词转换为表示其类型的字母,在第二次传递中,您将第一次传递的结果与正则表达式匹配

method(input) {
    typed_input = '';
    for (word in input) {
        if (word is noun) {
            typed_input += 'n'
        else if (word is adjective)
            typed_input += 'a'
        else if (word is hyphen)
            typed_input += 'h'
        else if (word is verb Past Participle)
            typed_input += 'v'
        else if (word is verb Present Participle)
            typed_input += 'p'
        else if (word is one of the special adjectives)
            typed_input += 's'
        else
           throw exception("invalid input")
    }
    return typed_input.match("a*n+h[v|p|s]a*n+")
}

你写的语法模式很简单,不实用。您应该使用块解析。句子中的形容词可能不仅仅是一个词(如“猫”),也可能是一大块词(如“棕色眼睛的黑猫”)

当句子包含“块”而不是单个形容词时,你的模式就会失败。句子应该像树结构一样解析

语法检查是一个相当复杂的问题在你写任何东西之前-你应该熟悉语法检查和自然语言处理的理论。

您可以从以下内容开始:

也许这也是:

我可以把它放在评论中,但标题很长,在这里更容易阅读。

这可能会对您有所帮助。
您还可以下载java中的代码

我已经使用stand ford解析器用java编写了simler程序。您应该使用java stand ford解析器生成数组列表的标记字

 package postagger;
    /*
     * 
     * 
     * lphabetical list of part-of-speech tags used in the Penn Treebank Project:

    Number
    Tag
    Description
    1.  CC  Coordinating conjunction
    2.  CD  Cardinal number
    3.  DT  Determiner
    4.  EX  Existential there
    5.  FW  Foreign word
    6.  IN  Preposition or subordinating conjunction
    7.  JJ  Adjective
    8.  JJR Adjective, comparative
    9.  JJS Adjective, superlative
    10. LS  List item marker
    11. MD  Modal
    12. NN  Noun, singular or mass
    13. NNS Noun, plural
    14. NNP Proper noun, singular
    15. NNPS    Proper noun, plural
    16. PDT Predeterminer
    17. POS Possessive ending
    18. PRP Personal pronoun
    19. PRP$    Possessive pronoun
    20. RB  Adverb
    21. RBR Adverb, comparative
    22. RBS Adverb, superlative
    23. RP  Particle
    24. SYM Symbol
    25. TO  to
    26. UH  Interjection
    27. VB  Verb, base form
    28. VBD Verb, past tense
    29. VBG Verb, gerund or present participle
    30. VBN Verb, past participle
    31. VBP Verb, non-3rd person singular present
    32. VBZ Verb, 3rd person singular present
    33. WDT Wh-determiner
    34. WP  Wh-pronoun
    35. WP$ Possessive wh-pronoun
    36. WRB Wh-adverb
     */
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.LinkedHashSet;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.io.StringReader;

    import semanticengine.Description;


    import edu.stanford.nlp.objectbank.TokenizerFactory;
    import edu.stanford.nlp.process.CoreLabelTokenFactory;
    import edu.stanford.nlp.process.DocumentPreprocessor;
    import edu.stanford.nlp.process.PTBTokenizer;
    import edu.stanford.nlp.ling.CoreLabel;  
    import edu.stanford.nlp.ling.HasWord;  
    import edu.stanford.nlp.ling.TaggedWord;
    import edu.stanford.nlp.trees.*;
    import edu.stanford.nlp.parser.lexparser.LexicalizedParser;

    public class EnglishParser {
    public    static LexicalizedParser lp = null;

      public static void main(String[] args)
      {



          EnglishParser MC=new EnglishParser();
          Scanner sc=new Scanner(System.in);
          String s="";
          while(s!="end")
          {
          s=sc.nextLine();
          ArrayList<TaggedWord> AT=MC.Parse(s);
          Description obj=   new  Description(AT );

          System.out.println (AT);
          }


      }



    public static void demoDP(LexicalizedParser lp, String filename) {
        // This option shows loading and sentence-segment and tokenizing
        // a file using DocumentPreprocessor
        TreebankLanguagePack tlp = new PennTreebankLanguagePack();
        GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
        // You could also create a tokenier here (as below) and pass it
        // to DocumentPreprocessor
        for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
          Tree parse = lp.apply(sentence);
          parse.pennPrint();
          System.out.println();

          GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
          Collection tdl = gs.typedDependenciesCCprocessed(true);
          System.out.println(tdl);
          System.out.println();
        }
      }










      //Method for Pos taging.(POS) tagger that assigns its class
      //(verb, adjective, ...) to each word of the sentence,
      //para@ english is the argument to be tagged
      public ArrayList<TaggedWord> Parse(String English)
      { 
          String[] sent =English.split(" ");// { "This", "is", "an", "easy", "sentence", "." };
          List<CoreLabel> rawWords = new ArrayList<CoreLabel>();
          for (String word : sent) {
              CoreLabel l = new CoreLabel();
              l.setWord(word);
              rawWords.add(l);
                }
      Tree parse = lp.apply(rawWords);
      return parse.taggedYield();

      }










      public EnglishParser() 
      {
          lp = 
                  new LexicalizedParser("grammar/englishPCFG.ser.gz");


      } // static methods only

    }


    // return pattern of the sentence
        public String getPattern(ArrayList<TaggedWord> Sen) 
{
            Iterator<TaggedWord> its = Sen.iterator();
            while (its.hasNext()) {
                TaggedWord obj = its.next();
                if ((obj.tag().equals("VBZ")) || (obj.tag().equals("VBP"))) {
                    if (its.hasNext()) {
                        TaggedWord obj2 = its.next();

                        if (obj2.tag().equals("VBG")) {
                            if (its.hasNext()) {
                                TaggedWord obj3 = its.next();
                                if ((obj3.tag().equals("VBN"))) {
                                    return "PRESENT_CONT_PASS";

                                }
                            }
                            return "PRESENT_CONT";
                            // Present Continues
                        } else if ((obj2.tag().equals("VBN"))) {
                            return "PRESENT_PASS";

                        }
                        return "PRESENT_SIMP";

                    } else {
                        return "PRESENT_SIMP";
                    }

                } else if (obj.tag().equals("VBD")) {
                    if (its.hasNext()) {
                        TaggedWord obj2 = its.next();

                        if (obj2.tag().equals("VBG")) {

                            if (its.hasNext()) {
                                TaggedWord obj3 = its.next();
                                if ((obj3.tag().equals("VBN"))) {
                                    return "PATT_CONT_PASS";

                                }
                            }

                            return "PAST_CONT";
                        } else if ((obj2.tag().equals("VBN"))) {
                            return "PAST_PASS";

                        }
                        return "PAST_SIMP";

                    } else {
                        return "PAST_SIMP";
                    }

                }

                else if (obj.tag().equals("VB")) {
                    if (its.hasNext()) {
                        TaggedWord obj2 = its.next();

                        if (obj2.tag().equals("VBG")) {
                            return "FUT_CONT";
                        } else if ((obj2.tag().equals("VBN"))) {
                            return "FUT_CONT";

                        }

                    } else {
                        return "FUT_SIMP";
                    }

                }

            }
            return "NO_PATTERN";
        }
package-postager;
/*
* 
* 
*宾夕法尼亚树库项目中使用的词性标签列表:
数
标签
描述
1.协调连词
2.CD基数
3.DT测定器
4.前存在主义者
5.外来词
6.用介词或从属连词
7.JJ形容词
8.比较级形容词
9最高级形容词
10列表项标记
11MD模态
12NN名词,单数或质量
13名词,复数
14NNP专有名词,单数
15NNPS专有名词,复数
16PDT预测器
17POS所有格结尾
18人称代词
19所有格代词
20RB副词
21比较副词
22RBS副词,最高级
23RP颗粒
24符号
25到
26嗯感叹词
27动词,基本形式
28动词,过去式
29动词、动名词或现在分词
30动词过去分词
31VBP动词,非第三人称单数present
32VBZ动词,第三人称单数present
33WDT Wh限定词
34代词
35WP$所有格wh代词
36Wh副词
*/
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.LinkedHashSet;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
导入java.util.Scanner;
导入java.io.StringReader;
导入语义引擎描述;
导入edu.stanford.nlp.objectbank.TokenizerFactory;
导入edu.stanford.nlp.process.CoreLabelTokenFactory;
导入edu.stanford.nlp.process.DocumentPreprocessor;
导入edu.stanford.nlp.process.PTBTokenizer;
导入edu.stanford.nlp.ling.corelab;
导入edu.stanford.nlp.ling.HasWord;
导入edu.stanford.nlp.ling.TaggedWord;
导入edu.stanford.nlp.trees.*;
导入edu.stanford.nlp.parser.lexparser.LexicalizedParser;
公务舱英语播音员{
公共静态LexicalizedParser lp=null;
公共静态void main(字符串[]args)
{
EnglishParser MC=新的EnglishParser();
扫描仪sc=新的扫描仪(System.in);
字符串s=“”;
而(s!=“结束”)
{
s=sc.nextLine();
ArrayList AT=MC.Parse;
描述obj=新描述(AT);
System.out.println(AT);
}
}
公共静态void解调器(LexicalizedParser lp,字符串文件名){
//此选项显示加载和句子段以及标记化
//使用文档预处理器的文件
TreebankLanguagePack tlp=新的PennTreebankLanguagePack();
语法结构工厂gsf=tlp.grammaticStructureFactory();
//您还可以在这里创建一个标记器(如下所示)并传递它
//文档预处理器
for(列表语句:新文档预处理器(文件名)){
树解析=lp.apply(句子);
parse.pennPrint();
System.out.println();
语法结构gs=gsf.newgrammaticstructure(parse);
集合tdl=gs.TypedDependenciescpProcessed(真);
系统输出打印LN(tdl);
System.out.println();
}
}
//Pos标记的方法。(Pos)指定其类的标记器
//(动词,形容词,…)到句子的每个单词,
//para@english是要标记的参数
公共ArrayList解析(字符串英语)
{ 
String[]sent=English.split(“”;//{“This”、“is”、“an”、“easy”、“句子”、“句子”);
List rawWords=new ArrayList();
for(字符串字:已发送){
CoreLabel l=新CoreLabel();
l、 设定字;
添加(l);
}
树解析=lp.apply(rawWords);
返回parse.taggedYield();
}
公共英语
{
lp=
新的词汇化解析器(“grammar/englishPCFG.ser.gz”);
}//仅限静态方法
}
//句子的返回模式
公共字符串getPattern(ArrayList Sen)
{
迭代器its=Sen.Iterator();
while(its.hasNext()){
TaggedWord obj=its.next();
if((obj.tag().equals(“VBZ”))| |(obj.tag().equals(“VBP”)){