library(data.table);library(tidyverse);library(qdapRegex);library(tidytext)

การ Deal กับ Character (String)

Fake_text = 'กระเพราจานนี้โคตร Amazing ลองตามไปตำดู 
  อร่อยเกิ้นนนนน !!!&$^%!^%#@&$ https://stackoverflow.com/questions/25166624/insert-picture-table-in-r-markdown 
ต้องไแลอง แล้วล่ะ 12345 92 a am an the no was were are you 0202'
Fake_text
## [1] "กระเพราจานนี้โคตร Amazing ลองตามไปตำดู \n  อร่อยเกิ้นนนนน !!!&$^%!^%#@&$ https://stackoverflow.com/questions/25166624/insert-picture-table-in-r-markdown \nต้องไแลอง แล้วล่ะ 12345 92 a am an the no was were are you 0202"

delete number

gsub('[0-9]+',"",Fake_text)
## [1] "กระเพราจานนี้โคตร Amazing ลองตามไปตำดู \n  อร่อยเกิ้นนนนน !!!&$^%!^%#@&$ https://stackoverflow.com/questions//insert-picture-table-in-r-markdown \nต้องไแลอง แล้วล่ะ   a am an the no was were are you "

delete

str_squish(Fake_text)
## [1] "กระเพราจานนี้โคตร Amazing ลองตามไปตำดู อร่อยเกิ้นนนนน !!!&$^%!^%#@&$ https://stackoverflow.com/questions/25166624/insert-picture-table-in-r-markdown ต้องไแลอง แล้วล่ะ 12345 92 a am an the no was were are you 0202"

delete Web pattern

gsub('https://.*','',Fake_text)
## [1] "กระเพราจานนี้โคตร Amazing ลองตามไปตำดู \n  อร่อยเกิ้นนนนน !!!&$^%!^%#@&$ "

หายหมด อาจต้องหา pattern ให้ดีหรือใช้ Library ช่วย

rm_url(Fake_text)
## [1] "กระเพราจานนี้โคตร Amazing ลองตามไปตำดู อร่อยเกิ้นนนนน !!!&$^%!^%#@&$ ต้องไแลอง แล้วล่ะ 12345 92 a am an the no was were are you 0202"

delete Puntuation พวก +@#$%! บลาๆ

gsub('[[:punct:]]+',"",Fake_text)
## [1] "ระเราาี้โร Amazing ลอามไำู \n  อร่อยเิ้  httpsstackoverflowcomquestions25166624insertpicturetableinrmarkdown \n้อไแลอ แล้วล่ะ 12345 92 a am an the no was were are you 0202"

delete StopWords (a an the คำพวกโผล่ซ้ำบ่อยเกินความจำเป็น)

Fake_text_tokens <- Fake_text %>% data.table('text'=.) %>% unnest_tokens(input = text ,
                                                               output = word)
Fake_text_tokens %>% anti_join(stop_words,by = 'word')
## Remove Twitter Short URL
x <- c("download file from http://example.com", 
         "this is the link to my website http://example.com", 
         "go to http://example.com from more info.",
         "Another url ftp://www.example.com",
         "And https://www.example.net",
         "twitter type: t.co/N1kq0F26tG",
         "still another one https://t.co/N1kq0F26tG :-)")
print(1)
## [1] 1
rm_url(x)
## [1] "download file from"             "this is the link to my website"
## [3] "go to from more info."          "Another url"                   
## [5] "And"                            "twitter type: t.co/N1kq0F26tG" 
## [7] "still another one :-)"
print(2)
## [1] 2
rm_twitter_url(x)
## [1] "download file from http://example.com"            
## [2] "this is the link to my website http://example.com"
## [3] "go to http://example.com from more info."         
## [4] "Another url ftp://www.example.com"                
## [5] "And https://www.example.net"                      
## [6] "twitter type:"                                    
## [7] "still another one :-)"
print(3)
## [1] 3
x %>% rm_twitter_url() %>% rm_url()
## [1] "download file from"             "this is the link to my website"
## [3] "go to from more info."          "Another url"                   
## [5] "And"                            "twitter type:"                 
## [7] "still another one :-)"