我有一个 400,000 行的文件,其中包含需要进行地理编码的手动输入地址。文件中的相同地址有很多不同的变体,因此多次对同一地址使用 API 调用似乎很浪费。
为了减少这种情况,我想减少这五行:
Address
1 Main Street, Country A, World
1 Main St, Country A, World
1 Maine St, Country A, World
2 Side Street, Country A, World
2 Side St. Country A, World
Run Code Online (Sandbox Code Playgroud)
下降到两个:
Address
1 Main Street, Country A, World
2 Side Street, Country A, World
Run Code Online (Sandbox Code Playgroud)
使用该stringdist包,您可以将字符串的“单词”部分组合在一起,但字符串匹配算法不区分数字。这意味着它将同一街道上的两个不同房屋号码归为同一地址。
为了解决这个问题,我想出了两种方法:首先,尝试使用正则表达式将数字和地址手动分离到单独的列中,然后重新加入它们。这样做的问题是,有这么多手动输入的地址,似乎有数百种不同的边缘情况,而且它变得笨拙。
使用这个关于分组的答案和这个将单词转换为数字的答案,我有第二种方法来处理边缘情况,但在计算上非常昂贵。有没有更好的第三种方法来做到这一点?
library(gsubfn)
library(english)
library(qdap)
library(stringdist)
library(tidyverse)
similarGroups <- function(x, thresh = 0.8, method = "lv"){
grp <- integer(length(x))
Address …Run Code Online (Sandbox Code Playgroud) # Sample Data Frame
df <- data.frame(Column_A
=c("1011 Red Cat",
"Mouse 2011 is in the House 3001", "Yellow on Blue Dog walked around Park"))
Run Code Online (Sandbox Code Playgroud)
我有一列试图清除的手动输入数据。
Column_A
1|1011 Red Cat |
2|Mouse 2011 is in the House 3001 |
2|Yellow on Blue Dog walked around Park|
Run Code Online (Sandbox Code Playgroud)
我想将每个特征分成其自己的列,但仍保留列A以在以后提取其他特征。
Colour Code Column_A
1|Red |1001 |Cat
2|NA |2001 3001 |Mouse is in the House
3|Yellow on Blue |NA |Dog walked around Park
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在用gsub重新排列它们并捕获组,然后使用Tidyr :: extract分离它们。
library(dplyr)
library(tidyr)
library(stringr)
df1 <- df %>%
# Reorders …Run Code Online (Sandbox Code Playgroud) 通过初学者Python书籍,我有两个相当简单的事情,我不明白,并希望有人在这里可以提供帮助.
本书中的示例使用正则表达式从剪贴板中接收电子邮件地址和电话号码,并将它们输出到控制台.代码如下所示:
#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
import pyperclip, re
# Create phone regex.
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? #[1] area code
(\s|-|\.)? #[2] separator
(\d{3}) #[3] first 3 digits
(\s|-|\.) #[4] separator
(\d{4}) #[5] last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))? #[6] extension
)''', re.VERBOSE)
# Create email regex.
emailRegex = re.compile(r'''(
[a-zA-Z0-9._%+-]+
@
[\.[a-zA-Z0-9.-]+
(\.[a-zA-Z]{2,4})
)''', re.VERBOSE)
# Find matches in clipboard text.
text = str(pyperclip.paste())
matches = []
for groups in phoneRegex.findall(text): …Run Code Online (Sandbox Code Playgroud)