Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ทำยังไงถึงจะตรวจสอบคำศัพท์ในข้อความ โดยจะเก็บค่าที่ได้ไปทำอย่างอื่น



 

ทำยังไงถึงจะตรวจสอบคำศัพท์ในข้อความ โดยจะเก็บค่าที่ได้ไปทำอย่างอื่น

 



Topic : 121821



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์




ขออภัยที่หัวข้อดูงงๆครับ ผมเพิ่งหัดเขียนครับ
ผมขออธิบายรายละเอียดในเนื้อหาแทนนะครับ

สิ่งที่ผมทำ คล้ายๆกับทำ ChatBot ครับ
ผมประกาศตัวแปรไว้ดังนี้ครับ
Code (VB.NET)
Dim MInput As Object 'ประโยคที่ได้รับ
Dim MOutput As Object 'ประโยคที่จะส่งออก
Dim UName As Object 'ชื่อผู้สนทนา


แล้วทีนี้ผมต้องการตรวจหาคำศัพท์ในประโยค แล้วให้ Select Case เป็นตัวตัดสินใจในการเลือกดำเนินการครับ

เช่น ผมกำหนดให้

ประโยคครั้งแรกเป็น
Code (VB.NET)
MInput = "สวัสดีนายแดง"


ประโยคครั้งที่สองเป็น
Code (VB.NET)
MInput = "ลาก่อนนายแดง"


แล้วให้ Select Case ตรวจดูว่า ในตัวแปร MInput มีคำที่กำหนดไว้หรือเปล่า
ต่อไปนี้ผมจะเขียนคำสั่งแบบผิดๆเพื่อสมมุติเหตุการณ์ครับ
Code (VB.NET)
Select Case ตรวจสอบ MInput ว่ามีคำตามเคสไหน

Case ที่ Minput มีคำว่า "สวัสดี"
          สั่งให้ MOutput แสดงคำว่า "สวัสดีนาย" & UName

Case ที่ Minput มีคำว่า "ลาก่อน"
          สั่งให้ MOutput แสดงคำว่า "ลาก่อนนาย" & UName

Case Else
          ส่ง MInput ไปตรวจสอบในคลาสอื่น
End Select


ประมาณนี้อ่ะครับ เราต้องใช้คำสั่งไหนถึงจะให้ Select Case สามารถตรวจตามที่เรากำหนดได้อ่ะครับ

ขอบคุณผู้รู้ที่มาชี้แนะครับ



Tag : .NET, Win (Windows App), VB.NET, VS 2012 (.NET 4.x), Windows









ประวัติการแก้ไข
2016-03-08 20:16:22
2016-03-08 20:17:03
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-03-08 20:14:07 By : Jaruwat View : 1101 Reply : 7
 

 

No. 1

Guest


ถ้าใช้ if ก็พอมีทางอยู่ โดยใช้ https://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-09 08:36:30 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 2



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : ห้ามตอบเกินวันละ 2 กระทู้ เมื่อวันที่ 2016-03-09 08:36:30
รายละเอียดของการตอบ ::
ในเหตุการณ์นี้มีทางเลือกมากกว่า 2 อย่างครับ หากนำคำสั่ง If มาใช้ จะทำให้ต้องเขียนโค้ดเยอะ ยากต่อการตรวจสอบ และแก้ไขลำบากครับ
สมมุติมีการตรวจสอบคำมากกว่า 50 คำ การใช้ if จะแลดูปวดหัวมากในตอนที่มีข้อผิดพลาดแล้วต้องหาสาเหตุครับ
ผมจึงหาทางออกอื่นโดยการใช้ Select Case น่าจะง่ายกว่าครับ (ในหนังสือกอธิบายว่า Select Case คืออะไร แต่วิธีการใช้ยังคลุมเคลือ ผมจึงโพสถามครับ)


ขอบคุณมากๆสำหรับคำแนะนำครับผม

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-09 11:46:03 By : Jaruwat
 

 

No. 3

Guest


ตอบความคิดเห็นที่ : 2 เขียนโดย : Jaruwat เมื่อวันที่ 2016-03-09 11:46:03
รายละเอียดของการตอบ ::
ทำแบบนี้มันผิดตั้งแต่คิดใช้ switch แล้วเด้อ

การใช้ switch ได้มันต้องเป็นค่าที่คงที่

ถ้าคิดจะใช้ switch จริงๆ ขอบอกเลยว่าไม่มีทางเลย

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-09 12:33:29 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 4



โพสกระทู้ ( 281 )
บทความ ( 0 )



สถานะออฟไลน์


มันคือการ Replace ข้อความไม่ใช่เหรอครับ
เพราะต้องการเปลี่ยนคำหนึ่งคำเป็นอีกคำ หรือผมเข้าใจผิดไป ?

ถ้าจะตรวจสอบว่ามีคำที่ต้องการอยู่ในประโยคที่ส่งเข้ามาลองใช้คำสั่ง indexOf ดูครับ
เมื่อเจอว่ามีคำนี้อยู่ในประโยคมันจะส่ง index ตำแหน่งเริ่มต้นของคำๆนั้นกลับมาให้ครับ (>=0) ถ้าไม่มีก็ส่งกลับมาเป็น -1
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-09 14:15:35 By : deksoke
 


 

No. 5



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

หากจะมองให้แง่ของการเลือกเหตการณ์(Select Case )

ขอให้มองการ KeyDown ดูครับ

กระบวนการตั้งแต่ เรา KeyDown(Input)มันลงไปไปยันที่มันแสดงผล(Output)
Code (VB.NET)
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown

End Sub


หากต้องการกระชับ หรือ จำกัดขอบเขต ก็แค่ประกาศ Enum ไว้

Code (VB.NET)
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case (e.KeyCode)
            Case Keys.A
            Case Keys.B
            Case Keys.C
            Case Keys.D
            Case Else
End Select
End Sub


Keys.A - Keys.D คือ Enum ที่ได้ประกาศไว้ เพื่อให้ทางเลือกนั้นแคบลง

ในทางเลือกอาจจะมี ทางเลือก ซ้อนกันไปเรื่อยๆแล้วแต่รูปแบบการเขียนของคนนั้นๆ ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-09 14:38:33 By : lamaka.tor
 


 

No. 6

Guest


เหตุการณ์บนรถ "อุ้ยคลำคนแก่ ท่าทางใจดี"
Quote:
ฟ้ามืดมัวหม่น เมฆฝนครึ้มดำ
เสียงพระอ่านทำ ขออุ้ยคลำไปดี


จรัญ : อ้ายคนสึ่งตึง
xxx: สึ่งตึงหมายความว่าอย่างไร?
ผม: เป็นคำหยอก ประมาณว่า "สาวให้ท่า" แต่ไม่มีปัญญาจีบ


ถ้ามันมามากขนาดนั้น มันก็ต้องมี Algorithm เป็นตัวช่วย

Trie

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Trie
{
    public class TrieNode
    {
        public string RemainingKey = "";
        public string Value = null;
        public TrieNode[] Children = null;

        // Add a value to this node's subtrie.
        public void AddValue(string newKey, string newValue)
        {
            int index;

            // If the remaining new key is not blank and matches
            // the remaining node key, place the value here.
            if ((newKey.Length > 0) && (newKey == RemainingKey))
            {
                Value = newValue;
                return;
            }

            // If the remaining new key is blank and
            // so is the remaining node key, place the value here.
            if ((newKey.Length == 0) && (RemainingKey.Length == 0))
            {
                Value = newValue;
                return;
            }

            // If the new key is blank but the node's remaining key isn't blank,
            // move the node's remaining key into a child and place the value here.
            if ((newKey.Length == 0) && (RemainingKey.Length > 0))
            {
                // This must be a leaf node so give it children.
                Children = new TrieNode[26];
                index = RemainingKey[0] - 'A';
                Children[index] = new TrieNode();
                Children[index].RemainingKey = RemainingKey.Substring(1);
                Children[index].Value = Value;
                RemainingKey = "";
                Value = newValue;
                return;
            }

            // We need a child node.
            if (Children == null)
            {
                // Make the children.
                Children = new TrieNode[26];

                // See if we have a remaining key.
                if (RemainingKey.Length > 0)
                {
                    // Move this into the appropriate child.
                    index = RemainingKey[0] - 'A';
                    Children[index] = new TrieNode();
                    Children[index].RemainingKey = RemainingKey.Substring(1);
                    Children[index].Value = Value;
                    RemainingKey = "";
                    Value = null;
                }
            }

            // Search the appropriate subtrie.
            index = newKey[0] - 'A';
            if (Children[index] == null)
            {
                // This child doesn't exist. Make it and
                // let it represent the rest of the new key.
                Children[index] = new TrieNode();
                Children[index].RemainingKey = newKey.Substring(1);
                Children[index].Value = newValue;
                return;
            }

            // Search the appropriate subtrie.
            Children[index].AddValue(newKey.Substring(1), newValue);
        }

        // Find a value in this node's subtrie.
        public string FindValue(string targetKey)
        {
            // If the remaining key matches the
            // remaining node key, return this node's value.
            if (targetKey == RemainingKey) return Value;

            // Search the appropriate child.
            if (Children == null) return null;
            int index = targetKey[0] - 'A';
            if (Children[index] == null) return null;
            return Children[index].FindValue(targetKey.Substring(1));
        }

        // Return a textual representation of this subtrie.
        public override string ToString()
        {
            return MakeString('-', 0);
        }

        // Return a textual representation for this subtrie.
        // Variable letter is this node's letter. 
        private string MakeString(char letter, int indent)
        {
            // Start with our value.
            string result = new string(' ', indent);
            result += letter;
            if (RemainingKey.Length > 0) result += " -> " + RemainingKey;
            if (Value != null) result += " (" + Value + ")";
            result += Environment.NewLine;

            // Add the child values if we have any.
            if (Children != null)
            {
                for (int i = 0; i < 26; i++)
                {
                    if (Children[i] != null)
                    {
                        char ch = (char)('A' + i);
                        result += Children[i].MakeString(ch, indent + 2);
                    }
                }
            }
            return result;
        }
    }
}




Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Trie
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // The root of the trie.
        private TrieNode Root = new TrieNode();

        // Add a value to the trie.
        private void addButton_Click(object sender, EventArgs e)
        {
            string key = keyTextBox.Text.ToUpper();
            if (key.Length == 0)
            {
                MessageBox.Show("Key must not be blank");
                keyTextBox.Focus();
                return;
            }
            string value = valueTextBox.Text;
            if (value.Length == 0)
            {
                MessageBox.Show("Value must not be blank");
                valueTextBox.Focus();
                return;
            }

            Root.AddValue(key, value);

            trieTextBox.Text = Root.ToString();
            trieTextBox.Select(0, 0);

            keyTextBox.Clear();
            keyTextBox.Focus();
            valueTextBox.Clear();
        }

        // Find a value to the trie.
        private void findButton_Click(object sender, EventArgs e)
        {
            string key = keyTextBox.Text.ToUpper();
            string value = Root.FindValue(key);

            if (value == null) valueTextBox.Text = "null";
            else valueTextBox.Text = value;

            trieTextBox.Text = Root.ToString();
            trieTextBox.Select(0, 0);
            keyTextBox.Focus();
            keyTextBox.Select(0, keyTextBox.Text.Length);
        }

        // Uncomment to build a tree at startup for testing.
        private void Form1_Load(object sender, EventArgs e)
        {
            //Root.AddValue("หำ", "29");
            //Root.AddValue("หำน้อย", "36");
            //Root.AddValue("WANTED", "10");
            //Root.AddValue("WAN", "18");
            //Root.AddValue("หำเล็ก", "72");
            trieTextBox.Text = Root.ToString();
            trieTextBox.Select(0, 0);
        }
    }
}


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-10 12:38:47 By : หน้าฮี
 


 

No. 7

Guest


การประกาศตัวแปรแบบนี้ก็ใช้คำหยอกได้เหมือนกันว่า "อ้ายคนสึ่งตึง"
Code (VB.NET)
Dim MInput As Object 'ประโยคที่ได้รับ
Dim MOutput As Object 'ประโยคที่จะส่งออก
Dim UName As Object 'ชื่อผู้สนทนา

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-03-10 12:44:43 By : หน้าฮี
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ทำยังไงถึงจะตรวจสอบคำศัพท์ในข้อความ โดยจะเก็บค่าที่ได้ไปทำอย่างอื่น
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่