Pergunta

Estou tentando criar um resumidor em Java.estou usando o Marcador de parte de fala log-linear de Stanford para marcar as palavras, e então, para determinadas tags, estou pontuando a frase e finalmente no resumo, estou imprimindo frases com valor de pontuação alto.Aqui está o código:

    MaxentTagger tagger = new MaxentTagger("taggers/bidirectional-distsim-wsj-0-18.tagger");

    BufferedReader reader = new BufferedReader( new FileReader ("C:\\Summarizer\\src\\summarizer\\testing\\testingtext.txt"));
    String line  = null;
    int score = 0;
    StringBuilder stringBuilder = new StringBuilder();
    File tempFile = new File("C:\\Summarizer\\src\\summarizer\\testing\\tempFile.txt");
    Writer writerForTempFile = new BufferedWriter(new FileWriter(tempFile));


    String ls = System.getProperty("line.separator");
    while( ( line = reader.readLine() ) != null )
    {
        stringBuilder.append( line );
        stringBuilder.append( ls );
        String tagged = tagger.tagString(line);
        Pattern pattern = Pattern.compile("[.?!]"); //Find new line
        Matcher matcher = pattern.matcher(tagged);
        while(matcher.find())
        {
            Pattern tagFinder = Pattern.compile("/JJ"); // find adjective tag
            Matcher tagMatcher = tagFinder.matcher(matcher.group());
            while(tagMatcher.find())
            {
                score++; // increase score of sentence for every occurence of adjective tag
            }
            if(score > 1)
                writerForTempFile.write(stringBuilder.toString());
            score = 0;
            stringBuilder.setLength(0);
        }

    }

    reader.close();
    writerForTempFile.close();

O código acima não está funcionando.Embora, se eu cortar meu trabalho e gerar pontuação para cada linha (não frase), isso funcionará.Mas os resumos não são gerados dessa forma, não é?Aqui está o código para isso:(todas as declarações sendo iguais às anteriores)

while( ( line = reader.readLine() ) != null )
        {
            stringBuilder.append( line );
            stringBuilder.append( ls );
            String tagged = tagger.tagString(line);
            Pattern tagFinder = Pattern.compile("/JJ"); // find adjective tag
            Matcher tagMatcher = tagFinder.matcher(tagged);
            while(tagMatcher.find())
            {
                score++;  //increase score of line for every occurence of adjective tag
            }
            if(score > 1)
                writerForTempFile.write(stringBuilder.toString());
            score = 0;
            stringBuilder.setLength(0);
        }

EDITAR 1:

Informações sobre o que o MaxentTagger faz.Um exemplo de código para mostrar que está funcionando:

import java.io.IOException;

import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class TagText {
    public static void main(String[] args) throws IOException,
            ClassNotFoundException {

        // Initialize the tagger
        MaxentTagger tagger = new MaxentTagger(
                "taggers/bidirectional-distsim-wsj-0-18.tagger");

        // The sample string
        String sample = "This is a sample text";

        // The tagged string
        String tagged = tagger.tagString(sample);

        // Output the result
        System.out.println(tagged);
    }
}

Saída:

This/DT is/VBZ a/DT sample/NN sentence/NN

EDITAR 2:

Código modificado usando BreakIterator para encontrar quebras de frase.No entanto, o problema persiste.

while( ( line = reader.readLine() ) != null )
        {
            stringBuilder.append( line );
            stringBuilder.append( ls );
            String tagged = tagger.tagString(line);
            BreakIterator bi = BreakIterator.getSentenceInstance();
            bi.setText(tagged);
            int end, start = bi.first();
            while ((end = bi.next()) != BreakIterator.DONE)
            {
                String sentence = tagged.substring(start, end);
                Pattern tagFinder = Pattern.compile("/JJ");
                Matcher tagMatcher = tagFinder.matcher(sentence);
                while(tagMatcher.find())
                {
                    score++;
                }
                scoreTracker.add(score);
                if(score > 1)
                    writerForTempFile.write(stringBuilder.toString());
                score = 0;
                stringBuilder.setLength(0);
                start = end;
            }
Foi útil?

Solução

Encontrar quebras de frase pode ser um pouco mais complicado do que apenas procurar por [.?!], considere usar BreakIterador.getSentenceInstance()

Seu desempenho é bastante semelhante ao da implementação (mais complexa) do LingPipe e melhor que o do OpenNLP (pelo menos em meus próprios testes).

Código de amostra

BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(text);
int end, start = bi.first();
while ((end = bi.next()) != BreakIterator.DONE) {
    String sentence = text.substring(start, end);
    start = end;
}

Editar

Acho que é isso que você está procurando:

    Pattern tagFinder = Pattern.compile("/JJ");
    BufferedReader reader = getMyReader();
    String line = null;
    while ((line = reader.readLine()) != null) {
        BreakIterator bi = BreakIterator.getSentenceInstance();
        bi.setText(line);
        int end, start = bi.first();
        while ((end = bi.next()) != BreakIterator.DONE) {
            String sentence = line.substring(start, end);
            String tagged = tagger.tagString(sentence);
            int score = 0;
            Matcher tag = tagFinder.matcher(tagged);
            while (tag.find())
                score++;
            if (score > 1)
                writerForTempFile.println(sentence);
            start = end;
        }
    }

Outras dicas

Sem entender tudo, meu palpite seria que seu código deveria ser mais assim:

    int lastMatch = 0;// Added

    Pattern pattern = Pattern.compile("[.?!]"); //Find new line
    Matcher matcher = pattern.matcher(tagged);
    while(matcher.find())
    {
        Pattern tagFinder = Pattern.compile("/JJ"); // find adjective tag

        // HERE START OF MY CHANGE
        String sentence = tagged.substring(lastMatch, matcher.end());
        lastMatch = matcher.end();
        Matcher tagMatcher = tagFinder.matcher(sentence);
        // HERE END OF MY CHANGE

        while(tagMatcher.find())
        {
            score++; // increase score of sentence for every occurence of adjective tag
        }
        if(score > 1)
            writerForTempFile.write(sentence);
        score = 0;
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top