문서 변환

    어떤 문서에 Computer라는 단어가 있다고 하자. 일반적으로 이 단어는 computer, COMPUTER와 같은 단어일 것이다. 또, 복수 형태인 computers와도 같은 단어다. 뿐만 아니라 ‘computer’와 같이 따옴표가 붙어 있어도 같은 단어다.

    이들을 모두 같은 단어로 처리하려면 문서에서 문장 부호를 제거하거나, 문자를 모두 소문자로 바꾸거나, 단어를 그 원형이 되는 뿌리root 형태로 바꿔주는 스테밍Stemming[20]을 적용하는 등 문서를 변환할 필요가 있다. 이때 사용하는 함수가 tm_map( )이다.

    표 10-13 문서 변환

    tm::tm_map : 문서에 함수를 적용하여 변환한다.

    tm::tm_map(
      x,   # 코퍼스
      FUN  # 변환에 사용할 함수

    반환 값은 변환된 결과다.

    tm_map에 지정하는 함수는 사용자가 직접 만든 함수여도 되고, tm이 제공하는 함수여도 된다. tm이 제공하는 변환 함수들의 목록은 getTransformations( )로 볼 수 있다. 다음에 이들 중 몇 가지 함수를 보였다.

    표 10-14 tm이 제공하는 문서 변환 함수

    함수명

    의미

    tm::removePunctuation

    문장 부호를 없앤다.

    tm::stripWhitespace

    불필요한 공백을 지운다. 연속된 공백 여러 개는 공백 하나로 치환된다.

    다음은 crude 문서들의 글자들을 모두 소문자로 바꾸고, 문장 부호를 제거하는 예다.

    > inspect(tm_map(tm_map(crude, tolower), removePunctuation)[1])
    A corpus with 1 text document
    
    The metadata consists of 2 tag-value pairs and a data frame
    Available tags are:
      create_date creator
    Available variables in the data frame are:
      MetaID
    
    $`reut-00001.xml`
    diamond shamrock corp said that
    effective today it had cut its contract prices for crude oil by
    150 dlrs a barrel
        the reduction brings its posted price for west texas
    intermediate to 1600 dlrs a barrel the copany said
        the price reduction today was made in the light of falling
    oil product prices and a weak crude oil market a company
    spokeswoman said
        diamond is the latest in a line of us oil companies that
    have cut its contract or posted prices over the last two days
    citing weak oil markets
     reuter
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.