Đây là câu hỏi đầu tiên của tôi về SO vì vậy hãy cho tôi biết nếu nó có thể được cải thiện. Tôi đang làm việc trên một dự án xử lý ngôn ngữ tự nhiên trong R và đang cố gắng để xây dựng một data.table có chứa các trường hợp thử nghiệm. Ở đây, tôi xây dựng một nhiều ví dụ đơn giản:Cột chia tách dữ liệu có thể tạo ra NAs
texts.dt <- data.table(string = c("one",
"two words",
"three words here",
"four useless words here",
"five useless meaningless words here",
"six useless meaningless words here just",
"seven useless meaningless words here just to",
"eigth useless meaningless words here just to fill",
"nine useless meaningless words here just to fill up",
"ten useless meaningless words here just to fill up space"),
word.count = 1:10,
stop.at.word = c(0, 1, 2, 2, 4, 3, 3, 6, 7, 5))
này trả về data.table chúng tôi sẽ làm việc trên:
string word.count stop.at.word
1: one 1 0
2: two words 2 1
3: three words here 3 2
4: four useless words here 4 2
5: five useless meaningless words here 5 4
6: six useless meaningless words here just 6 3
7: seven useless meaningless words here just to 7 3
8: eigth useless meaningless words here just to fill 8 6
9: nine useless meaningless words here just to fill up 9 7
10: ten useless meaningless words here just to fill up space 10 5
Trong ứng dụng thực tế, giá trị trong cột stop.at.word
được xác định một cách ngẫu nhiên (với giới hạn trên = word.count
- 1). Ngoài ra, các chuỗi không được sắp xếp theo chiều dài nhưng điều đó không tạo nên sự khác biệt.
Mã nên thêm hai cột input
và output
, nơi input
chứa chuỗi con từ vị trí 1 đến stop.at.word
và output
chứa các từ mà sau (từ đơn), như vậy:
>desired_result
string word.count stop.at.word input
1: one 1 0
2: two words 2 1 two
3: three words here 3 2 three words
4: four useless words here 4 2 four useless
5: five useless meaningless words here 5 4 five useless meaningless words
6: six useless meaningless words here just 6 2 six useless
7: seven useless meaningless words here just to 7 3 seven useless meaningless
8: eigth useless meaningless words here just to fill 8 6 eigth useless meaningless words here just
9: nine useless meaningless words here just to fill up 9 7 nine useless meaningless words here just to
10: ten useless meaningless words here just to fill up space 10 5 ten useless meaningless words here
output
1:
2: words
3: here
4: words
5: here
6: meaningless
7: words
8: to
9: fill
10: just
Đáng tiếc là những gì tôi có được thay vì là thế này:
string word.count stop.at.word input output
1: one 1 0
2: two words 2 1 NA NA
3: three words here 3 2 NA NA
4: four useless words here 4 2 NA NA
5: five useless meaningless words here 5 4 NA NA
6: six useless meaningless words here just 6 3 NA NA
7: seven useless meaningless words here just to 7 3 NA NA
8: eigth useless meaningless words here just to fill 8 6 NA NA
9: nine useless meaningless words here just to fill up 9 7 NA NA
10: ten useless meaningless words here just to fill up space 10 5 ten NA
Thông báo kết quả không phù hợp, với một chuỗi rỗng trên hàng 1 và "mười" trở lại trên hàng 10
Đây là mã tôi đang sử dụng:
texts.dt[, c("input", "output") := .(
substr(string,
1,
sapply(gregexpr(" ", string),"[", stop.at.word) - 1),
substr(string,
sapply(gregexpr(" ", string),"[", stop.at.word),
sapply(gregexpr(" ", string),"[", stop.at.word + 1) - 1)
)]
Tôi chạy nhiều xét nghiệm và substr
hướng dẫn hoạt động tốt khi tôi cố gắng xâu cá nhân trong giao diện điều khiển, nhưng thất bại khi áp dụng cho các data.table. Tôi nghi ngờ tôi thiếu một cái gì đó liên quan đến phạm vi trong data.table, nhưng tôi đã không được sử dụng gói này cho lâu vì vậy tôi khá bối rối.
Tôi sẽ đánh giá cao một số trợ giúp. Cảm ơn bạn trước!
Khiếu nại nhỏ: Cố gắng làm cho các ví dụ của bạn đủ nhỏ để chúng không yêu cầu cuộn ngang trong trình duyệt. – Frank
@Franck - Chắc chắn, tôi sẽ làm tốt hơn lần sau! –
Tôi không chắc tại sao hai câu trả lời khác lại bị xóa ..? @ProcrastinatusMaximus – eddi