R разделение текстов в пакете тм - распознавание говорящих - PullRequest
1 голос
/ 11 января 2012

Я пытаюсь определить наиболее часто употребляемые слова в выступлениях на конгрессе, и их приходится разделять по словам конгрессмена.Я только начинаю узнавать о R и пакете tm.У меня есть код, который может найти наиболее часто встречающиеся слова, но какой код я могу использовать для автоматической идентификации и сохранения говорящего?

Текст выглядит следующим образом:

OPENING STATEMENT OF SENATOR HERB KOHL, CHAIRMAN

    The Chairman. Good afternoon to everybody, and thank you 
very much for coming to this hearing this afternoon.
    In today's tough economic climate, millions of seniors have 
lost a big part of their retirement and investments in only a 
matter of months. Unlike younger Americans, they do not have 
time to wait for the markets to rebound in order to recoup a 
lifetime of savings.
[....]

   STATEMENT OF SENATOR MEL MARTINEZ, RANKING MEMBER
[....]

Я бы хотел получить эти имена или отдельный текст от людей.Надеюсь, ты сможешь мне помочь.Большое спасибо.

Ответы [ 2 ]

0 голосов
/ 22 марта 2014

Вот как я подхожу к этому, используя пример Бена (используйте qdap для анализа и создания кадра данных, а затем преобразовать в Corpus с 3 документами; обратите внимание, что qdap был разработан для таких данных стенограммы, какэто и Corpus может быть не лучшим форматом данных):

library(qdap)
dat <- unlist(strsplit(x, "\\n"))

locs <- grep("STATEMENT OF ", dat)
nms <- sapply(strsplit(dat[locs], "STATEMENT OF |,"), "[", 2)
dat[locs] <- "SPLIT_HERE"
corp <- with(data.frame(person=nms, dialogue = 
    Trim(unlist(strsplit(paste(dat[-1], collapse=" "), "SPLIT_HERE")))),
    df2tm_corpus(dialogue, person))

tm::inspect(corp)

## A corpus with 3 text documents
## 
## The metadata consists of 2 tag-value pairs and a data frame
## Available tags are:
##   create_date creator 
## Available variables in the data frame are:
##   MetaID 
## 
## $`SENATOR BIG APPLE KOHL`
## I am trying to identify the most frequently used words in the  congress speeches, and have to separate them by the congressperson.  I am just starting to learn about R and the tm package. I have a code  that can find the most frequent words, but what kind of a code can I   use to automatically identify and store the speaker of the speech
## 
## $`SENATOR HERB KOHL`
## The Chairman. Good afternoon to everybody, and thank you  very much for coming to this hearing this afternoon.     In today's tough economic climate, millions of seniors have  lost a big part of their retirement and investments in only a  matter of months. Unlike younger Americans, they do not have  time to wait for the markets to rebound in order to recoup a  lifetime of savings.
## 
## $`SENATOR LITTLE ORANGE`
## Would it be correct to say that you want  to split the file so you have one text object  per speaker? And then use a regular expression  to grab the speaker's name for each object? Then  you can write a function to collect word frequencies,  etc. on each object and put them in a table where the  row or column names are the speaker's names.
0 голосов
/ 11 января 2012

Правильно ли будет сказать, что вы хотите разделить файл, чтобы у вас был один текстовый объект на каждого оратора?А затем использовать регулярное выражение, чтобы получить имя говорящего для каждого объекта?Затем вы можете написать функцию для сбора частот слов и т. Д. Для каждого объекта и поместить их в таблицу, где имена строк или столбцов являются именами говорящего.

Если это так, вы можете сказать, что x - это ваш текст,затем используйте strsplit(x, "STATEMENT OF"), чтобы разделить слова STATEMENT OF, затем grep() или str_extract(), чтобы вернуть 2 или 3 слова после SENATOR (у них всегда есть только два имени, как в вашем примере?).

Узнайте больше об использовании этих функций и о работе с текстом в целом в R: http://en.wikibooks.org/wiki/R_Programming/Text_Processing

ОБНОВЛЕНИЕ Вот более полный ответ...

#create object containing all text
x <- c("OPENING STATEMENT OF SENATOR HERB KOHL, CHAIRMAN

    The Chairman. Good afternoon to everybody, and thank you 
very much for coming to this hearing this afternoon.
    In today's tough economic climate, millions of seniors have 
lost a big part of their retirement and investments in only a 
matter of months. Unlike younger Americans, they do not have 
time to wait for the markets to rebound in order to recoup a 
lifetime of savings.

STATEMENT OF SENATOR BIG APPLE KOHL, CHAIRMAN

I am trying to identify the most frequently used words in the 
congress speeches, and have to separate them by the congressperson. 
I am just starting to learn about R and the tm package. I have a code 
that can find the most frequent words, but what kind of a code can I  
use to automatically identify and store the speaker of the speech

STATEMENT OF SENATOR LITTLE ORANGE, CHAIRMAN

Would it be correct to say that you want 
to split the file so you have one text object 
per speaker? And then use a regular expression 
to grab the speaker's name for each object? Then 
you can write a function to collect word frequencies, 
etc. on each object and put them in a table where the 
row or column names are the speaker's names.")

# split object on first two words
y <- unlist(strsplit(x, "STATEMENT OF"))

#load library containing handy function
library(stringr)   

 # use word() to return words in positions 3 to 4 of each string, which is where the first and last names are
    z <- word(y[2:4], 3, 4) # note that the first line in the character vector y has only one word and this function gives and error if there are not enough words in the line
    z # have a look at the result...
    [1] "HERB KOHL,"     "BIG APPLE"      "LITTLE ORANGE,"

Без сомнения, мастер регулярных выражений может придумать что-нибудь, чтобы сделать это быстрее и удобнее!

В любом случае, отсюда вы можете запустить функцию для вычисления частоты словкаждая строка в векторе y (т. е. речь каждого говорящего), а затем создайте еще один объект, который объединяет результаты слова freq с именами для дальнейшего анализа.

...