使用词边界和 POS 将句子拆分为固定大小的块

Dav*_*nch 5 java string recursion split nlp

我正在尝试使用 Java 根据单词边界和 POS(词性)将句子拆分为固定的分块关键字短语(请参阅本文末尾的更新代码)

1)无视某些POS

2)某些POS 不能作为root 关键字。

并产生以下输出:

**Root Keyword:** In
**Phrase:** None
Run Code Online (Sandbox Code Playgroud)
**Root Keyword:** 2017
**Phrase:** None
Run Code Online (Sandbox Code Playgroud)
**Root Keyword:** Joe Smith
**Phrase:** None
Run Code Online (Sandbox Code Playgroud)
**Root Keyword:** announced
**Phrase 1:** In CD, NNP announced he was
**Phrase 2:** CD, NNP announced he was diagnosed
**Phrase 3:** NNP announced he was diagnosed with
**Phrase 4:** announced he was diagnosed with Lyme
Run Code Online (Sandbox Code Playgroud)
**Root Keyword:** diagnosed
**Phrase 1:** CD, NNP announced he was diagnosed
**Phrase 2:** NNP announced he was diagnosed with
**Phrase 3:** announced he was diagnosed with Lyme
**Phrase 4:** he was diagnosed with Lyme disease
Run Code Online (Sandbox Code Playgroud)

生成短语的最后一个可能的词是:疾病

**Root Keyword:** disease
**Phrase 1:** he was diagnosed with Lyme disease
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经实现了以下代码:

public class Sentence {


    public Sentence()
    {

    }


    ArrayList<Word> wordList = new ArrayList<Word>();

    public void addWord(Word word)
    {
        wordList.add(word);
    }

    public ArrayList<Word> getWordList() {
        return wordList;
    }

}
Run Code Online (Sandbox Code Playgroud)
public class Word {

    public Word(String word, String pos) {

        this.word = word;
        this.pos = pos;
    }


    String word;
    String pos;
    ArrayList<String> phraseList = new ArrayList<String>();


    public String getWord() {
        return word;
    }

    public String getPos() {
        return pos;
    }


    public void setPhraseList(ArrayList<String> phraseList)
    {
        phraseList.addAll(phraseList);
    }

}
Run Code Online (Sandbox Code Playgroud)
public void generatePhrases()
{


    Sentence sentence = new Sentence();
    sentence.addWord(new Word("In", "IN"));
    sentence.addWord(new Word("2017", "CD"));
    sentence.addWord(new Word(",", "PUNCT"));
    sentence.addWord(new Word("Joe Smith", "NNP"));
    sentence.addWord(new Word("announced", "VB"));
    sentence.addWord(new Word("he", "PRP"));
    sentence.addWord(new Word("was", "VBD"));
    sentence.addWord(new Word("diagnosed", "VBN"));
    sentence.addWord(new Word("with", "IN"));
    sentence.addWord(new Word("Lyme", "NN"));
    sentence.addWord(new Word("disease", "NN"));
    sentence.addWord(new Word(".", "PUNCT"));


    ArrayList<String> posListNotUsedAsRootKeyword = new ArrayList<String>();
    posListNotUsedAsRootKeyword.add("NNP");
    posListNotUsedAsRootKeyword.add("CD");


    ArrayList<String> posListNotCountedTowardMin = new ArrayList<String>();
    posListNotCountedTowardMin.add("VBD");
    posListNotCountedTowardMin.add("IN");
    posListNotCountedTowardMin.add("PRP");
    posListNotCountedTowardMin.add("TO");

    int minPhraseLength = 4; 
    int maxPhraseLength = 6;


    for (int wordCounter = 0; wordCounter < sentence.getWordList().size(); wordCounter++) {

        ArrayList<String> phraseList = new ArrayList<String>();


        Word word = sentence.getWordList().get(wordCounter);
        String wordAsStr = word.getWord();
        String pos = word.getPos();

        if (posListNotUsedAsRootKeyword.contains(pos) || posListNotCountedTowardMin.contains(pos)) {
            continue;
        }


        boolean phraseDesiredLength = false;

        String phrase = wordAsStr;
        int phraseCounter = wordCounter + 1;
        while (!phraseDesiredLength && phraseCounter < sentence.getWordList().size()) {

            Word phraseWord = sentence.getWordList().get(phraseCounter);
            String phraseWordAsStr = phraseWord.getWord();
            String phrasePOS = phraseWord.getPos();


            String appendPhrase = (posListNotUsedAsRootKeyword.contains(phrasePOS)) ? phrasePOS : phraseWordAsStr;
            phrase += " " + appendPhrase;

            if (StringX.countNumberOfWordsInStr(phrase) == minPhraseLength || StringX.countNumberOfWordsInStr(phrase) == maxPhraseLength) {

                phraseDesiredLength = true;
            }


            phraseCounter++;
        }


        System.out.println("PHRASE: " + phrase);

        phraseList.add(phrase);

    }

}
Run Code Online (Sandbox Code Playgroud)

我主要在生成在根关键字之前开始并在根关键字之后结束的短语(递归?)以及验证短语长度 == 最小或最大短语长度方面遇到困难。