我尝试包 dateutil 从字符串中提取日期部分。如果字符串中包含确切的日期,则效果很好,例如:
from dateutil.parser import parse
try:
date = parse(string, fuzzy=True)
print(str(date)[:10])
except ValueError:
print("no date in text")
string = "an example of date:8 march 2019"
output: 2019-03-08
string = "an example of date: 2019/3/8"
output: 2019-03-08
string = "an example of pure string"
output: no date in text
Run Code Online (Sandbox Code Playgroud)
但是,当字符串中包含数字而不是日期时,就会出错并将其视为日期:
string = "an example of wrong date: 8"
output: 2022-03-08
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何使用这个包或类似的包来解决这个问题。有一些与提取日期相关的帖子,例如Extract date from string in python,但它们没有涵盖这个主题,并且它们适用于特定的日期格式。
非常感谢您的帮助!
假设我有一个文档集合,例如:
text = c("is it possible to highlight text for some words" ,
"suppose i want words like words to be red and words like text to be blue")
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以使用 R 的预定义单词列表的颜色来突出显示文档(特别是对于大型语料库)。列表中的每个单词都会获得特定的颜色。例如,将“单词”突出显示为红色,将“文本”突出显示为蓝色,如下所示。
我想计算聚类评估的轮廓。R中有一些包,例如cluster和clValid。这是我使用 cluster 包的代码:
# load the data
# a data from the UCI website with 434874 obs. and 3 variables
data <- read.csv("./data/spatial_network.txt",sep="\t",header = F)
# apply kmeans
km_res <- kmeans(data,20,iter.max = 1000,
nstart=20,algorithm="MacQueen")
# calculate silhouette
library(cluster)
sil <- silhouette(km_res$cluster, dist(data))
# plot silhouette
library(factoextra)
fviz_silhouette(sil)
Run Code Online (Sandbox Code Playgroud)
该代码适用于较小的数据,例如具有 50,000 个 obs 的数据,但是当数据大小有点大时,我会收到类似“错误:无法分配大小为 704.5 Gb 的向量”的错误。对于 Dunn 索引和大型数据集的其他内部索引来说,这可能会出现问题。
我的电脑有 32GB RAM。问题来自于计算 dist(data)。我想知道是否可以不提前计算dist(data),并在轮廓公式需要时计算相应的距离。
我感谢您对这个问题以及如何计算大型和超大型数据集的轮廓的帮助。
拼写更正时请考虑以下事项:
from autocorrect import spell
import re
WORD = re.compile(r'\w+')
def reTokenize(doc):
tokens = WORD.findall(doc)
return tokens
text = ["Hi, welcmoe to speling.","This is jsut an exapmle, but cosnider a veri big coprus."]
def spell_correct(text):
sptext = []
for doc in text:
sptext.append(' '.join([spell(w).lower() for w in reTokenize(doc)]))
return sptext
print(spell_correct(text))
Run Code Online (Sandbox Code Playgroud)
这是上面一段代码的输出:
如何停止在 jupyter 笔记本中显示输出?特别是如果我们有大量的文本文档,就会产生大量的输出。
我的第二个问题是:在大数据上应用时,如何提高代码的速度和准确性(例如,请检查输出中的“veri”一词)?有没有更好的方法来做到这一点?我感谢您以更快的速度做出回应和(替代)解决方案。