2012-01-11 19 views
1

私は、議会のスピーチで最も頻繁に使用される言葉を特定しようとしており、議員によってそれらを分けなければなりません。私はRとtmパッケージについて学び始めたばかりです。私は最も頻繁な単語を見つけることができるコードを持っていますが、スピーチのスピーカーを自動的に特定して保存するためにどのようなコードを使用できますか?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 
[....] 

私は人々によってこれらの名前、または別々のテキストを取得できるようにしたいと思います。あなたが私を助けてくれることを願います。どうもありがとう。

答えて

0

スピーカーごとに1つのテキストオブジェクトを持つようにファイルを分割したいと言うのは正しいでしょうか?そして、正規表現を使って、各オブジェクトの話者の名前をつかんでください。次に、各オブジェクトに単語の頻度などを収集し、行または列の名前が話者の名前である表にそれらを入れる関数を書くことができます。

もしそうなら、あなたは上院議員は、(彼らはいつものように2つだけの名前を持って行った後、次に2つのまたは3つの言葉を返すために、その後grep()またはstr_extract()、の言葉の声明に分割するstrsplit(x, "STATEMENT OF")を使用し、xがテキストであると言うかもしれない、あなたの例?)。

は、これらの関数の使用方法の詳細についてはこちらを見てみると、一般的なテキスト操作 R中: http://en.wikibooks.org/wiki/R_Programming/Text_Processing

UPDATEここでは、より完全な答えだ...

#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を計算し、freqの結果と後の解析のための名前を組み合わせた別のオブジェクトを作成する関数を実行できます。

+1

ありがとう - これはうまくいくと思います。 – appletree

+0

@appletree、私は少し私の答えを拡大しました、私は助けてくれることを願っています。私はショットを正規表現の解決策を持っていたが、動作させることができなかった。おそらく、誰かが私たちにそれがどのように行われたかを示すでしょう... – Ben

0

これは私が解析し、作成データフレームを、その後3つの文書でCorpusに変換する(ベンの例を使用しての使用qdapをそれにアプローチしたい方法です。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.