【问题标题】:Find a Name in an Email (Low-Level I/O)在电子邮件中查找名称(低级 I/O)
【发布时间】:2014-10-10 13:15:10
【问题描述】:

第 2 轮:在电子邮件中挑选领导者 好的,所以我的下一个问题是试图弄清楚谁是项目的领导者。为了确定这一点,我们会收到一封电子邮件,并且必须找到说“你想要...”的人(大小写可能会有所不同)。我觉得我的代码大部分应该都可以工作,但是我在弄清楚如何正确填充单元格数组时确实遇到了问题。我可以用它来创建单元阵列,但它只是将电子邮件重新放入其中。所以每个单元格基本上就是名字。

function[Leader_Name] = teamPowerHolder(email)

email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line

conversations = {lines}; %// Creates my cell array


while ischar(lines) %// Populates my cell array, just not correct
    Convo = fgets(email);
    if Convo == -1 %// Prevents it from just logging -1 into my cell array like a jerk
        break; %// Returns to function
    end
    conversations = [conversations {lines}]; %// Populates my list
end
Sentences = strfind(conversations,'Do you want'); %// Locates the leader position


Leader_Name = Sentences{1}; %// Indexes that position

fclose(email);
end

理想情况下,我需要它做的是找到“/n”字符(这就是我使用 fgets 的原因),但我不确定如何让它做到这一点。我试图让我的 while 循环像:

while lines == '/n'

但这是不正确的。我觉得我知道如何做'/n'位,我只是想不出。所以我会很感激一些提示或技巧来做到这一点。我总是可以尝试 strsplit 或 strtok 函数,但我需要填充我的单元格数组,这样可能会变得混乱。

请并感谢您的帮助:)

Test Case:
Anna: Hey guys, so I know that he just assigned this project, but I want to go ahead   and get started on it.
Can you guys please respond and let me know a weekly meeting time that will work for you?

Wiley: Ummmmm no because ain't nobody got time for that.

John: Wiley? What kind of a name is that? .-.

Wiley: It's better than john. >.>

Anna: Hey boys, let's grow up and talk about a meeting time.
Do you want to have a weekly meeting, or not?

Wiley: I'll just skip all of them and not end up doing anything for the project anyway.
So I really don't care so much.

John: Yes, Anna, I'd like to have a weekly meeting.
Thank you for actually being a good teammate and doing this. :)

out2 = teamPowerHolder('teamPowerHolder_convo2.txt')
    => 'Anna'

【问题讨论】:

  • 您忘记包含 MATLAB 标记。对您的帖子进行了小修改。
  • 糟糕。这可能是最重要的部分。

标签: matlab while-loop fgets low-level-io


【解决方案1】:

它不起作用的主要原因是您应该在循环中更新lines 变量,但您正在创建一个名为Convo 的新变量,它正在更新。这就是为什么每次将lines 放入元胞数组时,它只会重复放入第一行并且永远不会退出循环。


但是,我建议你阅读每一行,然后查找 : 字符,然后提取字符串直到你第一次遇到这个字符 minus 1 因为你不想包含实际的 : 字符本身。这很可能对应于说话人的姓名。如果我们错过这个事件,那么那个人还在说话。因此,您必须保留一个变量来跟踪当前仍在说话的人,直到找到“do you want”字符串。不管谁这么说,我们都会返回当前正在说话的人,当然会跳出循环!为确保该行不区分大小写,您需要将字符串转换为小写。

可能存在找不到领导的情况。在这种情况下,您可能希望返回空字符串。因此,将Leader_Name 初始化为空字符串。在这种情况下,那将是[]。这样,如果我们通过电子邮件没有找到领导者,MATLAB 将返回[]

您的逻辑非常正确,但我什至不会费心将东西存储到单元格数组中。只需检查文本文件中的每一行,并跟踪当前正在说话的人,直到我们遇到一个包含另一个 : 字符的句子。我们可以使用strfind 来促进这一点。但是,我要提到的一个小警告是,如果说话的人在他们的对话中包含 :,那么这个方法就会失效。

从我看到您的测试用例的对话来看,情况可能不会如此,所以我们没问题。因此,借用您当前的代码,只需这样做:

function[Leader_Name] = teamPowerHolder(email)

Leader_Name = []; %// Initialize leader name to empty
name = [];    

email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line

while ischar(lines)

    % // Get a line in your e-mail
    lines = fgets(email);

    % // Quit like a boss if you see a -1
    if lines == -1
        break;
    end

    % // Check if this line has a ':' character.
    % // If we do, then another person is talking.
    % // Extract the characters just before the first ':' character
    % // as we don't want the ':' character in the name
    % // If we don't encounter a ':' character, then the same person is
    % // talking so don't change the current name
    idxs = strfind(lines, ':');
    if ~isempty(idxs)
        name = lines(1:idxs(1)-1);
    end    

    % // If we find "do you want" in this sentence, then the leader
    % // is found, so quit.
    if ~isempty(strfind(lower(lines), 'do you want'))
        Leader_Name = name;
        break;
    end
end

通过用你的测试用例运行上面的代码,我得到了:

out2 = teamPowerHolder('teamPowerHolder_convo2.txt')

out2 = 

Anna

【讨论】:

  • 我希望我有你的大脑。这很有意义。我忘记了 ~isempty 函数。我想我可以找到它,然后……用换行符之类的东西做一些非常复杂的事情。
  • @JessicaMarie - 哈哈谢谢 :) 老实说,这只是需要练习。要相当擅长编程,您只需要练习并不断寻求帮助。另外,我很高兴这很有意义:)!。 isempty 非常有用……在你编程的时候,你会学到很多其他有用的功能。关于这种方法,这是对我有意义的方法。我所做的只是接受您的要求,并以我能做到的最自然的方式将其放入代码中。
猜你喜欢
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
相关资源
最近更新 更多