Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/khun.Facts/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"runtimes": {
"osx.10.11-x64": {}
},
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
Expand Down
23 changes: 20 additions & 3 deletions src/khun/KhunExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Text.RegularExpressions;

namespace GreatFriends.Khun {
public static class KhunExtension {
Expand All @@ -16,6 +17,11 @@ public static class KhunExtension {
"อ."
};

private static string[] vowels = new string[] {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ขอถกเรื่อง solution ใน issue นี้ก่อนนะครับ แนวทางตีโจทย์และหาทางออก เท่าที่คุยและคิดกัน เหมือนจะมี 2 อย่างครับ คือ ดูว่าตัวอักษรตัวที่ 4 เป็น สระ กับอีกอย่างคือ ยังมีชื่อที่ตัวอักษรตัวที่ 4 ไม่ใช่สระด้วย เช่น คุณณา แต่มันอาจจะมีน้อย จนเราอาจจะใช้วิธี list ชื่อคน"พิเศษ" เหล่านี้แบบ hard-code ไปเลย

คิดเห็นยังไงครับ? : )

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list พิเศษก็เหมาะสมครับ แค่อาจจะต้องคอยมาปรับปรุงอยู่เป็นระยะๆ

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ถ้าอย่างนั้น เราน่าจะมี test cases ได้ประมาณนี้

// names that in the list
"คุณวุฒิ".AsKhun() => "คุณคุณวุฒิ"
"คุณคุณวุฒิ".AsKhun() => "คุณคุณวุฒิ"

// names that not in the list (can check with vowels)
"คุณากร".AsKhun() => "คุณคุณากร"
"คุณคุณากร".AsKhun() => "คุณคุณากร"

// names that not in the list (cannot check with vowels)
"คุณศรี".AsKhun() => "คุณศรี"
"คุณคุณศรี".AsKhun() => "คุณคุณศรี"

// the list
string[] specialNames = new string [] { "คุณวุฒิ", "คุณสมบัติ" };

"า"
};

private const string khun = "คุณ";

public static string AsKhun(this string name) {

Expand All @@ -28,8 +34,19 @@ public static string AsKhun(this string name) {
string[] parts = name.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);

if (parts[0].StartsWith("คุณ")) {
return string.Join(" ", parts);
if (parts[0].StartsWith(khun)) {
Regex regex = new Regex(Regex.Escape(khun));
string nextToKhun = regex.Replace(parts[0], string.Empty, 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ใช้ RegEx ในกรณีนี้ ผมเห็นว่าเกินความจำเป็นนะครับ เราต้องการแค่เช็คตัวอักษรตัวที่ 4 เท่านั้นเอง

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

เดี๋ยวเปลี่ยนเป็น start with หรือ เช็คตำแหน่งที่ 4 แทนครับ

bool nextToKhunIsVowel = false;
for (int i = 0; i < vowels.Length; i++) {
if (nextToKhun.StartsWith(vowels[i])) {
nextToKhunIsVowel = true;
break;
}
}
if (!nextToKhunIsVowel) {
return string.Join(" ", parts);
}
}

for (int i = 0; i < prefixes.Length; i++) {
Expand Down