Trying to remove special characters and non-english words from my data R
up vote
1
down vote
favorite
I am trying to clean up my data to remove; i.) special characters (e.g
+_), ii.) specific words (e.g retweet, followers, couldn, better, person) iii.) words that do not appear in the english dictionary I am using the quanteda library. My objective is to get the top 50 bigrams and plot them on a graph.
install.packages("textcat")
library(tm)
library(textcat)
the_data <- read.csv("twitterData.csv")
tweets_data <- the_data$x
tweets_corpus <- Corpus(VectorSource(tweets_data))
subSpace <- content_transformer(function(x, pattern) gsub(pattern,
" ", x))
twitterHandleRemover <- function(x) gsub("@\S+","", x)
shortWordRemover <- function(x) gsub('\b\w{1,5}\b','',x)
urlRemover <- function(x) gsub("http:[[:alnum:]]*","", x)
hashtagRemover <- function(x) gsub("#\S+","", x)
tweets_corpus <- tm_map(tweets_corpus, subSpace, "/")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "@")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "\|%&*#+_><")
tweets_corpus <- tm_map(tweets_corpus, content_transformer(tolower))
tweets_corpus <- tm_map(tweets_corpus, removeNumbers)
tweets_corpus <- tm_map(tweets_corpus, content_transformer(urlRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(shortWordRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(twitterHandleRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(hashtagRemover))
tweets_corp<- corpus(tweets_corpus)
tweets_dfm <- tokens(tweets_corp, remove_numbers = T,
remove_hyphens = T) %>%
tokens_remove("\p{P}", valuetype = "regex", padding=TRUE) %>%
tokens_remove(stopwords("english"), padding=TRUE) %>%
tokens_remove("\d+", padding = TRUE) %>%
tokens_ngrams(n=2) %>% dfm()
topfeatures(tweets_dfm,50)
This is output from my code:
Edit
I have tried using
specialChars <- function(x) gsub("[^[:alnum:]///']","", x)
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(specialChars))
to remove special characters, but that seems to remove all characters - output is numeric(0)
r text-mining tm quanteda
add a comment |
up vote
1
down vote
favorite
I am trying to clean up my data to remove; i.) special characters (e.g
+_), ii.) specific words (e.g retweet, followers, couldn, better, person) iii.) words that do not appear in the english dictionary I am using the quanteda library. My objective is to get the top 50 bigrams and plot them on a graph.
install.packages("textcat")
library(tm)
library(textcat)
the_data <- read.csv("twitterData.csv")
tweets_data <- the_data$x
tweets_corpus <- Corpus(VectorSource(tweets_data))
subSpace <- content_transformer(function(x, pattern) gsub(pattern,
" ", x))
twitterHandleRemover <- function(x) gsub("@\S+","", x)
shortWordRemover <- function(x) gsub('\b\w{1,5}\b','',x)
urlRemover <- function(x) gsub("http:[[:alnum:]]*","", x)
hashtagRemover <- function(x) gsub("#\S+","", x)
tweets_corpus <- tm_map(tweets_corpus, subSpace, "/")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "@")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "\|%&*#+_><")
tweets_corpus <- tm_map(tweets_corpus, content_transformer(tolower))
tweets_corpus <- tm_map(tweets_corpus, removeNumbers)
tweets_corpus <- tm_map(tweets_corpus, content_transformer(urlRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(shortWordRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(twitterHandleRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(hashtagRemover))
tweets_corp<- corpus(tweets_corpus)
tweets_dfm <- tokens(tweets_corp, remove_numbers = T,
remove_hyphens = T) %>%
tokens_remove("\p{P}", valuetype = "regex", padding=TRUE) %>%
tokens_remove(stopwords("english"), padding=TRUE) %>%
tokens_remove("\d+", padding = TRUE) %>%
tokens_ngrams(n=2) %>% dfm()
topfeatures(tweets_dfm,50)
This is output from my code:
Edit
I have tried using
specialChars <- function(x) gsub("[^[:alnum:]///']","", x)
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(specialChars))
to remove special characters, but that seems to remove all characters - output is numeric(0)
r text-mining tm quanteda
If there aren't that many special characters, maybe just start by removing each one and seeing what output looks like e.g.gsub("<|_|>|+", "", "<a_b>c+d*")
. Here the|
symbol is used as an OR operation
– Jonny Phelps
Nov 9 at 16:18
2
Try to include some reproducible data. And if you are using quanteda, why don't you code everything with quanteda? At least most of your code would run in parallel (default 2 cores).
– phiver
Nov 9 at 17:32
You need a reproducible example to get help with this, but in quanteda, see the argumentstokens(x, remove_punct = TRUE, remove_symbols = TRUE)
. Any tokens remaining that you wish to remove (e.g. “ii”) can then be removed usingtokens_remove()
.
– Ken Benoit
Nov 9 at 18:58
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to clean up my data to remove; i.) special characters (e.g
+_), ii.) specific words (e.g retweet, followers, couldn, better, person) iii.) words that do not appear in the english dictionary I am using the quanteda library. My objective is to get the top 50 bigrams and plot them on a graph.
install.packages("textcat")
library(tm)
library(textcat)
the_data <- read.csv("twitterData.csv")
tweets_data <- the_data$x
tweets_corpus <- Corpus(VectorSource(tweets_data))
subSpace <- content_transformer(function(x, pattern) gsub(pattern,
" ", x))
twitterHandleRemover <- function(x) gsub("@\S+","", x)
shortWordRemover <- function(x) gsub('\b\w{1,5}\b','',x)
urlRemover <- function(x) gsub("http:[[:alnum:]]*","", x)
hashtagRemover <- function(x) gsub("#\S+","", x)
tweets_corpus <- tm_map(tweets_corpus, subSpace, "/")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "@")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "\|%&*#+_><")
tweets_corpus <- tm_map(tweets_corpus, content_transformer(tolower))
tweets_corpus <- tm_map(tweets_corpus, removeNumbers)
tweets_corpus <- tm_map(tweets_corpus, content_transformer(urlRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(shortWordRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(twitterHandleRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(hashtagRemover))
tweets_corp<- corpus(tweets_corpus)
tweets_dfm <- tokens(tweets_corp, remove_numbers = T,
remove_hyphens = T) %>%
tokens_remove("\p{P}", valuetype = "regex", padding=TRUE) %>%
tokens_remove(stopwords("english"), padding=TRUE) %>%
tokens_remove("\d+", padding = TRUE) %>%
tokens_ngrams(n=2) %>% dfm()
topfeatures(tweets_dfm,50)
This is output from my code:
Edit
I have tried using
specialChars <- function(x) gsub("[^[:alnum:]///']","", x)
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(specialChars))
to remove special characters, but that seems to remove all characters - output is numeric(0)
r text-mining tm quanteda
I am trying to clean up my data to remove; i.) special characters (e.g
+_), ii.) specific words (e.g retweet, followers, couldn, better, person) iii.) words that do not appear in the english dictionary I am using the quanteda library. My objective is to get the top 50 bigrams and plot them on a graph.
install.packages("textcat")
library(tm)
library(textcat)
the_data <- read.csv("twitterData.csv")
tweets_data <- the_data$x
tweets_corpus <- Corpus(VectorSource(tweets_data))
subSpace <- content_transformer(function(x, pattern) gsub(pattern,
" ", x))
twitterHandleRemover <- function(x) gsub("@\S+","", x)
shortWordRemover <- function(x) gsub('\b\w{1,5}\b','',x)
urlRemover <- function(x) gsub("http:[[:alnum:]]*","", x)
hashtagRemover <- function(x) gsub("#\S+","", x)
tweets_corpus <- tm_map(tweets_corpus, subSpace, "/")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "@")
tweets_corpus <- tm_map(tweets_corpus, subSpace, "\|%&*#+_><")
tweets_corpus <- tm_map(tweets_corpus, content_transformer(tolower))
tweets_corpus <- tm_map(tweets_corpus, removeNumbers)
tweets_corpus <- tm_map(tweets_corpus, content_transformer(urlRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(shortWordRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(twitterHandleRemover))
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(hashtagRemover))
tweets_corp<- corpus(tweets_corpus)
tweets_dfm <- tokens(tweets_corp, remove_numbers = T,
remove_hyphens = T) %>%
tokens_remove("\p{P}", valuetype = "regex", padding=TRUE) %>%
tokens_remove(stopwords("english"), padding=TRUE) %>%
tokens_remove("\d+", padding = TRUE) %>%
tokens_ngrams(n=2) %>% dfm()
topfeatures(tweets_dfm,50)
This is output from my code:
Edit
I have tried using
specialChars <- function(x) gsub("[^[:alnum:]///']","", x)
tweets_corpus <- tm_map(tweets_corpus,
content_transformer(specialChars))
to remove special characters, but that seems to remove all characters - output is numeric(0)
r text-mining tm quanteda
r text-mining tm quanteda
edited Nov 9 at 15:54
emilliman5
3,91321429
3,91321429
asked Nov 9 at 15:46
Emm
257
257
If there aren't that many special characters, maybe just start by removing each one and seeing what output looks like e.g.gsub("<|_|>|+", "", "<a_b>c+d*")
. Here the|
symbol is used as an OR operation
– Jonny Phelps
Nov 9 at 16:18
2
Try to include some reproducible data. And if you are using quanteda, why don't you code everything with quanteda? At least most of your code would run in parallel (default 2 cores).
– phiver
Nov 9 at 17:32
You need a reproducible example to get help with this, but in quanteda, see the argumentstokens(x, remove_punct = TRUE, remove_symbols = TRUE)
. Any tokens remaining that you wish to remove (e.g. “ii”) can then be removed usingtokens_remove()
.
– Ken Benoit
Nov 9 at 18:58
add a comment |
If there aren't that many special characters, maybe just start by removing each one and seeing what output looks like e.g.gsub("<|_|>|+", "", "<a_b>c+d*")
. Here the|
symbol is used as an OR operation
– Jonny Phelps
Nov 9 at 16:18
2
Try to include some reproducible data. And if you are using quanteda, why don't you code everything with quanteda? At least most of your code would run in parallel (default 2 cores).
– phiver
Nov 9 at 17:32
You need a reproducible example to get help with this, but in quanteda, see the argumentstokens(x, remove_punct = TRUE, remove_symbols = TRUE)
. Any tokens remaining that you wish to remove (e.g. “ii”) can then be removed usingtokens_remove()
.
– Ken Benoit
Nov 9 at 18:58
If there aren't that many special characters, maybe just start by removing each one and seeing what output looks like e.g.
gsub("<|_|>|+", "", "<a_b>c+d*")
. Here the |
symbol is used as an OR operation– Jonny Phelps
Nov 9 at 16:18
If there aren't that many special characters, maybe just start by removing each one and seeing what output looks like e.g.
gsub("<|_|>|+", "", "<a_b>c+d*")
. Here the |
symbol is used as an OR operation– Jonny Phelps
Nov 9 at 16:18
2
2
Try to include some reproducible data. And if you are using quanteda, why don't you code everything with quanteda? At least most of your code would run in parallel (default 2 cores).
– phiver
Nov 9 at 17:32
Try to include some reproducible data. And if you are using quanteda, why don't you code everything with quanteda? At least most of your code would run in parallel (default 2 cores).
– phiver
Nov 9 at 17:32
You need a reproducible example to get help with this, but in quanteda, see the arguments
tokens(x, remove_punct = TRUE, remove_symbols = TRUE)
. Any tokens remaining that you wish to remove (e.g. “ii”) can then be removed using tokens_remove()
.– Ken Benoit
Nov 9 at 18:58
You need a reproducible example to get help with this, but in quanteda, see the arguments
tokens(x, remove_punct = TRUE, remove_symbols = TRUE)
. Any tokens remaining that you wish to remove (e.g. “ii”) can then be removed using tokens_remove()
.– Ken Benoit
Nov 9 at 18:58
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Why not just do something like this:
> x <- "je n'aime pas ça"
> Encoding(x)
[1] "latin1"
> iconv(x, from = "latin1", to = "ASCII//TRANSLIT")
[1] "je n'aime pas ca"
So do iconv(tweets_data, from = "latin1", to = "ASCII//TRANSLIT")
assuming your data is in latin1
And next keep only alphanumeric characters or spaces
gsub(pattern = "[^[:alnum:][:space:]]", " ", "<friends @symbols")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228954%2ftrying-to-remove-special-characters-and-non-english-words-from-my-data-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Why not just do something like this:
> x <- "je n'aime pas ça"
> Encoding(x)
[1] "latin1"
> iconv(x, from = "latin1", to = "ASCII//TRANSLIT")
[1] "je n'aime pas ca"
So do iconv(tweets_data, from = "latin1", to = "ASCII//TRANSLIT")
assuming your data is in latin1
And next keep only alphanumeric characters or spaces
gsub(pattern = "[^[:alnum:][:space:]]", " ", "<friends @symbols")
add a comment |
up vote
0
down vote
Why not just do something like this:
> x <- "je n'aime pas ça"
> Encoding(x)
[1] "latin1"
> iconv(x, from = "latin1", to = "ASCII//TRANSLIT")
[1] "je n'aime pas ca"
So do iconv(tweets_data, from = "latin1", to = "ASCII//TRANSLIT")
assuming your data is in latin1
And next keep only alphanumeric characters or spaces
gsub(pattern = "[^[:alnum:][:space:]]", " ", "<friends @symbols")
add a comment |
up vote
0
down vote
up vote
0
down vote
Why not just do something like this:
> x <- "je n'aime pas ça"
> Encoding(x)
[1] "latin1"
> iconv(x, from = "latin1", to = "ASCII//TRANSLIT")
[1] "je n'aime pas ca"
So do iconv(tweets_data, from = "latin1", to = "ASCII//TRANSLIT")
assuming your data is in latin1
And next keep only alphanumeric characters or spaces
gsub(pattern = "[^[:alnum:][:space:]]", " ", "<friends @symbols")
Why not just do something like this:
> x <- "je n'aime pas ça"
> Encoding(x)
[1] "latin1"
> iconv(x, from = "latin1", to = "ASCII//TRANSLIT")
[1] "je n'aime pas ca"
So do iconv(tweets_data, from = "latin1", to = "ASCII//TRANSLIT")
assuming your data is in latin1
And next keep only alphanumeric characters or spaces
gsub(pattern = "[^[:alnum:][:space:]]", " ", "<friends @symbols")
edited Nov 15 at 16:45
answered Nov 15 at 16:28
jwijffels
3,88721327
3,88721327
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228954%2ftrying-to-remove-special-characters-and-non-english-words-from-my-data-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If there aren't that many special characters, maybe just start by removing each one and seeing what output looks like e.g.
gsub("<|_|>|+", "", "<a_b>c+d*")
. Here the|
symbol is used as an OR operation– Jonny Phelps
Nov 9 at 16:18
2
Try to include some reproducible data. And if you are using quanteda, why don't you code everything with quanteda? At least most of your code would run in parallel (default 2 cores).
– phiver
Nov 9 at 17:32
You need a reproducible example to get help with this, but in quanteda, see the arguments
tokens(x, remove_punct = TRUE, remove_symbols = TRUE)
. Any tokens remaining that you wish to remove (e.g. “ii”) can then be removed usingtokens_remove()
.– Ken Benoit
Nov 9 at 18:58