基本上在我的文字中我只想保留名词并删除其他部分的词性.
我认为没有任何自动化的方法.如果有请建议.
如果没有自动化的方法,我也可以手动完成,但为此我需要所有可能的说法,动词或介词或连词或形容词等的列表.有人可以建议一个可能的来源,我可以得到这些特定的列表.
我对python很新.我无法弄清楚这个bug.我想用NLTK提取名词.
我写了以下代码:
import nltk
sentence = "At eight o'clock on Thursday film morning word line test best beautiful Ram Aaron design"
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
length = len(tagged) - 1
a = list()
for i in (0,length):
log = (tagged[i][1][0] == 'N')
if log == True:
a.append(tagged[i][0])
Run Code Online (Sandbox Code Playgroud)
当我运行它时,'a'只有一个元素
a
['detail']
Run Code Online (Sandbox Code Playgroud)
我不懂为什么?
当我在没有for循环的情况下执行它时,它正在运行
log = (tagged[i][1][0] == 'N')
if log == True:
a.append(tagged[i][0])
Run Code Online (Sandbox Code Playgroud)
通过手动将"i"的值从0更改为"length",我得到了完美的输出,但是对于for循环,它只返回end元素.有人能告诉我for循环发生了什么问题.
代码后'a'应如下所示
['Thursday', 'film', 'morning', 'word', 'line', 'test', 'Ram' 'Aaron', 'design']
Run Code Online (Sandbox Code Playgroud) 我有这个字符串.
temp <- "this is Mapof ttMapof qwqqwMApofRt it"
Run Code Online (Sandbox Code Playgroud)
我必须把它作为输出.
"this is Mapof (Mapof) ttMapof (Mapof) qwqqwMapofRt it"
Run Code Online (Sandbox Code Playgroud)
我这样做:(完美没问题!)
temp <- gsub('Mapof\\b', 'Mapof (Mapof)', temp) #code line 1
"this is Mapof (Mapof) ttMapof (Mapof) qwqqwMapofRt it"
Run Code Online (Sandbox Code Playgroud)
但问题是我不能直接这样做,因为我必须从矢量中采取'模式'和'替换'.因此,在从该向量中提取"模式"和"替换"后,我将它们存储如下
inc_spelling <- "Mapof" #(pattern)
cor_spelling <- "Map of" #(replacement)
Run Code Online (Sandbox Code Playgroud)
现在我使用如下的paste()来获得确切的代码行1(上面),但它不会发生.你自己看.这里发生了什么问题?
txt <- paste0("temp <- gsub('",inc_spelling,"\\b','",inc_spelling," (",cor_spelling,")'"," ,temp)")
txt
"temp <- gsub('Mapof\\b','Mapof (Map of)' ,temp)"
eval(parse(text=txt))
temp
"this is Mapof ttMapof qewqeqwMapofdffd it"
Run Code Online (Sandbox Code Playgroud)
它失败!为什么会这样?我无法找出错误!如果这个任务无法通过paste()实现,请提出另一种选择.谢谢!
我有一个类似于以下可重现的样本数据的庞大数据集.
Interval value
1 2012-06-10 552
2 2012-06-11 4850
3 2012-06-12 4642
4 2012-06-13 4132
5 2012-06-14 4190
6 2012-06-15 4186
7 2012-06-16 1139
8 2012-06-17 490
9 2012-06-18 5156
10 2012-06-19 4430
11 2012-06-20 4447
12 2012-06-21 4256
13 2012-06-22 3856
14 2012-06-23 1163
15 2012-06-24 564
16 2012-06-25 4866
17 2012-06-26 4421
18 2012-06-27 4206
19 2012-06-28 4272
20 2012-06-29 3993
21 2012-06-30 1211
22 2012-07-01 698
23 2012-07-02 5770
24 2012-07-03 5103
25 2012-07-04 775
26 2012-07-05 …Run Code Online (Sandbox Code Playgroud) 例如,我有一个文本
a <- "This IS A SAMple sentence TMP"
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
"This A ple sentence"
Run Code Online (Sandbox Code Playgroud)
我该怎么做?一些更简单的方法吗?