Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to split chinese character?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str104 地址1
    "河北省邢台市临西县摇鞍镇乡史庄村"                  
    "河北省保定市阜平县龙泉关镇印钞石村110号"        
    "河北省保定市阜平县龙泉关镇顾家台村"              
    "河北省保定市阜平县龙泉关镇顾家台村26号"          
    "河北省保定市易县三义村"                                
    "河北省保定市曲阳县产德乡胡家咀村村北"            
    "河北省保定市曲阳县恒州镇北马古庄村"              
    "河北省保定市曲阳县孝墓乡南孝墓村村南"            
    "河北省保定市清苑区臧村镇东庄村"                    
    "保定市北市区王辛庄村"                                    
    "河北省保定市清苑区臧村镇东庄村"                    
    "河北省保定市曲阳县灵山镇杏子沟村村北"            
    "河北省保定市曲阳县灵山镇南镇村村南"              
    "安国市郑章镇西桄村东大街061号"                        
    "安国市郑章镇海市村5街80号"                              
    "盐山县小庄乡吕宅村"                                      
    "盐山县千童镇十寨子村"                                    
    "盐山县小庄乡李长堤村"                                    
    "宁夏吴忠市红寺堡区红寺堡镇红关村001号"            
    "吴忠市红寺堡开发区太阳山镇红星村红古窑组006号"
    "吴忠市红寺堡区太阳山镇周圈村周圈组002号"        
    end

    I want to get the follow one,how to write command?
    地址1 区县 镇乡
    河北省邢台市临西县摇鞍镇乡史庄村 河北省 邢台市 临西县 摇鞍镇 史庄村
    河北省保定市阜平县龙泉关镇印钞石村110号 河北省 保定市 阜平县 龙泉关镇 印钞石村
    河北省保定市曲阳县孝墓乡南孝墓村村南 河北省 保定市 曲阳县 孝墓乡 南孝墓村
    河北省保定市清苑区臧村镇东庄村 河北省 保定市 清苑区 臧村镇 东庄村
    保定市北市区王辛庄村 保定市 北市区 王辛庄村
    安国市郑章镇西桄村东大街061号 安车市 郑章镇 西桄村
    盐山县小庄乡吕宅村 盐山县 小庄乡 吕宅村
    盐山县千童镇十寨子村 盐山县 千童镇 十寨子村
    盐山县小庄乡李长堤村 盐山县 小庄乡 李长堤村
    宁夏回族自治区吴忠市红寺堡区红寺堡镇红关村001号 宁夏回族自治区 吴忠市 红寺堡区 红寺堡镇 红关村
    吴忠市红寺堡开发区太阳山镇红星村红古窑组006号 吴忠市 红寺堡开发区 太阳山镇 红星村
    吴忠市红寺堡区太阳山镇周圈村周圈组002号 吴忠市 红寺堡区 太阳山镇 周圈村
    Last edited by huhu bao; 16 Mar 2020, 19:53.

  • #2
    Once your list of end-words become many, as compared to e.g., https://www.statalist.org/forums/for...is-chinese-str, there may be benefits to reshaping to a long layout. In your data example, no end-word is repeated, but I attempt to make the code below robust to repetition.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str104 地址1
    "河北省邢台市临西县摇鞍镇乡史庄村"                  
    "河北省保定市阜平县龙泉关镇印钞石村110号"        
    "河北省保定市阜平县龙泉关镇顾家台村"              
    "河北省保定市阜平县龙泉关镇顾家台村26号"          
    "河北省保定市易县三义村"                                
    "河北省保定市曲阳县产德乡胡家咀村村北"            
    "河北省保定市曲阳县恒州镇北马古庄村"              
    "河北省保定市曲阳县孝墓乡南孝墓村村南"            
    "河北省保定市清苑区臧村镇东庄村"                    
    "保定市北市区王辛庄村"                                    
    "河北省保定市清苑区臧村镇东庄村"                    
    "河北省保定市曲阳县灵山镇杏子沟村村北"            
    "河北省保定市曲阳县灵山镇南镇村村南"              
    "安国市郑章镇西桄村东大街061号"                        
    "安国市郑章镇海市村5街80号"                              
    "盐山县小庄乡吕宅村"                                      
    "盐山县千童镇十寨子村"                                    
    "盐山县小庄乡李长堤村"                                    
    "宁夏吴忠市红寺堡区红寺堡镇红关村001号"            
    "吴忠市红寺堡开发区太阳山镇红星村红古窑组006号"
    "吴忠市红寺堡区太阳山镇周圈村周圈组002号"        
    end
    
    gen strvar= 地址1
    local length 0
    local i=1
    foreach l in 省 区 市 县 镇 乡 村{
           gen pos`i'= strpos(地址1, "`l'")
           gen l`i'="`l'"
           replace strvar=(subinstr(strvar, "`l'", "", 1))
           local length= `length' + length("`l'")
           local++i 
           forval j=1/10{
                if regexm(strvar, "`l'")==1{
                      gen pos`i'= strpos(strvar, "`l'") + `length'
                      gen l`i'="`l'"
                      replace strvar=(subinstr(strvar, "`l'", "", 1))
                      local length= `length' + length("`l'")
                      local++i
                }
          }
    }
    gen id=_n
    reshape long pos l, i(id) j(which)
    sort id pos
    drop if pos==0
    by id: gen length= pos if _n==1
    by id: replace length= pos - pos[_n-1] if missing(length)
    by id: gen which2=_n
    qui sum which2
    local max=r(max)
    gen wanted=""
    replace strvar=地址1
    by id: replace wanted= substr(strvar, 1, length-1)+l if which2==1
    by id: replace strvar=ustrregexrf(strvar, wanted, "", 1) if which2==1
    by id: replace strvar=strvar[1]
    forval i=2/`max'{
           by id: replace wanted= substr(strvar, 1, length) if missing(wanted) & which2==`i'
           by id: replace strvar=ustrregexrf(strvar, wanted, "", 1) if which2==`i'
           by id: replace strvar=strvar[`i']
    }
    drop which strvar pos l length
    reshape wide wanted, i(id) j(which2)
    Res.:

    Code:
    . l 地址1 wanted*, notrim
    
                                                   地址1      wanted1        wanted2    wanted3    wanted4      wanted5   wanted6  
      1.                河北省邢台市临西县摇鞍镇乡史庄村       河北省         邢台市     临西县     摇鞍镇           乡    史庄村  
      2.         河北省保定市阜平县龙泉关镇印钞石村110号       河北省         保定市     阜平县   龙泉关镇     印钞石村            
      3.              河北省保定市阜平县龙泉关镇顾家台村       河北省         保定市     阜平县   龙泉关镇     顾家台村            
      4.          河北省保定市阜平县龙泉关镇顾家台村26号       河北省         保定市     阜平县   龙泉关镇     顾家台村            
      5.                          河北省保定市易县三义村       河北省         保定市       易县     三义村                         
      6.            河北省保定市曲阳县产德乡胡家咀村村北       河北省         保定市     曲阳县     产德乡     胡家咀村            
      7.              河北省保定市曲阳县恒州镇北马古庄村       河北省         保定市     曲阳县     恒州镇   北马古庄村            
      8.            河北省保定市曲阳县孝墓乡南孝墓村村南       河北省         保定市     曲阳县     孝墓乡     南孝墓村            
      9.                  河北省保定市清苑区臧村镇东庄村       河北省         保定市     清苑区       臧村           镇            
     10.                            保定市北市区王辛庄村       保定市         北市区   王辛庄村                                    
     11.                  河北省保定市清苑区臧村镇东庄村       河北省         保定市     清苑区       臧村           镇            
     12.            河北省保定市曲阳县灵山镇杏子沟村村北       河北省         保定市     曲阳县     灵山镇     杏子沟村            
     13.              河北省保定市曲阳县灵山镇南镇村村南       河北省         保定市     曲阳县     灵山镇       南镇村            
     14.                   安国市郑章镇西桄村东大街061号       安国市         郑章镇     西桄村                                    
     15.                       安国市郑章镇海市村5街80号       安国市         郑章镇     海市村                                    
     16.                              盐山县小庄乡吕宅村       盐山县         小庄乡     吕宅村                                    
     17.                            盐山县千童镇十寨子村       盐山县         千童镇   十寨子村                                    
     18.                            盐山县小庄乡李长堤村       盐山县         小庄乡   李长堤村                                    
     19.           宁夏吴忠市红寺堡区红寺堡镇红关村001号   宁夏吴忠市       红寺堡区   红寺堡镇     红关村                         
     20.   吴忠市红寺堡开发区太阳山镇红星村红古窑组006号       吴忠市   红寺堡开发区   太阳山镇     红星村                         
     21.         吴忠市红寺堡区太阳山镇周圈村周圈组002号       吴忠市       红寺堡区   太阳山镇     周圈村

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      Once your list of end-words become many, as compared to e.g., https://www.statalist.org/forums/for...is-chinese-str, there may be benefits to reshaping to a long layout. In your data example, no end-word is repeated, but I attempt to make the code below robust to repetition.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str104 地址1
      "河北省邢台市临西县摇鞍镇乡史庄村"
      "河北省保定市阜平县龙泉关镇印钞石村110号"
      "河北省保定市阜平县龙泉关镇顾家台村"
      "河北省保定市阜平县龙泉关镇顾家台村26号"
      "河北省保定市易县三义村"
      "河北省保定市曲阳县产德乡胡家咀村村北"
      "河北省保定市曲阳县恒州镇北马古庄村"
      "河北省保定市曲阳县孝墓乡南孝墓村村南"
      "河北省保定市清苑区臧村镇东庄村"
      "保定市北市区王辛庄村"
      "河北省保定市清苑区臧村镇东庄村"
      "河北省保定市曲阳县灵山镇杏子沟村村北"
      "河北省保定市曲阳县灵山镇南镇村村南"
      "安国市郑章镇西桄村东大街061号"
      "安国市郑章镇海市村5街80号"
      "盐山县小庄乡吕宅村"
      "盐山县千童镇十寨子村"
      "盐山县小庄乡李长堤村"
      "宁夏吴忠市红寺堡区红寺堡镇红关村001号"
      "吴忠市红寺堡开发区太阳山镇红星村红古窑组006号"
      "吴忠市红寺堡区太阳山镇周圈村周圈组002号"
      end
      
      gen strvar= 地址1
      local length 0
      local i=1
      foreach l in 省 区 市 县 镇 乡 村{
      gen pos`i'= strpos(地址1, "`l'")
      gen l`i'="`l'"
      replace strvar=(subinstr(strvar, "`l'", "", 1))
      local length= `length' + length("`l'")
      local++i
      forval j=1/10{
      if regexm(strvar, "`l'")==1{
      gen pos`i'= strpos(strvar, "`l'") + `length'
      gen l`i'="`l'"
      replace strvar=(subinstr(strvar, "`l'", "", 1))
      local length= `length' + length("`l'")
      local++i
      }
      }
      }
      gen id=_n
      reshape long pos l, i(id) j(which)
      sort id pos
      drop if pos==0
      by id: gen length= pos if _n==1
      by id: replace length= pos - pos[_n-1] if missing(length)
      by id: gen which2=_n
      qui sum which2
      local max=r(max)
      gen wanted=""
      replace strvar=地址1
      by id: replace wanted= substr(strvar, 1, length-1)+l if which2==1
      by id: replace strvar=ustrregexrf(strvar, wanted, "", 1) if which2==1
      by id: replace strvar=strvar[1]
      forval i=2/`max'{
      by id: replace wanted= substr(strvar, 1, length) if missing(wanted) & which2==`i'
      by id: replace strvar=ustrregexrf(strvar, wanted, "", 1) if which2==`i'
      by id: replace strvar=strvar[`i']
      }
      drop which strvar pos l length
      reshape wide wanted, i(id) j(which2)
      Res.:

      Code:
      . l 地址1 wanted*, notrim
      
      地址1 wanted1 wanted2 wanted3 wanted4 wanted5 wanted6
      1. 河北省邢台市临西县摇鞍镇乡史庄村 河北省 邢台市 临西县 摇鞍镇 乡 史庄村
      2. 河北省保定市阜平县龙泉关镇印钞石村110号 河北省 保定市 阜平县 龙泉关镇 印钞石村
      3. 河北省保定市阜平县龙泉关镇顾家台村 河北省 保定市 阜平县 龙泉关镇 顾家台村
      4. 河北省保定市阜平县龙泉关镇顾家台村26号 河北省 保定市 阜平县 龙泉关镇 顾家台村
      5. 河北省保定市易县三义村 河北省 保定市 易县 三义村
      6. 河北省保定市曲阳县产德乡胡家咀村村北 河北省 保定市 曲阳县 产德乡 胡家咀村
      7. 河北省保定市曲阳县恒州镇北马古庄村 河北省 保定市 曲阳县 恒州镇 北马古庄村
      8. 河北省保定市曲阳县孝墓乡南孝墓村村南 河北省 保定市 曲阳县 孝墓乡 南孝墓村
      9. 河北省保定市清苑区臧村镇东庄村 河北省 保定市 清苑区 臧村 镇
      10. 保定市北市区王辛庄村 保定市 北市区 王辛庄村
      11. 河北省保定市清苑区臧村镇东庄村 河北省 保定市 清苑区 臧村 镇
      12. 河北省保定市曲阳县灵山镇杏子沟村村北 河北省 保定市 曲阳县 灵山镇 杏子沟村
      13. 河北省保定市曲阳县灵山镇南镇村村南 河北省 保定市 曲阳县 灵山镇 南镇村
      14. 安国市郑章镇西桄村东大街061号 安国市 郑章镇 西桄村
      15. 安国市郑章镇海市村5街80号 安国市 郑章镇 海市村
      16. 盐山县小庄乡吕宅村 盐山县 小庄乡 吕宅村
      17. 盐山县千童镇十寨子村 盐山县 千童镇 十寨子村
      18. 盐山县小庄乡李长堤村 盐山县 小庄乡 李长堤村
      19. 宁夏吴忠市红寺堡区红寺堡镇红关村001号 宁夏吴忠市 红寺堡区 红寺堡镇 红关村
      20. 吴忠市红寺堡开发区太阳山镇红星村红古窑组006号 吴忠市 红寺堡开发区 太阳山镇 红星村
      21. 吴忠市红寺堡区太阳山镇周圈村周圈组002号 吴忠市 红寺堡区 太阳山镇 周圈村
      thank you very much , it greatly reduce my worlkload!

      Comment


      • #4
        One more thing that I can add. I noticed subsequently that you wanted the following grouping
        区县 镇乡
        You can change the end of the code (follows immediately after the loop) to achieve this as I have a variable identifying the word.

        Code:
        * 区 and县 are grouped together, as "镇" and "乡"
        replace l="区" if l=="县"
        replace l="镇" if l=="乡"
        *ENCODE ASSIGNS A UNIQUE NUMBER TO A GROUP
        encode l, gen(l2)
        *HERE MANUAL INPUT REQUIRED TO IDENTIFY DUPLICATES
        duplicates list id l2
        *ONE DUPLICATE IN DATA. ASSIGN UNIQUE VALUE
        replace l2= 8 in 5
        drop which* strvar pos l length
        reshape wide wanted, i(id) j(l2)
        Alternatively, because single words are uninformative and may be responsible for the duplicates problem (at least with this example data), you can eliminate them prior to grouping.

        Code:
        drop if wanted==l
        replace l="区" if l=="县"
        replace l="镇" if l=="乡"
        encode l, gen(l2)
        drop which* strvar pos l length
        reshape wide wanted, i(id) j(l2)

        Res.:

        Code:
        . l wanted*, notrim
        
                    wanted1      wanted2      wanted3   wanted4    wanted5  
          1.         临西县       邢台市       史庄村    河北省     摇鞍镇  
          2.         阜平县       保定市     印钞石村    河北省   龙泉关镇  
          3.         阜平县       保定市     顾家台村    河北省   龙泉关镇  
          4.         阜平县       保定市     顾家台村    河北省   龙泉关镇  
          5.           易县       保定市       三义村    河北省            
          6.         曲阳县       保定市     胡家咀村    河北省     产德乡  
          7.         曲阳县       保定市   北马古庄村    河北省     恒州镇  
          8.         曲阳县       保定市     南孝墓村    河北省     孝墓乡  
          9.         清苑区       保定市         臧村    河北省            
         10.         北市区       保定市     王辛庄村                      
         11.         清苑区       保定市         臧村    河北省            
         12.         曲阳县       保定市     杏子沟村    河北省     灵山镇  
         13.         曲阳县       保定市       南镇村    河北省     灵山镇  
         14.                      安国市       西桄村               郑章镇  
         15.                      安国市       海市村               郑章镇  
         16.         盐山县                    吕宅村               小庄乡  
         17.         盐山县                  十寨子村               千童镇  
         18.         盐山县                  李长堤村               小庄乡  
         19.       红寺堡区   宁夏吴忠市       红关村             红寺堡镇  
         20.   红寺堡开发区       吴忠市       红星村             太阳山镇  
         21.       红寺堡区       吴忠市       周圈村             太阳山镇
        Last edited by Andrew Musau; 18 Mar 2020, 06:35.

        Comment


        • #5
          Andrew Musau, you are so kind,thank you

          Comment

          Working...
          X