r语言 - 执行三元语法文档时出现问题,彼此混淆



问题陈述

在计算三克的文本文档时

,彼此混合在一起。

我正在尝试获取数据帧行的每个单独值并仅对该文档应用三元组,但无法做到。

输入数据 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+ 条记录的数据集。

可以使用

monotonically_increasing_id() 添加具有唯一序列号的持久列,而不是执行字符串索引器,并重新创建数据帧,如下所示:

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")));

最新更新