我正在使用R来构建情感分析工具,我遇到了一些重复问题.数据的主要来源是Twitter,看起来许多人通过在每条推文的末尾添加一些随机文本来绕过Twitter自己的垃圾邮件过滤器.例如
Click xxxxx to buy the amazing xxxxx for FREE ugjh
我在最后得到了大量带有不同随机字符串的精确推文.它们来自同一用户或来自不同用户.
是否有任何函数duplicated或者unique哪些函数返回2个字符串的接近程度,如果它们高于某个%,则忽略它们?
我知道这样做最终会删除那些说完全相同的人的真实推文,比如说
I love xxxx !
但是我将来会处理这个问题.
任何正确方向的提示将非常感谢!
我正在尝试构建一个矩阵,其中第一行是词性,第一列是句子。矩阵中的值应显示句子中此类 POS 的数量。
所以我以这种方式创建 POS 标签:
data = pd.read_csv(open('myfile.csv'),sep=';') 
target = data["label"]
del data["label"]
data.sentence = data.sentence.str.lower() # All strings in data frame to lowercase
for line in data.sentence:
    Line_new= nltk.pos_tag(nltk.word_tokenize(line))
    print(Line_new)
输出是:
[('together', 'RB'), ('with', 'IN'), ('the', 'DT'), ('6th', 'CD'), ('battalion', 'NN'), ('of', 'IN'), ('the', 'DT')]
如何从这样的输出创建我在上面描述的矩阵?
更新:所需的输出是
                   NN  VB    IN    VBZ    DT
 I was there       1   1     1      0     0
 He came there     0   0     1      1     1
我的文件.csv:
"A child who is exclusively or predominantly oral (using …我曾经quanteda::textmodel_NB创建一个模型,将文本分为两类之一。我将模型拟合到去年夏天的训练数据集上。
现在,我试图在今年夏天使用它来对我们在工作中获得的新文本进行分类。我尝试这样做并收到以下错误:
Error in predict.textmodel_NB_fitted(model, test_dfm) : 
feature set in newdata different from that in training set
可以在此处的第 157 至 165 行找到生成错误的函数中的代码。
我认为这是因为训练数据集中的单词与测试数据集中使用的单词不完全匹配。但是为什么会出现这个错误呢?我觉得好像——在现实世界的例子中有用——模型应该能够处理包含不同特征的数据集,因为这在应用中很可能总是发生。
所以我的第一个问题是:
1. 这个错误是朴素贝叶斯算法的一个属性吗?还是函数的作者选择这样做?
这让我想到了我的第二个问题:
2. 我该如何解决这个问题?
为了解决第二个问题,我提供了可重现的代码(最后一行生成了上面的错误):
library(quanteda)
library(magrittr)
library(data.table)
train_text <- c("Can random effects apply only to categorical variables?",
                "ANOVA expectation identity",
                "Statistical test for significance in ranking positions",
                "Is Fisher Sharp Null Hypothesis testable?",
                "List major reasons for different results from survival analysis among different studies",
                "How do the …我一直在使用 JJ Allaire 的指南,在神经网络模型中使用词嵌入进行文本处理(https://jjallaire.github.io/deep-learning-with-r-notebooks/notebooks/6.1-using-word-embeddings.nb .html)。我对模型如何将标记化的单词序列 (x_train) 与使用整个数据集(而不仅仅是训练数据)定义的词嵌入相关联感到困惑。有没有办法概念化单词标记如何映射到单词嵌入?否则,像“king”这样的词如何映射到词嵌入(例如使用 Glove 获得)。我说的是这些代码块之间的关系:
#building model 
history <- model %>% fit(
 x_train, y_train,
 epochs = 20,
 batch_size = 32,
 validation_data = list(x_val, y_val)
)
#relating model to word embeddings
model <- keras_model_sequential() %>% 
layer_embedding(input_dim = max_words, output_dim = embedding_dim, 
              input_length = maxlen) %>% 
layer_flatten() %>% 
layer_dense(units = 32, activation = "relu") %>% 
layer_dense(units = 1, activation = "sigmoid")
get_layer(model, index = 1) %>% 
 set_weights(list(embedding_matrix)) %>% 
 freeze_weights()
来自 x_train 的标记化单词如何链接回 embedding_matrix 中的单词(特别是如果嵌入层是针对所有数据进行训练的)?
我的问题是能够计算c中字符串中单引号或双引号的数量。例子
        String            Single Quote Count        Double Quote Count
     'hello world'                2                      0
     'hell'o world'               3                      0
     "hello world"                0                      2
     "hello" world"               0                      3
用户输入字符串,我通过 gets() 函数获取,然后我需要这个计数器来进一步分析该字符串。
例如,当我必须计算字符串中的“|”时,会更容易
        String            | Count        
     hello | world           1            
     hello | wo|rld          2            
所以我的功能很简单:
 int getNumPipe(char* cmd){
  int  num = 0;
  int i;
     for(i=0;i<strlen(cmd);i++){
       if(cmd[i]=='|'){ //if(condition)
       num++;
      }
     }
 return num;
}
但现在我必须分析报价,我不知道该为 if(condition) 添加什么
          if(cmd[i]==''')??
我有一个包含超过5000个文本文件的语料库.我想在每个文件运行预处理之后获得每个文件的单个字数(转向更低,删除停用词等).我对单个文本文件的单词计数没有任何好运.任何帮助,将不胜感激.
library(tm)
revs<-Corpus(DirSource("data/")) 
revs<-tm_map(revs,tolower) 
revs<-tm_map(revs,removeWords, stopwords("english")) 
revs<-tm_map(revs,removePunctuation) 
revs<-tm_map(revs,removeNumbers) 
revs<-tm_map(revs,stripWhitespace) 
dtm<-DocumentTermMatrix(revs) 
我已将 CSV 文件导入到 R 中的数据框中,其中一列包含文本。
我想对文本进行分析。我该怎么做?
我尝试制作一个仅包含文本列的新数据框。
OnlyTXT= Txtanalytics1 %>%
  select(problem_note_text)
View(OnlyTXT). 
下面是我的数据框的样子,您会看到我的数据框列之一是 URL,其他列是时间戳计数。当我运行此代码时:busiest_hosts[busiest_hosts['host'].str.contains('***.novo.dk')==True]我收到错误:error: nothing to repeat at position 0。我认为这是因为我的 URL 的第一个元素是*. 这看起来像是一个 python bug(我的 python 版本是 3.x)。如果有人能帮助我解决这个问题,我将非常感激。
注意:这个问题涵盖了为什么脚本这么慢。但是,如果您更喜欢改进某些东西,可以查看我在 CodeReview 上的帖子,该帖子旨在提高性能。
我正在处理一个处理纯文本文件 (.lst) 的项目。
文件名 ( fileName) 的名称很重要,因为我将从它们中提取node(例如abessijn)和component(例如 WR-PEA)到一个数据帧中。例子:
abessijn.WR-P-E-A.lst
A-bom.WR-P-E-A.lst
acroniem.WR-P-E-C.lst
acroniem.WR-P-E-G.lst
adapter.WR-P-E-A.lst
adapter.WR-P-E-C.lst
adapter.WR-P-E-G.lst
每个文件由一行或多行组成。每行由一个句子(在<sentence>标签内)组成。示例(abessijn.WR-PEA.lst)
/home/nobackup/SONAR/COMPACT/WR-P-E-A/WR-P-E-A0000364.data.ids.xml:  <sentence>Vooral mijn abessijn ruikt heerlijk kruidig .. : ) )</sentence>
/home/nobackup/SONAR/COMPACT/WR-P-E-A/WR-P-E-A0000364.data.ids.xml:  <sentence>Mijn abessijn denkt daar heel anders over .. : ) ) Maar mijn kinderen richt ik ook niet af , zit niet in mijn bloed .</sentence>
我从每一行中提取句子,对其进行一些小的修改,然后将其命名为sentence. 接下来是一个名为 的元素leftContext,它采用node(例如 …
我有一个市场矩阵文件,我必须用它来进行文本分析。
市场文件具有以下结构:
%%MatrixMarket matrix coordinate integer general
2000 5000 23000
1 4300 1
1 2200 1
1 3000 1
1 600  1
第二行中的值表示矩阵中的行数、列数和非零值的总数。此后的所有行都包含 3 个值:
正如在许多帖子中阅读的那样,我阅读了这个文件,使用 scipy.io.mmread 和处理解析数据结构的新 API。
特别是,我使用了以下代码:
    Matrix = (mmread('file_name.mtx'))
    B = Matrix.todense()
    df = pd.DataFrame(B)
    print(df.head())
但是,从这段代码中,我得到了一个从 0 索引的数据框:
        0     1     2     3     4     5     6     7     8     9     ...   4872  \
0     1     0     1     0     0     0     0     0     1     0  ...      0   
1     0     0     0     0 …text-analysis ×10
r ×7
python ×4
dataframe ×2
nlp ×2
pandas ×2
algorithm ×1
bigdata ×1
c ×1
corpus ×1
data-science ×1
keras ×1
naivebayes ×1
nltk ×1
python-3.x ×1
quanteda ×1
regex ×1
tm ×1
word-count ×1