/ / Vb.net में पाठ फ़ाइल में विशिष्ट शब्दों की गणना की गणना - vb.net, शब्द-गणना

Vb.net - vb.net, शब्द-गणना में एक पाठ फ़ाइल में विशिष्ट शब्दों की संख्या की गणना

मैं पाठ फ़ाइल में किसी आइटम की संख्या को गिनने का प्रयास कर रहा हूं, प्रत्येक उदाहरण को गिनकर आइटम को प्रोग्राम में पहले फ़ाइल में दर्ज किया गया था।

मेरे पास पहले से ही फ़ाइल से पढ़ा गया और अंदर हैएक टेक्स्ट बॉक्स। समस्या यह है कि मेरा वर्तमान कोड केवल टेक्स्टबॉक्स में वर्णों की गिनती कर रहा था और मेरे वांछित शब्द की संख्या फ़ाइल में नहीं थी।

For Each desiredword As String In txtContentofFile.Text
intdesiredword = intdesiredword + 1
txtdesiredwordcount.Text = intdesiredword
Next

यह इसके बजाय टेक्स्टबॉक्स में वर्णों को गिनता हैवांछित शब्दों की संख्या की गिनती। मैंने मदद मांगने से पहले बार-बार कोशिश की और बड़े पैमाने पर खोज की, लेकिन मैं सिर्फ यह समझता हूं कि मेरे कोड के साथ क्या गलत है। कृपया सहायता कीजिए :)

उत्तर:

जवाब के लिए 0 № 1

आप उपयोग कर सकते हैं विभाजित करें समारोह :

सी#:

int count = txtContentofFile.Text.Split(desiredword).Length - 1;

VB.net:

Dim count As Integer = txtContentofFile.Text.Split(desiredword).Length - 1

जवाब के लिए 0 № 2

इसे इस्तेमाल करे:

Dim text As String = IO.File.ReadAllText("C:file.txt")
Dim wordsToSearch() As String = New String() {"Hello", "World", "foo"}
Dim words As New List(Of String)()
Dim findings As Dictionary(Of String, List(Of Integer))

"Dividing into words
words.AddRange(text.Split(New String() {" ", Environment.NewLine()}, StringSplitOptions.RemoveEmptyEntries))

findings = SearchWords(words, wordsToSearch)
Console.WriteLine("Number of "foo": " & findings("foo").Count)

इस्तेमाल किया समारोह:

Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
Dim dResult As New Dictionary(Of String, List(Of Integer))()
Dim i As Integer = 0

For Each s As String In wordsToSearch
dResult.Add(s, New List(Of Integer))

While i >= 0 AndAlso i < allWords.Count
i = allWords.IndexOf(s, i)
If i >= 0 Then dResult(s).Add(i)
i += 1
End While
Next

Return dResult
End Function

आपके पास न केवल संख्याएँ होंगी, बल्कि फ़ाइल में अनुक्रमणिका स्थितियाँ, आसानी से समूहीकृत हो जाएँगी Dictionary.


जवाब के लिए 0 № 3

मैं इस प्रकार की स्थिति में रेगुलर एक्सप्रेशंस का उपयोग करना पसंद करता हूं। वे समझने में बहुत मुश्किल हैं लेकिन वे अन्य स्ट्रिंग हेरफेर तकनीकों की तुलना में बेहद शक्तिशाली और आमतौर पर तेज हैं।

Dim AllMatchResults As MatchCollection
Try
Dim RegexObj As New Regex(desiredword)
AllMatchResults = RegexObj.Matches(txtContentofFile.Text)
If AllMatchResults.Count > 0 Then
" Access individual matches using AllMatchResults.Item[]
Else
" Match attempt failed
End If
Catch ex As ArgumentException
"Syntax error in the regular expression
End Try

आपके मामले में आप AllMatchResults.Count से मान की तलाश कर रहे हैं।

जैसे एक बढ़िया रेगुलर एक्सप्रेशन टूल का उपयोग करना RegexBuddy भावों का निर्माण और परीक्षण करना भी एक बड़ी मदद है। (उपरोक्त कोड स्निपेट RegexBuddy द्वारा उत्पन्न किया गया था!)


उत्तर के लिए -1 № 4

निम्नलिखित कोड आज़माएं

Function word_frequency(word_ As String, input As String) As Integer
Dim ct = 0
Try
Dim wLEN = word_.Length
Do While input.IndexOf(word_) <> -1
Dim idx = input.IndexOf(word_) + wLEN
ct += 1
input = input.Substring(idx)
Loop
Catch ex As Exception

End Try
Return ct
End Function