사용자 도구

사이트 도구


android:텍스트문장비교하기
텍스트문장비교하기

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
android:텍스트문장비교하기 [2025/05/03 02:11] – 라이브러리 소개 이거니맨android:텍스트문장비교하기 [2025/05/04 01:37] (현재) 이거니맨
줄 87: 줄 87:
 ===== Java-diff-utils ===== ===== Java-diff-utils =====
  
-==== 소개 ====+==== 1. 소개 ====
  
 텍스트를 비교하는 글자로 자바진영에는 [[https://github.com/java-diff-utils/java-diff-utils|Java-diff-utils]]가 존재했다.  텍스트를 비교하는 글자로 자바진영에는 [[https://github.com/java-diff-utils/java-diff-utils|Java-diff-utils]]가 존재했다. 
줄 96: 줄 96:
  
 ^  제시어  ^  답변  ^ ^  제시어  ^  답변  ^
-|  Be wise in what is good and innocent in what is evil  | Be wise in <del>the</del> good and innocent in what is evil  | +|  Be wise in what is good and innocent in what is evil  | Be wise in <del>the</del> good and innocent in what is evil  |   
 + 
 + 
 +==== 2. 설치 ====  
 + 
 +모듈단계의 그래들 파일((build.gradle.kts (Module :app) ))에 다음의 의존성을 추가한다.  
 + 
 +<code kotlin> 
 +    // Java Diff - phrase difference compare util 
 +    implementation("io.github.java-diff-utils:java-diff-utils:4.15"
 +</code> 
 + 
 + 
 +==== 3. 사용예제 ====  
 + 
 +[[https://github.com/java-diff-utils/java-diff-utils|Java-diff-utils]] 페이지에도 있지만, 이 라이브러리를 사용하는 방법은 아래와 같다.  
 + 
 +<code kotlin>  
 +@Composable 
 +@Throws(DiffException::class) 
 +fun TestGenerator_Second() 
 +    val first = listOf("This is a test senctence. and this is very long sentence", "This is the second line.", "And here is the finish."
 +    val second = listOf("This is a test for diffutils. and this is very long diffutils sentence", "This is the second line."
 + 
 +    val generator = DiffRowGenerator.create() 
 +        .showInlineDiffs(true) 
 +        .inlineDiffByWord(true) //show the ~ ~ and ** ** around each different word instead of each letter 
 +        .oldTag { f: Boolean? -> "~" } //introduce markdown style for strikethrough 
 +        .newTag { f: Boolean? -> "**" } //introduce markdown style for bold 
 +        .build() 
 + 
 +    val rows: List<DiffRow> = generator.generateDiffRows(first, second) 
 + 
 +    rows.forEach() { 
 +//        Text(text = it.oldLine, color = Color.Yellow) 
 +        Text(text = richtextFromMarkdown(it.oldLine, "~"), color = Color.Yellow) 
 +        Text(text = richtextFromMarkdown(it.newLine, "**"), color = Color.White) 
 +    } 
 +
 + 
 +</code>  
 + 
 +generator.generateDiffRows(비교군, 대상군)라는 것에 주의하면 된다.  
 + 
 +그런데 위 예제에서 비교군(oldLine)에서는 차이점이 있는 부분의 앞뒤에 "물결무늬(~)"를 마크다운으로 추가해주고, 대상군(newLine)에서는 차이점이 있는 부분의 앞뒤에 "더블스타(**)"를 마크다운으로 추가해주게 했다.  
 + 
 +전후에 마크다운을 삽입하는 것보다는 텍스트의 스타일을 바꾸는 것 - 예를 들어 중간선을 넣는다거나, 폰트 색깔을 변경하는 것 -을 할 수 있다면 문장 비교가 결과가 더 시인성이 있을 것이다.  
 + 
 +==== 4. 마크다운을 스타일화된 텍스트로 바꾸기 ==== 
 + 
 +Jetpack compose에서 제공하는 AnnotatedString을 이용하면 스타일화된 텍스트를 만들 수 있다.  
 + 
 +마크다운으로 차이가 있는 부분에만 강조해주는 것이므로 다음과 같이 마크다운을 주심으로 텍스트를 나눈 후에 짝수부분에만 스타일을 설정해 주면 된다.  
 + 
 +<code kotlin> 
 +// 마크다운 텍스트를 리치스타일로 변환하는 유틸 
 +fun richtextFromMarkdown(inputText : String, divider : String) : AnnotatedString 
 +
 +    val listOfString = inputText.split(divider) 
 +    val oldLineStyle = SpanStyle(color = Color.Red) 
 +    val newLineStyle = SpanStyle(color = Color.Blue, textDecoration = TextDecoration.LineThrough) 
 + 
 +    val formattedString = buildAnnotatedString { 
 +        listOfString.forEachIndexed() { index, s -> 
 +            if (index % 2 == 0) { 
 +                append(s) 
 +            }else { 
 +                withStyle(if (divider == "~") { oldLineStyle  } else { newLineStyle} ) {append(s)} 
 +            } 
 +        } 
 + 
 +    } 
 + 
 +    return formattedString 
 +
 +</code> 
 + 
 +==== 5. 두 텍스트 사이의 차이점을 점수화하기 ==== 
 + 
 +'레벤스타인 거리'라는 측정 알고리즘이 존재한다.  
 + 
 +https://en.wikipedia.org/wiki/Levenshtein_distance  
 + 
android/텍스트문장비교하기.1746205865.txt.gz · 마지막으로 수정됨: 2025/05/03 02:11 저자 이거니맨