做Tri-gram文档时的问题是相互混淆的

时间:2017-05-17 07:49:22

标签: r for-loop n-gram

问题陈述

计算三克文本文档时彼此混淆。

我正在尝试获取dataframe行的每个值,并仅为该文档应用trigram,但无法执行。

输入数据1:

subject_lines
Need to make 'Address1' field mandatory in 'Leads'
No display of record in "All Opportunities" Saved Search list
"Organization" in Activity resources display name of a user

下面是我的代码,用于计算三元组及其在文档中的频率

#Text Analysis for Oracle Fusion Sales Cloud Service data 
library(lubridate)
library(tm)
library(SnowballC)
library(RColorBrewer)
library(wordcloud)
library(RWeka)
library(gsubfn)
library(sqldf)

#import the data in csv format
EmailSubject_Dataset <- read.csv("D:/TextAnalysisPOC_MasterPet/OracleFusionSalesCloudService_SampleDataset.csv", header=TRUE, sep=",")
#corpus creation
documents <- Corpus(VectorSource(EmailSubject_Dataset$Subject.lines))

# Data preprocessing 
documents <- tm_map(documents, content_transformer(tolower))
documents <- tm_map(documents, removePunctuation)
documents <- tm_map(documents, stripWhitespace)
stopwordList <- c('i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor','only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't")
documents <- tm_map(documents, removeWords, stopwordList)
reservedM<-c("if","for","function","else","repeat","next","TRUE","from")
documents <- tm_map(documents, removeWords, reservedM)
unwantedwordlist <- c("prod caej","add","iuf","caej","iwf","iwf iuf","make","xxx","iwfcrmrupgrade","ccbo","ceo","ie","ruf","csf","osc","sr","moo","etc","see","jbojbo","lov","isn","hcm","oim","th","jbo","sha","adf","sui","re","oim","outlook","r","rel","via","abhishek")
documents <- tm_map(documents, removeWords, unwantedwordlist)
#documents <- tm_map(documents, removeWords, stopwords("english"))
#documents = gsub('[[:punct:]]', '', documents)
documents <- tm_map(documents, stripWhitespace)
#minfreq_bigram<-2

#Bi-gram implemetation
token_delim <- " \\t\\r\\n.!?,;\"()"
tritoken <- NGramTokenizer(documents, Weka_control(min=3,max=3, delimiters = token_delim))
three_word <- data.frame(table(tritoken))

# Bi-gram frequency
sort_threee <- three_word[order(three_word$Freq),]
head(sort_threee)
sort_three <- as.data.frame(sort_threee)
TrigramData <- sqldf("select tritoken,Freq from sort_threee");
Most_Frequent <- subset(TrigramData,TrigramData$Freq>=2)
write.csv(TrigramData, file = "D:\\TextAnalysisPOC_MasterPet\\Analysis.csv")

实际输出:

    +---------------------------+-------------+
    |          Terms            |  Frequency  |        
    +---------------------------+-------------+
    |need address1 filed        |     1       |
    |mandatory leads display    |     1       |
    |record opportunities saved |     1       |
    |search list organization   |     1       |
    |activity resources display |     1       |
    +---------------------------+-------------+

预期产出:

   +-------------------------------+-------------+
    |          Terms               |  Frequency  |        
    +------------------------------+-------------+
    |need address1 field           |     1       |
    |address1 field mandatory      |     1       |
    |field mandatory leads         |     1       |
    |display record opportunities  |     1       |
    |record opportunities saved    |     1       |
    |opportunities saved search    |     1       |
    |saved search list             |     1       |
    |...................           |  ......     |
    +------------------------------+-------------+

请帮我实现这个预期的输出。 我的数据集大约有500多条记录。

1 个答案:

答案 0 :(得分:4)

您可以使用monotonically_increasing_id()添加具有唯一序列号的持久列,而不是使用字符串Indexer,并重新创建DataFrame,如下所示:

Dataset<Row> Test2=Dataset2.withColumn("rowId2", monotonically_increasing_id()) ;
Dataset<Row> Test1=Dataset1.withColumn("rowId1", monotonically_increasing_id()) ;

然后加入两个数据集:

Dataset<Row> Finalresult = Test1.join(Test2 , Test1.col("rowId1").equalTo(Test2.col("rowId2")));