如何查找文件中的某些单词然后执行某些操作?
如何查找文件中的某些单词,然后在发现任何单词时执行某些操作?如何查找文件中的某些单词然后执行某些操作?
我想做些事情,如果例如任何这些词banana
horse
window
whatever
被发现在一个文件中。
这是我最后一次尝试
Dim thefile As String = "C:\application\thefile"
If File.Exists(thefile) Then
Using reader As New StreamReader(thefile)
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains("Banana") OrElse line.Contains("horse") OrElse line.Contains("window") OrElse line.Contains("whatever") Then
msgbox("Word(s) found " & line)
Do_this_and_that()
Else
MsgBox("Word(s) not found")
Exit While
End If
End While
End Using
Else
msgbox("File not found")
End If
似乎有这样做的这么多的变化,但我不能让他们的工作的多个单词,而不是仅仅一个时。这样做的简单和最干净的方式是什么?
这可能是一个有点性能问题的,但你可以尝试使用List(Of String)
:
Dim thefile As String = "C:\application\thefile"
Dim toCheck as New List(of String)
'You can fill up your list by whoever you want
toCheck.Add("banana")
toCheck.Add("horse")
'...
Dim FoundWords As New List(Of String)
If File.Exists(thefile) Then
Using reader As New StreamReader(thefile)
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
'We check our list to see if it matches
For Each item in toCheck
if line.Contains(item) then
FoundWords.Add(item)
End If
Next
End While
End Using
If FoundWords.Count > 0 Then
msgbox(FoundWords.Count.ToString() & " Word(s) found")
Do_this_and_that()
Else
MsgBox("Word(s) not found")
End If
Else
msgbox("File not found")
End If
现在,这可以改善,但如果你没有的话去寻找那几千应该做的伎俩...
那么它是一个非常小的文件,我正在使用,所以也许性能不会成为我的情况下的问题。任何人,如果只有一个单词是文件,这个工作吗? msgboxes显示出来(没有找到单词和找到单词) – MadsTheMan
更新...所以现在它会将所有找到的单词放在列表中,然后告诉它找到了多少单词 –
完美!谢谢。 :) – MadsTheMan
您需要标记行并使用HashSet。这是最快的方法。把所有词语的HashSet的再检查,如果每个字是INIT:
static void Main()
{
var file = @"C:\application\thefile";
var hashSet = new HashSet<string>(new[] { "banana", "horse", "window", "whatever" }.Select(x => x.ToLower()));
foreach (var word in GetWords(file))
{
Console.WriteLine(word);
if (hashSet.Contains(word))
{
//DoSomething();
Console.WriteLine("\tFound!!");
//Continue or Break;
}
}
}
private static IEnumerable<string> GetWords(string file)
{
var rg = new Regex(@"[^\p{L}]");
const int bufferLen = 512;
using (var reader = File.OpenText(file))
{
var word = new StringBuilder();
while (!reader.EndOfStream)
{
var buffer = new char[bufferLen];
var readChars = reader.ReadBlock(buffer, 0, bufferLen);
for (int i = 0; i < readChars; i++)
{
if (rg.IsMatch(buffer[i].ToString()))//end of the word
{
if (word.Length > 0)
{
yield return word.ToString();
word = new StringBuilder();
}
}
else
word.Append(Char.ToLowerInvariant(buffer[i]));
}
}
if (word.Length > 0)
yield return word.ToString();
}
}
,并在这里VB
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim filename = "C:\application\thefile"
Dim words() As String = {"banana", "horse", "window", "whatever"}
Dim bagOfWords = New HashSet(Of String)(words.Select(Function(x) x.ToLower()))
For Each word As String In GetWords(filename)
Console.WriteLine(word)
If bagOfWords.Contains(word) Then
'DoSomething();
Console.WriteLine(vbTab & "Found!!")
'Exit For if you need to terminate here;
End If
Next
End Sub
Private Iterator Function GetWords(filename As String) As IEnumerable(Of String)
Dim rg = New Regex("[^\p{L}]")
Const bufferLen As Integer = 512
Using reader As New StreamReader(filename)
Dim word = New StringBuilder()
While Not reader.EndOfStream
Dim buffer = New Char(bufferLen - 1) {}
Dim readChars = reader.ReadBlock(buffer, 0, bufferLen)
For i As Integer = 0 To readChars - 1
If rg.IsMatch(buffer(i).ToString()) Then
'end of the word
If word.Length > 0 Then
Yield word.ToString()
word = New StringBuilder()
End If
Else
word.Append([Char].ToLowerInvariant(buffer(i)))
End If
Next
End While
If word.Length > 0 Then
Yield word.ToString()
End If
End Using
End Function
End Module
如果你需要更复杂的词来拆分单词,你应该看看类似于nltk的东西,但是对于.net –
'Tokenize'和'HashSet'对我来说都是两个陌生的词,我会对它进行一些研究。谢谢。 – MadsTheMan
你可以从C#翻译吗?我放了一个小的代码片段,但不幸的是它在c#中。为了得到文字,我使用了正则表达式。对于哈希集,它非常简单的概念,你应该看看这里https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Collections.Generic.HashSet%601.% 23); k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5); k(DevLang-csharp)&rd = true –
难道你不想在每个条件之前?你只有第一个。 –
你能否澄清你的问题?我不明白你的意思是“某个文件中的某些单词*然后使用一个if语句” - 你想找到单词然后做别的事吗?或者你(只)想看看某个单词是否在某个单词集中? – Default
当然。更新我的问题。如果在文件中找到单词(只是一个单词就足够了),我确实想做其他的事情。如果他们没有找到,什么都不要做。 – MadsTheMan