【问题标题】:In R how to split text/paragraph at end of statement (full stop) but not at the .(dot) in between the sentences在 R 中,如何在语句末尾(句号)而不是在句子之间的 .(点)处拆分文本/段落
【发布时间】:2019-01-19 18:19:13
【问题描述】:

在 R 中:- 例如:-

text_data<-" I have been a Gig subscriber for a decent amount of time.  When the service was originally installed I would observe 900+Mbps speeds.  I have deployed to Kosovo and since then (about 8 months ago) my wife has told me that the internet has become Slow.I consistently avg less than 286 Mbps when utilizing both speedtest.xfinity.com and fast.com as well as speedtest.net Current hardware: 1) Motorola MB8600 Modem 2) Linksys EA9500 I have not had a technician out to the site.Troubleshooting:I have replaced the Coax cable from wall to modem with 2 different new coax cables. I have replaced the ethernet from Modem to Router with 2 different new ethernet cables I have rebooted my Modem as well as attempted a factory reset. I have connected my home PC directly to the Modem (bypass router) with 2 different ethernet cables*NOTE*Wether I use the EA9500 or go directly to the PC I get the same slow speeds.*NOTE 2*I do not have a cable subscrption. No splitters on the line. It goes from Pole ---> Wall Jack --> Modem.There is always significant Uncorrected errors.  Attached are the Upstream and Downstream information and error logs.  These are 4 days after a modem reset."

> textdata<- as.String(text_data)

> a<-strsplit(text_data,".", fixed = TRUE)

输出:-

> a
[[1]]
 [1] " I have been a Gig subscriber for a decent amount of time"                                                                                                                           
 [2] "  When the service was originally installed I would observe 900+Mbps speeds"                                                                                                         
 [3] "  I have deployed to Kosovo and since then (about 8 months ago) my wife has told me that the internet has become Slow"                                                               
 [4] "I consistently avg less than 286 Mbps when utilizing both speedtest"                                                                                                                 
 [5] "xfinity"                                                                                                                                                                             
 [6] "com and fast"                                                                                                                                                                        
 [7] "com as well as speedtest"                                                                                                                                                            
 [8] "net Current hardware: 1) Motorola MB8600 Modem 2) Linksys EA9500 I have not had a technician out to the site"                                                                        
 [9] "Troubleshooting:I have replaced the Coax cable from wall to modem with 2 different new coax cables"                                                                                  
[10] " I have replaced the ethernet from Modem to Router with 2 different new ethernet cables I have rebooted my Modem as well as attempted a factory reset"                               
[11] " I have connected my home PC directly to the Modem (bypass router) with 2 different ethernet cables*NOTE*Wether I use the EA9500 or go directly to the PC I get the same slow speeds"
[12] "*NOTE 2*I do not have a cable subscrption"                                                                                                                                           
[13] " No splitters on the line"                                                                                                                                                           
[14] " It goes from Pole ---> Wall Jack --> Modem"                                                                                                                                         
[15] "There is always significant Uncorrected errors"                                                                                                                                      
[16] "  Attached are the Upstream and Downstream information and error logs"                                                                                                               
[17] "  These are 4 days after a modem reset"   

R 中所需的输出:- 文本应该在语句的末尾(句号)而不是在句子之间的 .(点)处分开。)

1)I have been a Gig subscriber for a decent amount of time.  
When the service was originally installed I would observe 900+Mbps speeds.  
2)I have deployed to Kosovo and since then (about 8 months ago) my wife has told me that the internet has become Slow.
3)I consistently avg less than 286 Mbps when utilizing both speedtest.xfinity.com and fast.com as well as speedtest.net Current hardware: 1) Motorola MB8600 Modem 2) Linksys EA9500 I have not had a technician out to the site.
4)Troubleshooting:I have replaced the Coax cable from wall to modem with 2 different new coax cables. 
5) I have replaced the ethernet from Modem to Router with 2 different new ethernet cables I have rebooted my Modem as well as attempted a factory reset. 
6)I have connected my home PC directly to the Modem (bypass router) with 2 different ethernet cables*NOTE*Wether I use the EA9500 or go directly to the PC I get the same slow speeds.
7)*NOTE 2*I do not have a cable subscrption. 
8)No splitters on the line. 
9)It goes from Pole ---> Wall Jack --> Modem.
10)There is always significant Uncorrected errors.  
11)Attached are the Upstream and Downstream information and error logs.  
12)These are 4 days after a modem reset.

请帮忙。

【问题讨论】:

  • 问题不清楚。句尾的点和句之间有什么区别?根据定义,句子之间有一个点或其他标记,这就是我们如何知道它们是两个句子,而不仅仅是一个。
  • 段落中的每个语句都应该在它的末尾拆分,即在分隔符句号处 (.) 但例如,如果语句在文本之间有 speedtest.xfinity.com,它应该保持原样是。
  • 只是初步的想法,但是你可以通过拆分". "得到一个近似的解决方案,因为大多数句子是用句点和空格分隔的,但域名和数字没有句点后跟a空间。

标签: r regex text nlp csv


【解决方案1】:

使用此特定数据的工作模式进行编辑;您可以在 . 后跟一个空格或大写字母与模式 \\.(?=( |[A-Z])) 分割。

您需要小心,因为您的句子后面没有正确的空格。这使得无法可靠地区分它们(参见输出中的第三个拆分句子)。这至少不会像您的第一次尝试那样在.com 的情况下分裂。这里我们用大写字母的区别来区分speedtest.xfinity.comSlow.I或者site.Troubleshooting,但是如果有人忘记了空格忘记大写下一句,就不行了。

library(stringr)
text_data <- " I have been a Gig subscriber for a decent amount of time. When the service was originally installed I would observe 900+Mbps speeds. I have deployed to Kosovo and since then (about 8 months ago) my wife has told me that the internet has become Slow.I consistently avg less than 286 Mbps when utilizing both speedtest.xfinity.com and fast.com as well as speedtest.net Current hardware: 1) Motorola MB8600 Modem 2) Linksys EA9500 I have not had a technician out to the site.Troubleshooting:I have replaced the Coax cable from wall to modem with 2 different new coax cables. I have replaced the ethernet from Modem to Router with 2 different new ethernet cables I have rebooted my Modem as well as attempted a factory reset. I have connected my home PC directly to the Modem (bypass router) with 2 different ethernet cablesNOTEWether I use the EA9500 or go directly to the PC I get the same slow speeds.*NOTE 2*I do not have a cable subscrption. No splitters on the line. It goes from Pole ---> Wall Jack --> Modem.There is always significant Uncorrected errors. Attached are the Upstream and Downstream information and error logs. These are 4 days after a modem reset."
text_data %>%
  str_split("\\.(?=( |[A-Z]))")
#> [[1]]
#>  [1] " I have been a Gig subscriber for a decent amount of time"                                                                                                                                                                     
#>  [2] " When the service was originally installed I would observe 900+Mbps speeds"                                                                                                                                                    
#>  [3] " I have deployed to Kosovo and since then (about 8 months ago) my wife has told me that the internet has become Slow"                                                                                                          
#>  [4] "I consistently avg less than 286 Mbps when utilizing both speedtest.xfinity.com and fast.com as well as speedtest.net Current hardware: 1) Motorola MB8600 Modem 2) Linksys EA9500 I have not had a technician out to the site"
#>  [5] "Troubleshooting:I have replaced the Coax cable from wall to modem with 2 different new coax cables"                                                                                                                            
#>  [6] " I have replaced the ethernet from Modem to Router with 2 different new ethernet cables I have rebooted my Modem as well as attempted a factory reset"                                                                         
#>  [7] " I have connected my home PC directly to the Modem (bypass router) with 2 different ethernet cablesNOTEWether I use the EA9500 or go directly to the PC I get the same slow speeds.*NOTE 2*I do not have a cable subscrption"  
#>  [8] " No splitters on the line"                                                                                                                                                                                                     
#>  [9] " It goes from Pole ---> Wall Jack --> Modem"                                                                                                                                                                                   
#> [10] "There is always significant Uncorrected errors"                                                                                                                                                                                
#> [11] " Attached are the Upstream and Downstream information and error logs"                                                                                                                                                          
#> [12] " These are 4 days after a modem reset."

reprex package (v0.2.0) 于 2018 年 8 月 12 日创建。

【讨论】:

  • 非常感谢您的及时回答,它对我有用,对于第三行之后没有空格的 . (句号),理想情况下应该有空间,已经进入了空间并且它按预期工作。
猜你喜欢
  • 2012-12-16
  • 2019-04-18
  • 1970-01-01
  • 2016-10-09
  • 2013-08-13
  • 1970-01-01
  • 2012-03-17
  • 2019-04-07
相关资源
最近更新 更多